Linux basic commands -find

find command

Citation

find command to find files in the specified directory. Any string parameters located before will be considered directory name you want to find. If you use this command does not set any parameters, the find command will find subdirectories and files in the current directory. And will look into all the subdirectories and files are displayed.

grammar

the Find (option) (parameters)
Options
-amin <min> : Find had been accessed in the file or directory specified time, the unit in minutes;
 -anewer <reference file or directory> : find its access time than the access time of the file or directory specified closer now file or directory;
 -atime <24 hours the number> : Find had been accessing the file or directory at a specified time, computing unit 24 hour;
 -cmin <min> : Find is changed at specified time off file or directory;
 -cnewer <reference file or directory> Find change its time than the specified time to change the file or directory is closer to the current file or directory;
 -ctime <24 hours number> : Find files are changed at the specified time or directory, unit to 24 hours;
 - dAY,: calculation time from this date;
 - depth: from the specified directory deepest subdirectory start looking;
 - expty: Looking at a file size of 0 Byte file or directory without any empty subdirectories or files;
 -exec <execute command> : suppose find instructions return value is True, on the implementation of the directive;
 - false : the return value of the find command are set to false
-fls <list file>: this parameter and the specified effect "- LS " similar parameters, but will result saved in the specified file list;
 - Follow: negative symbolic link;
-fprint <list file>: this parameter and the specified effect "- Print" similar parameters, but will save the results to a specified list file;
 -fprint0 <list file>: this parameter and the specified effect - " Similar -print0" parameter , but will save the results to a specified list file;
 -fprintf <list file> <output format>: effect of this parameter and the specified "- the printf" parameters are similar, but will save the results to a specified list file;
 -fstype < file system type> : just look for the file or directory in the file system type;
 -gid <group ID> : find qualified designated group ID of a file or directory;
 -group <group name> : Find comply with specified the group file or directory name;
 - help or --help: online help;
 -ilname <style template>: effect of this parameter and specify "- lname" similar parameters, but ignores the character case of difference;
 -iname < Model style>: effect of this parameter and specify "- name" parameter similar, but ignores the character case of difference;
 -inum <inode number> : Find conform to the specified file or directory inode number;
 -ipath <Style template>: effect of this parameter and specify "- path" parameter is similar, but the difference ignores the character case;
 -iregex <style templates>: Finding matches the specified number of files or directories hardwired;
 -iname <style template> : template pattern looking as specified string symbolic link;
 - LS : Suppose find command return value Ture, will file or directory name list to the standard output;
 -maxdepth <directory level> : Sets the maximum directory hierarchy;
 -mindepth <directory level> : set the minimum directory hierarchy;
 -mmin <minute> : Find a file or directory has been changed in the specified time, the unit in minutes;
 - Mount : the effect of this parameter and specify "- XDEV" the same;
 -mtime <24 hours number> : Find a file or directory has been changed in the specified time, the unit is calculated in 24 hours;
 -name <template style> : Specifies the string to find the file or directory as a template style;
 -newer <reference file or directory> : change the look of its time than the specified time to change the file or directory is closer to the current file or directory;
 - nogroup: not figure out belonging to the local host group ID of the file or directory;
 - noleaf: do not consider the presence of at least two directories need to have hard-wired;
 - NoUser: identify not a Local host user identification code file or directory;
 -OK <executing instructions>: effect of this parameter and specify "-exec "is similar, but before executing the command will first ask the user if the answer" y "or" Y ", then gives up command;
 -path <style template> : Specifies the string as a directory to find a template style;
 -perm <rights value> : Find file or directory permissions meet specified value;
 -print: Suppose find instructions Ture return value, will be listed in the file or directory name to the standard output format is a name of each column, before each name. Jie "./ " string;
 - -print0: Suppose find command return value Ture, will be listed in the file or directory name to the standard output format names are all in the same line;.
 -printf <output format> : Suppose find command return value Ture, will be listed in the file or directory name can be specified in the standard output itself;.
 - Prune: do not seek to find a file or directory as a character string template pattern;
 -regex <style template> : as specified string to find the file or directory template style;
 -size <file size> : find files meeting specified file size;
 - to true : the return value of the find command are set to True
-typ <type> : only looking for compliance with the specified file types;
 -uid <User ID> : Find files or directories that match the specified user identification code;
 <days> -used-user <owner name> : Find files or directories specified owner and operator name;
 - Version or --version: show the version information;
 - XDEV: the limited range of the first file system;
 -xtype <file types >: this parameter and the specified effect similar "-type" parameter, the difference is that it is connected to check for a symbol.

parameter

Home directory: find the home directory of the file.

Examples

According to a file or to match a regular expression

It lists the current directory and subdirectories of all files and folders

find .
In the /homeFind ending in .txt filename directory
find /home -name "*.txt"
As above, but ignore case
find /home -iname "*.txt"
The current find all files ending in .txt and .pdf directory and subdirectories
find . \( -name "*.txt" -o -name "*.pdf" \)
or
find . -name "*.txt" -o -name "*.pdf" 
Matching file path or file
find /usr/ -path "*local*"
Regular expression matching files based on the path
find . -regex ".*\(\.txt\|\.pdf\)$"
As above, but ignore case
find . -iregex ".*\(\.txt\|\.pdf\)$"
Negative Parameters

Identify under / home is not a file ending in .txt

find /home ! -name "*.txt"
Search by file type
the Find . The -type parameter type
Type parameter list:
  • regular file
  • symbolic link
  • d  directory
  • character device
  • block device
  • socket
  • Fifo

Based on the depth of directory search

3 down to the maximum depth limit

find . -maxdepth 3 -type f
Search all the files in the current directory depth from at least two subdirectories
find . -mindepth 2 -type f
Search by file timestamps
the Find . The -type f timestamp
Each file UNIX / Linux file system comes in three timestamp:
  • Access time (-atime / day, -amin / min): Last user access time.
  • Modified (-mtime / day, -mmin / min): the file was last modified time.
  • Change Time (-ctime / day, -cmin / min): the last file data element (e.g. permissions) modification time.

Been visited in the last seven days search all files

find . -type f -atime -7
Search happens to be visited during the first seven days of all documents
find . -type f -atime 7
It has been visited by more than seven days in the search for all files
find . -type f -atime +7
Search over 10 minutes of access time of all files
find . -type f -amin +10
To find than file all .log file modification times longer
find . -type f -newer file.log
Matching based on file size
Find . -size F -type file size unit
File Size unit:
  • B  - the block (512 bytes)
  • C  - Byte
  • W  - a word (2 bytes)
  • k  - kilobytes
  • M  - megabytes
  • G  - Gigabytes

Searching for files larger than 10KB of

find . -type f -size +10k
Search for files of less than 10KB
find . -type f -size -10k
Search equal to the file of 10KB
find . -type f -size 10k
Delete matching files

Delete all .txt files in the current directory

find . -type f -name "*.txt" -delete
According matching file permissions / ownership

Search the current directory permissions to 777 files

find . -type f -perm 777
Find out the current directory permissions are not 644 php file
find . -type f -name "*.php" ! -perm 644
Directory user to find all the files currently owned by tom
find . -type f -user tom
Identify all current file directory user groups have sunk
find . -type f -group sunk
With -execoption in conjunction with other command

Find out the root of all files under the current directory, and change the ownership to the user tom

find .-type f -user root -exec chown tom {} \;
In the above example, {}  for the -exec combination to match all files option, and then is replaced by the corresponding file name.

Find all .txt files in your home directory and delete

find $HOME/. -name "*.txt" -ok rm {} \;
The example above, -ok and -exec behavior the same, but it will prompt you whether appropriate action.

Find all .txt files under the current directory and stitching them together to write a file all.txt

find . -type f -name "*.txt" -exec cat {} \;> all.txt
Move .log file 30 days prior to the old directory
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
Find all .txt files in the current directory and to: print out the form in the "File name" of
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
Because the single-line command can not use a plurality of parameters -exec command, the following methods can be implemented after accepting multiple commands -exec
-exec ./text.sh {} \;
But out of the specified directory search

Find all .txt files in the current directory or subdirectories, but skip the subdirectories sk

find . -path "./sk" -prune -o -name "*.txt" -print
find other tricks to collect

To list all zero-length files

find . -empty

 

Guess you like

Origin www.cnblogs.com/lj7xun/p/10986067.html