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:
Copy CodeThe code is as follows:
[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:
Copy CodeThe code is as follows:
[[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:
Copy CodeThe code is as follows:
[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:
Copy CodeThe code is as follows:
[[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:
Copy CodeThe code is as follows:
[[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:
Copy CodeThe code is as follows:
[[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
I. Common usage examples of Find in Linux
·find Path-option [-print] [-exec-ok command] {} \;
#-print output of the found file to standard output
#-exec command {} \; The command operation-----The file to be traced, with a space between {} and \;
#-ok and-exec are the same, except to consult the user ====================================================-name filename before Operation #查找名为filename的文件
-perm #按执行权限来查找
-user username #按文件属主来查找
-group GroupName #按组来查找
-mtime-n +n #按文件更改时间来查找文件,-n means n days or less, +n means n days ago
-atime-n +n #按文件访问时间来查GIN: 0px ">-perm #按执行权限来查找
-user username #按文件属主来查找
-group GroupName #按组来查找
-mtime-n +n #按文件更改时间来查找文件,-n means n days or less, +n means n days ago
-atime-n +n #按文件访问时间来查找文件,-n means n days or less, +n means n days ago
-ctime-n +n #按文件创建时间来查找文件,-n means n days or less, +n means n days ago
-nogroup #查无有效属组的文件 that the genus of the file does not exist in the/etc/groups
-nouser #查无有效属主的文件, that is, the owner of the file does not exist in the/etc/passwd
-newer F1!f2 Find file,-n means n days or less, +n refers to n days ago
-ctime-n +n #按文件创建时间来查找文件,-n means n days or less, +n means n days ago
-nogroup #查无有效属组的文件 that the genus of the file does not exist in the/etc/groups
-nouser #查无有效属主的文件, that is, the owner of the file does not exist in the/etc/passwd
-newer F1!F2 #查更改时间比f1新但比f2旧的文件
-type b/d/c/p/l/f #查是块设备, directories, character devices, pipelines, symbolic links, plain files
-size N[c] #查长度为n块 [or N-byte] files
-depth #使查找在进入子目录前先行查找完本目录
-fstype #查更改时间比f1新但比f2旧的文件
-mount #查文件时不跨越文件系统mount点
-follow #如果遇到符号链接文件, just follow the file that the link refers to
-cpio #对匹配的文件使用cpio命令, back them up to tape devices
-prune #忽略某个目录 ====================================================
$find ~-name "*.txt"-print #在 $HOME. txt file and displays
$find. -name "*.txt"-print
$find. -name "[a-z]*"-PRI26NBSP; #对匹配的文件使用cpio命令, back them up to tape devices
-prune #忽略某个目录 $find. -name "[a-z]*"-print #查以大写字母开头的文件
$find/etc-name "host*"-print #查以host开头的文件
$find. -name "[A-z][a-z][0--9][0--9].txt"-print #查以两个小写字母和两个数字开头的txt文件
$find. -perm 755-print
$find. -perm-007-exec ls-l {} \; #查所有用户都可读写执行的文件同-perm 777
$find.-type d-print Print directory structure
$find. !-type D-printPrint non-catalog files find/usr/include-name ' *.h '-exec grep af_inef6 {} \; because grep cannot recursively search subdirectories, it can be used in conjunction with find. Find a string in the. h file in all/usr/include subdirectories Af_inef6
$find. -type l-print $find. -size +1000000c-print #查长度大于1Mb的文件
$find. -size 100c-print # Check for files of length 100c
$find. -size +10-print #查长度超过期作废10块的文件 (1 block = 512 bytes) $CD/
$find etc Home Apps-depth-print | Cpio-ivcdc65536-o/dev/rmt0
$find/etc-name "passwd*"-exec grep "Cnscn" {} \; #看是否存在cnscn用户
$find. -name "yao*" | Xargs file
$find. -name "yao*" | Xargs echo "" >/tmp/core.log
$find. -name "yao*" | Xargs chmod o-w ====================================================== find-name april* Find files starting with April in the current directory
find-name april* Fprint file finds files starting with April in the current directory and outputs the results
Find-name ap*-o-name may* Find files that start with an AP or May
Find/mnt-name tom.txt-ftype vfat to find files with the name Tom.txt and file system type VFAT under/MNT
Find/mnt-name T.txt! -ftype VFAT under/mnt to find files with the name Tom.txt and file system type not VFAT
Find/tmp-name wa*-type L Find a file with a type of symbolic link at/tmp that begins with the name WA
find/home-mtime-2 in/home search for files that have changed in the last two days
find/home-atime-1 checked files accessed within 1 days
Find/home-mmin +60 The files that were changed 60 minutes ago at/home
Find/home-amin +30 Check the files that were accessed last 30 minutes ago
Find/home-newer Tmp.txt in/home check update time than tmp.txt files or directories
Find/home-anewer tmp.txt a file or directory that is accessed more than tmp.txt near/home
Find/home-used-2 lists files or directories that have been accessed within 2nd after the file or directory has been altered
Find/home-user CNSCN lists files or directories in the/home directory that belong to the user Cnscn
Find/home-uid +501 lists files or directories with a user's ID greater than 501 in the/home directory
Find/home-group CNSCN list files or directories in/home with group CNSCN
Find/home-gid 501 lists files or directories with a group ID of 501 in/home
Find/home-nouser lists files or directories in/home that are not local users
Find/home-nogroup lists files or directories in/home that are not part of a local group
Find/home-name tmp.txt-maxdepth 4 Lists the Tmp.txt in/home with a depth of up to 3 layers
Find/home-name tmp.txt-mindepth 3, starting from the 2nd floor.
Find/home-empty finding a file size of 0 or an empty directory
Find/home-size +512k files larger than 512k
find/home-size-512k files less than 512k
Find/home-links +2 Check hard connections more than 2 files or directories
Find/home-perm 0700 Check the file or directory with permission 700
Find/tmp-name tmp.txt-exec Cat {} \;
Find/tmp-name tmp.txt-ok rm {} \; Find/-amin-10 # finds files accessed in the last 10 minutes of the system
Find/-atime-2 # finds files accessed in the last 48 hours of the system
Find/-empty # finds files or folders that are empty in the system
Find/-group Cat # finds files that belong to Groupcat in the system
Find/-mmin-5 # finds files that have been modified in the last 5 minutes of the system
Find/-mtime-1 #查找在系统中最后24小时里修改过的文件
Find/-nouser #查找在系统中属于作废用户的文件
Find/-user Fred #查找在系统中属于FRED这个用户的文件
Check all the normal files in the current directory
--------------------------------------------------------------------------------# 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 2003-02-25./conf.d/readme
Check all the normal files in the current directory and use the LS-L command in the-e x E C option to list them
=================================================
In the/L o G S directory, look for files that change time before 5th and delete them:
$ find Logs-type f-mtime +5-exec-ok rm {} \;
=================================================
Query for files modified on the day
[[email protected] class]# Find/-mtime-1-type f-exec ls-l {} \;
=================================================
Query the file and ask if you want to display
[[email protected] class]# Find/-mtime-1-type f-ok ls-l {} \;
< LS .... /classdb.inc.php >? Y
-rw-r--r--1 cnscn cnscn 13709 January 12:22./classdb.inc.php
[[email protected] class]# Find/-mtime-1-type f-ok ls-l {} \;
< LS .... /classdb.inc.php >? N
[Email protected] class]# =================================================
Query and give it to awk to handle it.
[email protected] class]# who | awk ' {print ' \ t '
CNSCN pts/0 =================================================
awk---grep---sed [[email protected] class]# Df-k | awk ' {print '} ' | Grep-v ' None ' | sed S "/\/dev\///g"
File system
Sda2
Sda1
[Email protected] class]# Df-k | awk ' {print '} ' | Grep-v ' None '
File system
/dev/sda2
/dev/sda1
1) Find all the *.h in/tmp and look for "syscall_vector" in these files, and finally print out all the filenames that contain "syscall_vector" a) find/tmp-name "*.h" | Xargs-n50 grep syscall_vector
B) grep syscall_vector/tmp/*.h | Cut-d ': '-f1| Uniq > FileName
C) find/tmp-name "*.h"-exec grep "Syscall_vector" {} \; -print
2) Find/-name filename-exec rm-rf {} \;
Find/-name filename-ok rm-rf {} \;
3) For example, to find files larger than 3M on the disk:
Find. -size +3000k-exec ls-ld {};
4) Copy the find out to another place.
Find *.c-exec cp ' {} '/tmp '; ' If you have a special file, you can use Cpio, or you can use this syntax:
Find Dir-name Filename-print | CPIO-PDV Newdir
6) Look for files changed at 2004-11-30 16:36:37
# a= ' Find/-name ' *php ' | Ls-l--full-time $A 2>/dev/null | grep "2004-11-30 16:36:37
second, the use of the Linux find command
1. Basic usage:
Find/-name file name find ver1.d ver2.d-name ' *.c '-print find ver1.d,ver2.d *.c file and print find.-type d-print from the current directory Find, find only the directory, and when found, print the path name. Can be used to print the directory structure.
2. No Error Lookup:
Find/-name Access_log 2 >/dev/null
3. Search by Size:
Find/-size 1500c (look for a 1,500-byte file, c for bytes)
Find/-size +1500c (finds files larger than 1,500 bytes, + represents greater than)
Find/-size +1500c (finds files less than 1,500 bytes in size-Indicates less than)
4. By Time:
Find/-amin n last N minutes
Find/-atime n last n days
Find/-cmin n last N minutes change state
Find/-ctime N last n days change state
5. Other:
Find/-empty blank file, blank folder, folder without subdirectories
Find/-false files that are always wrong in the lookup system
Find/-fstype Type Locate the file that exists in the specified file system, such as type ext2
Find/-gid n set of files with ID n
Find/-group gname group file named Gname
Find/-depth N first search for file contents in a certain level of specified directory
Find/-maxdepth levels in a hierarchical directory in descending order
6. Logic
-and conditions and-or conditions or
7. Finding strings
Find. -name ' *.html '-exec grep ' mailto: ' {}
Linux Find command usage under find