I. Common usage examples of find in Linux
· Find path-Option [-print] [-Exec-OK command] {}\;
#-Print: output the searched files to the standard output
#-Exec command {}\; ----- execute the command operation on the retrieved file. There is a space between {} And \;
#-OK is the same as-exec, however, you only need to ask the user before the operation ======================================== ===============================- name filename # search for a file named filename
-Perm # search by execution permission
-User Username # search by file owner
-Group groupname # search by group
-Mtime-N + N # Find the file based on the file change time.-N indicates that the file is less than N days, and + N indicates that the file is earlier than N days.
-Atime-N + N # query gin by file access time: 0px ">-Perm # query by execution permission
-User Username # search by file owner
-Group groupname # search by group
-Mtime-N + N # Find the file based on the file change time.-N indicates that the file is less than N days, and + N indicates that the file is earlier than N days.
-Atime-N + N # search for files based on the file access time.-N indicates that the file is less than N days, and + N indicates that the file is earlier than N days.
-Ctime-N + N # Find the file based on the file creation time.-N indicates that the file is less than N days, and + N indicates that the file is earlier than N days.
-Nogroup # Check for files with no valid group, that is, the file group does not exist in/etc/groups.
-Nouser # Check files with no valid owner, that is, the file owner is not stored in/etc/passwd.
-Newer F1! F2 file search,-N refers to N days or less, + N refers to N days ago
-Ctime-N + N # Find the file based on the file creation time.-N indicates that the file is less than N days, and + N indicates that the file is earlier than N days.
-Nogroup # Check for files with no valid group, that is, the file group does not exist in/etc/groups.
-Nouser # Check files with no valid owner, that is, the file owner is not stored in/etc/passwd.
-Newer F1! F2 # Query files whose modification time is earlier than F1 but earlier than F2
-Type B/D/C/P/L/F # Check Block devices, directories, character devices, pipelines, symbolic links, and common files.
-Size N [c] # query objects with a length of N [or n Bytes]
-Depth # search for the local directory before entering the subdirectory
-Fstype # Query files whose change time is newer than F1 but earlier than F2
-Mount # The mount point of the file system is not crossed during file query.
-Follow # If a symbolic link file is encountered, the file indicated by the link will be tracked.
-Cpio # Use the cpio command to back up the matching files to the tape device.
-Prune # ignore a directory ====================================== ============================
$ Find ~ -Name "*. txt"-print example: Check the. txt file and display it in home ‑.
$ Find.-Name "*. txt"-print
$ Find.-Name "[A-Z] *"-pri26nbsp; # Use the cpio command for matching files to back them up to tape Devices
-Prune # ignore a directory $ find.-Name "[A-Z] *"-print # Query files starting with an uppercase letter
$ Find/etc-name "Host *"-print # Query files starting with host
$ Find.-Name "commana-z00000000a-z0000000000000--9000000000--90000.txt"-print # query TXT files starting with two lower-case letters and two numbers
$ Find.-Perm 755-print
$ Find.-Perm-007-exec LS-l {}\; # check whether all files that can be read and written by users are the same as-Perm 777
$ Find.-type D-Print print the directory structure
$ Find. ! -Type D-print Print non-directory files Find/usr/include-name '*. H'-exec grep af_inef6 {}\;Because grep cannot Recursively search for subdirectories, it can be used with find. Find the af_inef6 string in the. h file in all/usr/include subdirectories.
$ Find.-type L-Print $ find.-size + 000000c-print # Query files with a length greater than 1 MB
$ Find.-size 100c-print # Query files with a length of C
$ Find.-size + 10-print # Check for files with a length of over 10 expired items (1 piece = 512 bytes) $ CD/
$ Find etc home apps-depth-print | cpio-ivcsc65536-o/dev/rmt0
$ Find/etc-name "passwd *"-exec grep "cnscn" {}\; # check whether a cnscn user exists
$ Find.-Name "Yao *" | xargs File
$ Find.-Name "Yao *" | xargs echo "">/tmp/CORE. Log
$ Find. -Name "Yao *" | xargs chmod o-w ======================== ======================================== find-name secrets l * search for file
Find-name extension L * fprint file searches for files starting with extension l in the current directory, and outputs the results to the file.
Find-name AP *-o-name may * search for files starting with AP or may
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/mntand the file system type is not vfat.
Find/tmp-name wa *-type l search for files starting with Wa and whose type is symbolic link under/tmp
Find/home-mtime-2 check files modified in the last two days under/home
Find/home-atime-1. Check the files that have been accessed within one day.
Find/home-mmin + 60 check files modified 60 minutes ago under/home
Find/home-Amin + 30 check files that have been accessed in the last 30 minutes
Find/home-newer tmp.txt check for files or directories that are earlier than tmp.txt in/home.
Find/home-anewer tmp.txt check the files or directories with a time closer to tmp.txt under/home.
Find/home-used-2 list the files or directories that have been accessed within two days after the files or directories have been modified.
Find/home-user cnscn list the files or directories belonging to the user's cnscn in the/Home Directory
Find/home-UID + 501 list files or directories with user IDs greater than 501 in the/Home Directory
Find/home-group cnscn list/home files or directories in which the group is cnscn
Find/home-gid 501 list/home files or directories with group ID 501
Find/home-nouser: list files or directories that do not belong to local users in/home.
Find/home-nogroup list files or directories in/home that do not belong to the Local Group
Find/home-name tmp.txt-maxdepth 4 list/The tmp.txt in homecan be found at a maximum of three layers.
Find/home-name tmp.txt-mindepth 3 query from Layer 1
Find/home-empty
Find/home-size + 512 K Query files larger than K
Find/home-size-512 K to query files smaller than K
Find/home-links + 2 check files or directories with hard connections greater than 2
Find/home-Perm 0700 check the file or directory with the permission of 700
Find/tmp-name tmp.txt-exec cat {}\;
Find/tmp-name tmp.txt-OK RM {}\; find/-Amin-10 # Find the file accessed in the last 10 minutes of the system
Find/-atime-2 # search for files accessed in the last 48 hours in the system
Find/-empty # search for files or folders that are empty in the system
Find/-group cat # Find the groupcat file in the system
Find/-mmin-5 # search for the files modified in the last 5 minutes in the system
Find/-mtime-1 # search for files modified in the last 24 hours in the system
Find/-nouser # search for files belonging to the void user in the system
Find/-user Fred # search for files belonging to the user Fred IN THE SYSTEM
Query all common files in the current directory
-------------------------------------------------------------------------------- # Find.-Type F-exec LS-l {}\;
-RW-r -- 1 Root 34928 2003-02-25./CONF/httpd. conf
-RW-r -- 1 Root 12959 2003-02-25./CONF/magic
-RW-r -- 1 Root 180 2003-02-25./CONF. d/readme
Query all common 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, find the files whose changes were earlier than 5 days and delete them:
$ Find logs-type F-mtime + 5-Exec-OK RM {}\;
========================================================== ==========
Query the files modified on the current day
[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 {}\;
<Ls.../classdb. Inc. php>? Y
-RW-r -- 1 cnscn 13709 January 12 12:22./classdb. Inc. php
[Root @ book class] # Find./-mtime-1-type F-OK ls-l {}\;
<Ls.../classdb. Inc. php>? N
[Root @ book class] #====================================== ======================
Query and submit it to awk for processing
[Root @ book class] # Who | awk '{print $1 "\ t" $2 }'
Cnscn pts/0 ========================================== ==================
Awk --- grep --- sed [root @ book class] # DF-k | awk '{print $1}' | grep-V 'none' | sed s "// Dev \ /// G"
File System
Sda2
Sda1
[Root @ book class] # DF-k | awk '{print $1}' | grep-V 'none'
File System
/Dev/sda2
/Dev/sda1
1) Search for all * In/tmp *. h, search for "syscall_vector" in these files, and print out all file names containing "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 search for files larger than 3 m on a disk:
Find.-size + 3000 K-exec LS-LD {};
4) copy the find to another place.
Find *. C-exec CP '{}'/tmp ';' if a special file exists, you can use cpio or the following syntax:
Find Dir-name filename-print | cpio-PDV newdir
6) Search for the files changed at 16:36:37,-11-30.
# A = 'Find./-name "* PHP" '| LS-l -- full-time $ A 2>/dev/null | grep "16:36:37
Ii. usage of the find command in Linux
1. Basic usage:
Find/-name file nameFind ver1.d ver2.d-name '*. c'-print find the ver1.d and ver2.d *. c files and print them. Find.-type D-print find from the current directory, only find the directory, and print the path name. It can be used to print the directory structure.
2. No error search:
Find/-name access_log 2>/dev/null
3. Search by size:
Find/-size 1500c (search for 1,500-byte files; c Indicates bytes)
Find/-size + 1500c (find files larger than 1,500 bytes, + indicates files larger)
Find/-size + 1500c (find files smaller than 1,500 bytes,-Indicates smaller)
4. By Time:
Find/-Amin n last n minutes
Find/-atime n last n days
Find/-Cmin n last n minutes change status
Find/-ctime n changes the status in the last n days
5. Others:
Find/-empty blank file, blank folder, folder without subdirectories
Find/-false find files that are always wrong in the system
Find/-fstype type: Find the file that exists in the specified file system. For example, if the type is ext2
Find/-gid N group ID n files
Find/-group gname group file named gname
Find/-depth n preferentially searches for file content in a specified directory at a Layer
Find/-maxdepth levels search in a hierarchical directory in descending Mode
6. Logic
-And and-or conditions or
7. Search for strings
Find.-Name '*. html'-exec grep 'mailto :'{}