1. The general form of the find command is;
find pathname -options [-print -exec -ok ...]
2. The parameters of the find command;
pathname: the directory path searched by the find command. For example, use . to represent the current directory and / to represent the system root directory.
-print: The find command outputs matching files to 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: It has the same function as -exec, except that it executes the shell command given by this parameter in a safer mode. Before executing each command, a prompt will be given to let the user determine whether implement.
3. Find command option
-name
Search for files by file name.
-perm
Find files based on file permissions.
-prune
Use this option to prevent the find command from searching in the currently specified directory. If the -depth option is also used, -prune will be ignored by the find command.
-user
Find files according to their owners.
-group
Find files by the group they belong to.
-mtime -n +n
Search files according to their change time. - n means that the file change time is within n days from now, + n means that the file change time was n days ago from now. The find command also has -atime and -ctime options, but they are the same as the -m time option.
-nogroup
Find files that do not have a valid group to which they belong, that is, the group to which the file belongs does not exist in /etc/groups.
-nouser
looks for files without a valid owner, that is, the owner of the file does not exist in /etc/passwd.
-newer file1 ! file2
Find files with change times newer than file1 but older than file2.
-type
Find files of a certain type, such as:
b - block device files.
d - Directory.
c - Character device file.
p - Pipe file.
l - Symbolic link file.
f - normal file.
-size n: [c] Find files with a file length of n blocks. With c, the file length is in bytes. -depth: When searching for files, first search for files in the current directory, and then in its subdirectories.
-fstype: Find files located 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 this system.
-mount: Do not cross the file system mount point when looking for files.
-follow: If the find command encounters a symbolic link file, it will follow the file pointed to by the link.
-cpio: Use the cpio command on matching files to back up these files to a tape device.
In addition, the difference between the following three:
-amin n
Find the files accessed in the last N minutes in the system
-atime n
Find the files accessed in the last n*24 hours in the system
-cmin n
Find the files whose file status was changed in the last N minutes in the system
-ctime n
Find the files whose file status was changed in the last n*24 hours in the system
-mmin n
Find the files whose file status was changed in the last N minutes in the system Data files
-mtime n
Find files whose file data has been changed in the last n*24 hours in the system
4. Use exec or ok to execute shell commands
When using the find command, just write the desired operation In a file, you can use exec to search with the find command. It is very convenient. Some operating systems only allow the -exec option to execute commands such as ls or ls -l. Most users use this option to find old files and delete them. It is recommended that before actually executing the rm command to delete files, it is best to use the ls command to take a look and confirm that they are the files to be deleted.
The exec option is followed by the command or script to be executed, then a pair of { }, a space and a, and finally a semicolon. In order to use the exec option, the print option must also be used. If you verify the find command, you will find that the command only outputs the relative path and file name from the current path.
For example: in order to use the ls -l command to list the matched files, you can put the ls -l command in the -exec option of the find command
# find . -type f -exec ls -l { } ;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/ magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
In the above example, the find command matches all ordinary files in the current directory and- Use the ls -l command in the exec option to list them.
Find files in the /logs directory that were changed more than 5 days ago and delete them:
$ find logs -type f -mtime +5 -exec rm { } ;
Remember: Delete by any means in the shell Before filing the file, you should check the corresponding file first, so be careful! Safe mode with the -exec option can be used when using commands such as mv or rm. It will prompt you before operating on each matching file.
In the following example, the find command searches for all files in the current directory whose file names end with .LOG and whose modification time is more than 5 days, and deletes them, but only gives a prompt before deleting.
$ find . -name "*.conf" -mtime +5 -ok rm { } ;
< rm ... ./conf/httpd.conf > ? n
Press the y key to delete the file, press the n key not to delete it.
Any form of command can be used with -exec option.
In the following example we use grep command. The find command first matches all files named "passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if there is a sam user in these files.
# find /etc -name "passwd*" -exec grep "sam" { } ;
sam:x:501:501::/usr/sam:/bin/bash