The find command plays an integral part in some of the most important scripts that are used in any Linux system.
Find Linux Command.
* Find can search for files in a directory hierarchy.
More about the find command.
By default, find returns all files below the current working directory. Further, find allows the user to specify an action to be taken on each matched file.
The find program is no longer preferred for searching for files by name in the entire filesystem. Instead, the locate programs, which use a database of indexed files (obtained through find), are more efficient.
A single white space is needed to divide syntax elements when writing a find command. In it’s simplest use the find command searches for files in the current directory and its subdirectories:
* As always, the dot indicates the current directory.
Search all directories:
find / -name “myfile” -type f -print
This searches every file on the computer for a file with the name myfile. It is generally not a good idea to look for data files this way.
Execute an action
find /var/ftp/mp3 -name “*.mp3? -type f -exec chmod 644 {} \;
This command changes the permissions of all files with a name ending in .mp3 in the directory /var/ftp/mp3.
The action is carried out by specifying the option -exec chmod 644 {} \;
The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command.
EXAMPLES
find /tmp -name core -type f -print | xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing file?names in such a way that file or directory names containing single or double quotes, spaces or newlines are correctly handled. The -name test comes before the -type test in order to avoid having to call stat(2) on every file.
find . -type f -exec file ’{}’ \;
Runs ‘file’ on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though’;’ could have been used in that case also.
Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.
Search for files which have read and write permission for their owner, and group, but which other users can read but not write to. Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.
Search for files which are writable by somebody (their owner, or their group, or anybody else).
find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w
All three of these commands do the same thing, but the first one uses the octal repre?sentation of the file mode, and the other two use the symbolic form. These commands all search for files which are writable by either their owner or their group. The files don’t have to be writable by both the owner and group to be matched; either will do.
find . -perm -220
find . -perm -g+w,u+w
Both these commands do the same thing; search for files which are writable by both their owner and their group.
find . -perm -444 -perm /222 ! -perm /111
find . -perm -a+r -perm /a+w ! -perm /a+x
These two commands both search for files that are readable for everybody (-perm -444 or -perm -a+r), have at least on write bit set (-perm /222 or -perm /a+w) but are not executable for anybody (! -perm /111 and ! -perm /a+x respectively).
Now, suppose you want to see what hidden files in your home directory changed in the last 5 days:
$ find ~ -mtime -5 -name \.\*
If you know something has changed much more recently than that, say in the last 14 minutes, and want to know what it was there’s the mmin argument:
$ find ~ -mmin 14 -name \.\*
Be aware that doing a ‘ls’ will affect the access time-stamps of the files shown by that action. If you do an ls to see what’s in a directory and try the above to see what files were accessed in the last 14 minutes all files will be listed by find.