The find
command under Linux searches for files in the directory structure and performs the specified operations. find
under Linux provides a considerable number of search criteria and is very powerful. Because of its power, find
has a lot of options, most of which are worth taking the time to understand. Even if your system contains a Network File System (NFS), the find
command will work on that file system as well, as long as you have the appropriate permissions. When running a very resource-intensive find
command, many people prefer to run it in the background because it can take a long time to traverse a large filesystem (in this case, a filesystem of 30G bytes or more).
1. Command format.
find pathname -options [-print -exec -ok ...]
2. Command function.
It is used to find files in the file tree and process them accordingly.
3. Command parameters.
pathname: The path of the directory found by the find command. For example, use. for the current directory and / for the system root directory.
-print: The find command outputs the matching file to the standard output.
-exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is of the form 'command' { } \;, note the space between { } and, ;.
-ok: serves the same purpose as -exec, except that the shell command given by this argument is executed in a safer mode, and before each command is executed, the user is prompted to determine whether to execute it.
4. Command options.
- name Find files by file name.
- perm Find files by file permissions.
- prune Use this option to make the find command not look in the currently specified directory. If the -depth option is also used, then -prune will be ignored by the find command.
- user finds files by their owners.
- group Find files by the group they belong to.
- mtime -n +n finds files by their change time, - n means the file has changed within n days, + n means the file has changed n days ago. find also has the -atime and -ctime options, but they both work with the -m time option.
- nogroup Finds files without a valid group, i.e., the group to which the file belongs does not exist in /etc/groups.
- nouser Find files with no valid attribute, i.e. the file's attribute does not exist in /etc/passwd.
- nouser file1 ! file2 Finds files that have changed since file1 but are older than file2.
- type Finds files of a certain type, such as
- b - block device files.
- d - directory.
- c - Character device files.
- p - pipe files.
- l - Symbolic link files.
- f - Normal files.
- size n: [c] Finds files with n blocks of file length, with c indicating that the file length is in bytes. - depth: When finding files, first find files in the current directory, then in its subdirectories.
- fstype: Finds files located in a certain type of file system, which can usually be found in the configuration file /etc/fstab, which contains information about the file system on this system.
- mount: Find files without crossing filesystem mount points.
- follow: If the find command encounters a symbolic link file, it tracks to the file to which the link points.
- cpio: Use the cpio command on matching files to back up those files to the tape device.
Also, the difference between the following three.
- amin n finds the last N minutes of files accessed on the system
- atime n Finds the last n*24 hours of files accessed on the system
- cmin n Finds the last N minutes of the system that the file state was changed
- ctime n Find the last n*24 hours of files on the system that have been changed
- mmin n Find the last N minutes of the system that the file data has been changed
- mtime n Find the last n*24 hours of file data changed on the system
5. Examples of use.
Example 1: Find the files that have been modified within the specified time
command.
find -atime -2
Output.
[root@peidachang ~]# find -atime -2
. /logs/monitor
. /.bashrc
. /.bash_profile
. /.bash_history
Description.
Superfind files that have been modified within 48 hours
Example 2: Search by keyword
Command.
find . -name "*.log"
Output.
[root@localhost test]# find . -name "*.log"
. /log_link.log
. /log2014.log
. /test4/log3-2.log
. /test4/log3-3.log
. /test4/log3-1.log
. /log2013.log
. /log2012.log
. /log.log
. /test5/log5-2.log
. /test5/log5-3.log
. /test5/log.log
. /test5/log5-1.log
. /test5/test3/log3-2.log
. /test5/test3/log3-3.log
. /test5/test3/log3-1.log
. /test3/log3-2.log
. /test3/log3-3.log
. /test3/log3-1.log
Description.
Finds files ending with. log in the current directory." . " represents the current directory
Example 3: Find files by directory or file permissions
Command.
find /opt/soft/test/ -perm 777
Output.
[root@localhost test]# find /opt/soft/test/ -perm 777
/opt/soft/test/log_link.log
/opt/soft/test/test4
/opt/soft/test/test5/test3
/opt/soft/test/test3
Description.
Find files in the /opt/soft/test/ directory with permission 777
Example 4: Find by type
command.
find . -type f -name "*.log"
Output.
[root@localhost test]# find . -type f -name "*.log"
. /log2014.log
. /test4/log3-2.log
. /test4/log3-3.log
. /test4/log3-1.log
. /log2013.log
. /log2012.log
. /log.log
. /test5/log5-2.log
. /test5/log5-3.log
. /test5/log.log
. /test5/log5-1.log
. /test5/test3/log3-2.log
. /test5/test3/log3-3.log
. /test5/test3/log3-1.log
. /test3/log3-2.log
. /test3/log3-3.log
. /test3/log3-1.log
[root@localhost test]#
Description.
Find the current directory, ordinary files ending with. log
Example 5: Find and sort all current directories
Command.
find . -type d | sort
Output.
[root@localhost test]# find . -type d | sort
. /scf
. /scf/bin
. /scf/doc
. /scf/lib
. /scf/service
. /scf/service/deploy
. /scf/service/deploy/info
. /scf/service/deploy/product
. /test3
. /test4
. /test5
. /test5/test3
[root@localhost test]#
Example 6: Finding files by size
command.
find . -size +1000c -print
Output.
[root@localhost test]# find . -size +1000c -print
. /test4
. /scf
. /scf/lib
. /scf/service
. /scf/service/deploy
. /scf/service/deploy/product
. /scf/service/deploy/info
. /scf/doc
. /scf/bin
. /log2012.log
. /test5
. /test5/test3
. /test3
[root@localhost test]#
Description.
Find files larger than 1K in the current directory
Reference: