Original article:
Http://www.cnblogs.com/serendipity/articles/2133385.html
Http://linux.chinaunix.net/techdoc/develop/2007/08/21/965951.shtml
I. Find Command Format
1. The common form of the find command is;
Find pathname-options [-print-Exec-OK...]
2. Parameters of the find command;
Pathname: directory path searched by the find command. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system.
-Print: The find command outputs matching files to the standard output.
-Exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And.
-OK: The Role of-exec is the same, but the shell command given by this parameter is executed in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute.
3. Find Command Options
-Name
Search for files by file name.
-Perm
Find the file according to the file permission.
-Prune
This option can make the find command not to be searched in the specified directory. If the-depth option is used at the same time,-prune will be ignored by the find command.
-User
Search for files by file owner.
-Group
Find the file according to the file group.
-Mtime-N + n
Find the file according to the file change time.-N indicates that the file change time is less than N days from now, and + N indicates that the file change time is earlier than N days from now. The find command also has the-atime and-ctime options, but they both have the-M time options.
-Nogroup
Find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups.
-Nouser
Find a file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.
-Newer file1! File2
Find the file whose modification time is newer than file1 but earlier than file2.
-Type
Find a type of file, such:
B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-Symbolic Link file.
F-common file.
-Size N: [c] searches for files with a length of N blocks. If a file contains C, the file length is measured in bytes.
-Depth: when searching for a file, first find the file in the current directory and then find it in its subdirectory.
-Fstype: searches for files in a certain type of file system. These file system types can usually be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system.
-Mount: the mount point of the file system is not crossed during file search.
-Follow: If the find command encounters a symbolic link file, it will trace the file to which the link points.
-Cpio: Use the cpio command to back up the files to the tape device.
In addition, the following three differences:
-Amin n
Find the files accessed in the last n minutes of the system
-Atime n
Find the last n * 24 hours of files in the system
-Cmin n
Find the file whose status is changed in the last n minutes in the system
-Ctime n
Find the file whose status is changed in the last n * 24 hours in the system.
-Mmin n
Find the file whose data is changed in the last n minutes in the system
-Mtime n
Find the file whose data has been changed for the last n * 24 hours in the system.
4. execute shell commands using exec or OK.
When using find, you only need to write the desired operation in a file, you can use exec to search with find, which is very convenient.
In some operating systems, only the-exec option is allowed to execute commands such as l s or LS-L. Most users use this option to find and delete old files. Before executing the RM command to delete files, we recommend that you use the LS command to check whether they are the files to be deleted.
The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, a \, and a semicolon. To use the exec option, you must use the print option at the same time. If you verify the find command, you will find that this command only outputs the relative path and file name from the current path.
For example, to use the LS-l command to list the matched files, you can place the LS-l command in the-exec option of the find command.
# Find.-Type F-exec LS-l {}\;
-RW-r -- 1 Root 34928 2003-02-25./CONF/httpd. conf
-RW-r -- 1 Root 12959 2003-02-25./CONF/magic
-RW-r -- 1 Root 180 2003-02-25./CONF. d/readme
In the preceding example, the find command matches all common files in the current directory and lists them using the LS-l command in the-exec option.
In the/logs directory, find the files whose changes were earlier than 5 days and delete them:
$ Find logs-type F-mtime + 5-exec RM {}\;
Remember:Before deleting a file in shell, check the corresponding file. Be careful! When using commands such as MV or RM, you can use the-exec option safe mode. It will prompt you before performing operations on each matching file.
In the following example, the find command searches for all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion.
$ Find.-Name "*. conf"-mtime + 5-OK RM {}\;
<RM.../CONF/httpd. conf>? N
Press Y to delete the file, and press n to not delete the file.
Any form of command can be used in the-exec option.
In the following example, we use the grep command. The find command First matches all files named "passwd *", such as passwd, passwd. Old, passwd. Bak, and then runs the grep command to check whether a SAM user exists in these files.
# Find/etc-name "passwd *"-exec grep "Sam "{}\;
SAM: X: 501: 501:/usr/SAM:/bin/bash
2. Example of the find command;
1. Search for all files in the current user's home directory:
The following two methods can be used:
$ Find $ home-print
$ Find ~ -Print
2. Grant the file owner in the current directory the read and write permissions, and the users in the file group and other users have the read permissions;
$ Find.-Type F-Perm 644-exec LS-l {}\;
3. In order to find all common files with a length of 0 in the system and list their full paths;
$ Find/-type F-size 0-exec LS-l {}\;
4. Search for Common files that were modified seven days ago in the/var/logs directory and ask them before deletion;
$ Find/var/logs-type F-mtime + 7-OK RM {}\;
5. To find all the files in the root group in the system;
$ Find.-Group root-exec LS-l {}\;
-RW-r -- 1 Root 595 October 31 01:09./fie1
6. The find command will delete the admin. log file that contains the numeric suffix since the access time in the directory was 7 days.
This command only checks three digits, so the suffix of the corresponding file should not exceed 999. Create several Admin. log * files before you can use the following command:
$ Find.-Name "Admin. log [0-9] [0-9] [0-9]"-atime-7-OK
Rm {}\;
<RM.../admin. log001>? N
<RM.../admin. log002>? N
<RM.../admin. log042>? N
<RM.../admin. log942>? N
7. To find and sort all directories in the current file system;
$ Find.-type D | sort