The find command under Linux searches for files in the directory structure and performs the specified operation. The find command under Linux provides quite a few search conditions and is very powerful. Because find has powerful functions, it also has many options, most of which are worth our time to understand. Even if the system contains a Network File System (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions. When running a very resource-consuming find command, many people tend to execute it in the background, because traversing a large file system may take a long time (here refers to a file system of more than 30G bytes).
1. Command format:
find pathname -options [-print -exec -ok ...]
2. Command function:
Used to search for files in the file tree and handle them accordingly
3. Command parameters:
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' { } ;, pay attention to the space between { } and;.
-ok: 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 allow the user to determine whether to execute it. .
4. Command options:
-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 Search files according to their owners.
-group Find files according to 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 Find files without a valid owner, that is, the owner of the file does not exist in /etc/passwd.
-newer file1 ! file2 Find files whose change time is 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 file system mount points 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 tape devices.
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 accessed in the system in the last N minutes Files that have changed file status
-ctime n Find files whose file status has been changed in the last n*24 hours in the system
-mmin n Find files whose file data has been changed in the last N minutes in the system
-mtime n Find last n*24 in the system Files whose file data has been changed every hour
5. Usage examples:
Example 1: Find files modified within a specified time
Command:
who have modified’s modified’s within 48 hours. : Search based on keywords
Command:
find . -name "*.log"
Output:
[root@peidachang ~]# find -atime -2 . ./logs/monitor ./.bashrc ./.bash_profile ./.bash_history
Description:
Find files ending with .log in the current directory. ". "Represents the current directory
Example 3: Find files according to the permissions of the directory or file
Command:
find /opt/soft/test/ -perm 777
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
Instructions:
Find / Files with permission 777 in the opt/soft/test/ directory
Example 4: Find by type
Command:
find . -type f -name "*.log"
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
Instructions:
Find ordinary files ending in .log in the current directory
Example 5: Find all current directories and sort them
Command:
find . -type d | sort
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]#
Example 6: Find files by size
Command:
find . -size +1000c -print
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]#
Description:
Find files larger than 1K in the current directory
1. Common usage examples of find in Linux
·find path -option [ -print ] [ -exec -ok command ] {} ;
#-print Output the found file to the standard output
#-exec command {} ; -----Will find the file File execution command operation, there is a space between {} and;
#-ok is the same as -exec, except that the user must be consulted before the operation==================== ================================ -name filename #Find the file named filename
-perm #According to the execution permission Search
-user username #Search by file owner
-group groupname #Search by group
-mtime -n +n #Search files by file change time, -n means within n days, +n means n days ago
-atime -n +n #Search by file access time: 0px">-perm name #Search by group
- mtime -n +n #Search for files based on file change time, -n refers to within n days, +n refers to n days ago
-atime -n +n #Search for files based on file access time, -n refers to within n days, +n means n days ago
-ctime -n +n #Search for files based on file creation time, -n means within n days, +n means n days ago
-nogroup #Search for files without a valid group, that is, the file’s The group does not exist in the/etc/Groups without
-NOUSER #属 属 属 属 属 属, that is, the owner of the file is not stored in/etc/passwd , +n refers to n days ago
-ctime through The belonging group does not exist in the/ETC/Groups.
-NOUSER #Find files that have no valid ownership, that is, the owner of the file is not stored in/etc/passwd. Files older than f2
-type [or n bytes]
-depth #Do not cross files when checking files System mount point
-follow #If a symbolic link file is encountered, follow the file pointed to by the link
-cpio #Use the cpio command for the matching files and back them up to the tape device
-prune ==== ==============================================
$find ~ -name "*.txt" -print #Check .txt files in $HOME and display
$find . -name "*.txt" -print
$find . -name "[A-Z]*" -pri26nbsp; #Yes Matching files use the cpio command to back them up to tape devices
-prune "host*" -print #Check files starting with host
$find . -name "[a-z][a-z][0--9][0--9].txt" -print #Check with two lowercase letters and txt files starting with two numbers
$find . -perm 755 -print
$find . -perm -007 -exec ls -l {} ; #Check files that all users can read, write and execute with -perm 777
$ find . -type d -print Print directory structure
$find . ! -type d -print Print non-directory files find /usr/include -name '*.h' -exec grep AF_INEF6 {} ; Because grep cannot recursively search subdirectories , so it can be used in conjunction with find. Find the string AF_INEF6 in .h files in all subdirectories of /usr/include
$find . -type l -print $find . -size +1000000c -print -Print # Files with 100C in length
$ Find. -Size +10 -PRINT # Check the length of the length of the period with a period of 10 pieces (1 = 512 bytes) $ CD /
$ Find ETC Home Apps -Depth -PRINT | cpio -ivcdC65536 -o /dev/rmt0
$find /etc -name "passwd*" -exec grep "cnscn" {} ; #See if there is a cnscn user
$find . -name "yao*" | xargs file
$find . -name "yao*" | xargs echo "" > /tmp/core.log
$find . -name "yao*" | xargs chmod o-w ============== ======================================== find -name april* Search in the current directory Files starting with April
find -name april* fprint file Find files starting with April in the current directory and output the results to file
find -name ap* -o -name may* Find files starting with ap or may File
find /mnt -name tom.txt -ftype vfat Find the file named tom.txt and the file system type is vfat under /mnt
find /mnt -name t.txt ! -ftype vfat Find the file named tom.txt under /mnt and the file system type is not vfat
find /tmp -name wa* -type l Find the file named wa under /tmp Files that start with a symbolic link and have a type of symbolic link
find /home HOME -MMIN +60 The files that have been changed 60 minutes ago under /home
find /Home -AMIN +30 Check the files that have been accessed in the last 30 minutes
find /Home -Newer TMP.txt. Files or directories updated more recently than tmp.txt
find /home -anewer tmp.txt Check files or directories in /home that have access times more recent than tmp.txt
find /home -used -2 List files or directories After being modified, files or directories that have been accessed within 2 days
find /home -user cnscn List the files or directories belonging to user cnscn in the /home directory
find /home -uid +501 List the files or directories in the /home directory that belong to the user cnscn The file or directory of the user's recognition code is greater than 501
Find /Home -GROUP CNSCN list /Home in the file or directory of CNSCN
find /Home -Gid 501 List /Home in the inner group of 501 or directory
find /home -nouser List the files or directories in /home that do not belong to the local user
find /home -nogroup List the files or directories in /home that do not belong to the local group
find /home -name tmp.txt - maxdepth 4 columns The search depth of tmp.txt in /home is up to 3 levels
find /home -name tmp.txt -mindepth 3 Start checking from the 2nd level
find /home -empty Find files or empty directories with a size of 0
find /home Search for files or directories with hard links greater than 2
find /home -perm 0700 Search File or directory with permissions 700
find /tmp -name tmp.txt -exec cat {};
Find /TMP -Ming TMP.txt -OK RM {}; Find / -AMIN -10 # Find the file that is accessed in the last 10 minutes in the system
find /-atime -2 # Find the file visited in the last 48 hours in the system
Find /-empty # Find a file or a folder in the system
find /-group cat # Find a file
find /-mmin -5 # search that belongs to groupcat in the system in the last 5 minutes in the system Files
find / -mtime -1 #Find files that have been modified in the last 24 hours in the system
find / -nouser #Find files belonging to invalid users in the system
find / -user fred #Find files belonging to FRED in the system this User's files
Check all ordinary 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- 1 root Use the ls -l command 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 -ok rm {} ;
== ===============================================
Inquiry on the day Modified file
[root@book class]# find ./ -mtime -1 -type f -exec ls -l {} ;
==================== ==============================
Query the file and ask if you want to display it
[root@book class]# find ./ - mtime -1 -type f -ok ls -l {} ;
3ea740f4647892e65a3f42054d4901d1 ? y
-rw-r--r-- 1 cnscn cnscn 13709 January 12 12 :22 ./classDB.inc.php
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} ;
367b87dd3f04a8eaecc1e54fbf7ce776 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 found stuff to another place
find *.c -exec cp '{}' /tmp ';' If there are special files, you can use cpio, or Use this syntax:
find dir -name filename -print | cpio -pdv newdir
6) Find 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
2. How to use the find command under Linux
1. Basic usage:
find / -name file name find ver1.d ver2.d -name '*.c' -print Find ver1.d, ver2.d *.c files and print find . -type d -print Search from the current directory, only search Directory, after finding it, print the path name.
2. No error search:
find / -name access_log 2 >/dev/null
3. Find by size:
find / -size 1500c (find 1,500 bytes in size files, c means bytes)
Find / -size +1500c (Find files larger than 1,500 bytes in size, + means greater than)
Find / -size +1500c (Find files less than 1,500 bytes in size, - means less than)
4. By time:
find / -amin n last n minutes
find / -atime n last n days
find / -cmin n change status in last n minutes
find / -ctime n change status in last n days
5. Others:
find / -empty find blank files, blank folders, folders without subdirectories
find / -false find files that are always wrong in the system
find / -fstype type find files that exist in the specified file system , such as type is ext2
Find / -gid n Files with group id n
Find / -group gname Files with group name gname
Find / -depth n Prioritize the search for file content in a specified directory at a certain level
Find / -maxdepth Levels are searched in a descending manner in a certain hierarchical directory
6. Logic
-and condition and -or condition or
7. Search string
find . -name '*.html' -exec grep 'mailto:'{}