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:
Pathname: The directory path that the Find command looks for. For example , use. To represent the current directory, and /to represent the system root directory.
-Print: The find command outputs the matched file to standard output.
-exec: The find command executes the shell command given by this parameter to the matching file . The corresponding command is in the form of ' command ' { } ;, note the space between {} and;
-OK: and -exec function the same, but in a more secure mode to execute the parameters given by the Shell command, before executing each command, will give a prompt for the user to determine whether to execute.
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 the-depth option is used at the same time , then -prune will be ignored by the find command.
-user finds files according to the owner of the file.
-groupfinds files according to the groups to which the files belong.
-mtime -n +N to find the file according to the file change time, - n means that the file change time is less than n days,+ n means that the file change time is now N days ago. the Find command also hasthe-atime and -ctime options, but they are both and -m time options.
-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, i.e. the owner of the file does not exist in/etc/passwd.
-Newer file1 ! file2 Look for a file that changes time than the file File1 new but older than the file file2.
-Type tofind 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] Find 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 it up 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 n24-hour Access file in the system
-cmin n Find files in the last n minutes of the system changed file status
-ctime N Lookup System last n. 24 hours file status changed
-mmin n Find files in the last n minutes of the system changed file data
-mtime N Find the last n24-hour file that changed file data in the system
5. Usage Examples:
Example 1: Finding files that have been modified within a specified time
Command:
Find -atime -2
Output:
[root@peidachang ~]# find-atime-2
.
. /Logs/monitor
. /. BASHRC
. /. Bash_profile
. /. Bash_history
Description: Super-Find files 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 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:
[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 with permission 777 under the/opt/soft/test/directory
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 common files that end with a. Log in a directory
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: Find 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 with current directory larger than 1K
One Linux command per day: Find command overview