The Linux Find command searches the directory structure for files and performs the specified actions. The Linux Find command provides quite a lot of search criteria and is powerful. Because find has powerful features, it has a lot of options, most of which are worth taking the time to look at. Even if the system contains a network file system (NFS), the Find command works equally well in the file system, and you only have the appropriate permissions. When running a very resource-intensive find command, many people tend to put it in the background because it can take a long time to traverse a large file system (this refers to a file system with more than 30G bytes).
1. Command format:
Find Pathname-options [-print-exec-ok ...]
2. Command function:
Used to locate files in the file tree and to make corresponding processing
3. Command parameters:
The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.
The-print:find command outputs the matched file to standard output.
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;
-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.
4. Command options:
-name finds files by file name.
-perm to find files according to file permissions.
-prune Use this option to have the Find command not be found in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command.
-user Search for files according to the owner of the file.
-group finds files according to the group to which the files belong.
-mtime-n +n The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they both and the-m time option.
-nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups.
-nouser finds a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.
-newer file1! File2 look for a file that changes time than the file File1 new but older than the file file2.
-type find a file of a certain type, such as:
B-block device files.
D-Directory.
C-character device file.
P-Pipeline file.
L-Symbolic link file.
F-Normal file.
-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes. -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.
-fstype: Find files located in a file system of a certain type, these file system types can usually be found in the configuration file/etc/fstab, which contains information about the file system in this system.
-mount: Does not cross the file system mount point when locating files.
-follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.
-cpio: Use the cpio command for matching files to back up these files to the tape device.
In addition, the following three differences:
-amin N Find the last n minutes of files accessed in the system
-atime N Find the last n*24 hour Access file in the system
-cmin n Find files in the last n minutes of the system changed file status
-ctime n Find files that have changed file status in the last n*24 hours of the system
-mmin n Find files that have changed file data in the last N minutes of the system
-mtime n Find files that have changed file data for the last n*24 hours in the system
5. Usage examples:
Example 1: Finding files that have been modified within a specified time
Command:
Find-atime-2
Output:
[Email protected] ~]# find-atime-2
.
./logs/monitor
./.BASHRC
./.bash_profile
./.bash_history
Description
Find files that have been modified within 48 hours
Example 2: Search by keyword
Command:
Find. -name "*.log"
Output:
[[email protected] 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 that end 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:
[Email protected] 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 with permission 777 under the/opt/soft/test/directory
Example 4: Find by Type
Command:
Find. -type f-name "*.log"
Output:
[[email protected] 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
[Email protected] test]#
Description
Find common files that end with a. Log in a directory
Example 5: Find and sort all current directories
Command:
Find. -type D | Sort
Output:
[[email protected] 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
[Email protected] test]#
Example 6: Find files by size
Command:
Find. -size +1000c-print
Output:
[[email protected] 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
[Email protected] test]#
Description
Find files with current directory larger than 1K
One Linux command per day: Find command overview