![]() ![]() ![]() The file -bi command will output the MIME type of the file. If the file was found to be a shell script, the find command will proceed to output the file's pathname (the -print at the end, which could also be replaced by some other action). If the output does not contain that string, it exits with a non-zero exit status which causes find to continue immediately with the next file. This script runs file -bi on the found file and exits with a zero exit status if the output of that command contains the string /x-shellscript. The find command above will find all regular files in or below the current directory, and for each such file call a short in-line shell script. ]' bash \ -printĪdd -name sunrise before the -exec if you wish to only detect scripts with that name. type f -exec sh -c 'Ĭase $( file -bi "$1" ) in (*/x-shellscript*) exit 0 esac Using file with find to detect the MIME type of regular files, and use that to only find shell scripts: find. These types of data may however be distinguished by the file utility, which looks at particular signatures within the files themselves to determine type of the file contents.Ī common way to label the different types of data files is by their MIME type, and file is able to determine the MIME type of a file. The find utility can not by itself distinguish between a "shell script", "JPEG image file" or any other type of regular file. These are the type of files that find can filter on with its -type option. This also assumes a bourne-like shell ( ksh and bash should work."File types" on a Unix system are things like regular files, directories, named pipes, character special files, symbolic links etc. In the non-GNU version, awk find devices with a mount starting with /dev to get real file systems and then print the last field (the mountpoint) from the df output. The grep -F excludes other things returned, like swap partitions. Other versions might have other parameters or need it left out. P makes GNU df give standard POSIX output. (This is a bit fragile if any of your mountpoints have spaces in) ( df parameters might need tuning. $ find $(df -P|awk '$1 ~ /^\/dev/ ') -xdev | grep -F -i book1 # Non-GNU based OSes. If you have multiple partitions and you don't know on which one the file is on, you can get a list with lsblk (on Linux-based OSes, parsing df output is an option otherwise) and feed that into find: (root again if you don't know if you can access the file) $ find $(lsblk -O MOUNTPOINT -n | grep -F /) -xdev -iname 'book1*' -print # GNU-based OSes (Errors can be hidden by appending 2> /dev/null at the end of the find command (the comment should be removed)) If you do not include -xdev find searches things on other partitions, like /proc and /sys, which tend to flood your screen with errors, especially if you are not root. $ find / -xdev -print | grep -F -i /book1 # if iname is not available If you have a single partition: (run as root if your user might not have access to the file) $ find / -xdev -iname 'book1*' -print # If the iname extension to find is available It tend to be much slower, but also much more precise. If locate is not available, you can use find instead. If file is on the system for more than a day it should already be in the index and this can be skipped # updatedb # run as root, possibly using sudo, e.g. Locate and its variants tend to be a fast method. The criteria you can tell find to exit after finding the first matching file: find / -iname 'book1*' -print -quit 2>/dev/null I also suggest putting 2>/dev/null at the end of the line to hideĪll *permission denied* and other errors that will be present if you invoke find as a non-root user: find / -iname 'book1*' 2>/dev/nullĪnd if you're sure that you're looking for a single file, and there is only a single file on your system that match If you don't know the full filename, capitalization and location indeed you should use something like this: find / -iname 'book1*' Looking for is actually in your $HOME directory if you worked on Third, remember about quoting the pattern as said in the otherĪnd last - are you sure that you want to look for the fileĮverywhere on your system? It's possible that the file you're Then don't use -iname but -name, it will be faster: find / -name 'Book1.gnumeric' You're sure the file you're looking for is called Book1.gnumeric ![]() Specify -iname book1 it might also find Book1, bOok1 etc. Second, -iname will make find ignore the filename case so if you Or specify the full name: find / -iname 'Book1.gnumeric' String book1 match Book1.gnumeric you either have to add * so it Gist is that in order for find to actually find a file theįilename must match the specified pattern. First, an argument to -iname is a shell pattern. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |