find command instructions

1, Command Overview

find command to find the file in the specified directory. Any string parameters located before will be considered directory name you want to find. Many find parameter options, and support for regular and powerful. And pipe combination can realize complex functions, the system administrators and ordinary users must master command. 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. If the server load is high so as not to use the find command in the peak period, find command is still relatively vague search consumes system resources.

2, the command syntax

find [path] [options] [parameters]

3, command 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> change the look of 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, calculated in units of 24 hours;
-daystart: calculate the time from the start of the day;
the -depth: from the specified directory deepest subdirectory start looking;
-expty: find 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 <column File>: this parameter and the specified effect similar to "-ls" parameters, but will save the results for the specified list file;
-follow: negative symbolic link;
-fprint <list file>: effect of this parameter and the specified "-print "similar parameters, but will save the results to a specified list file;
-fprint0 <list file>: this parameter and the specified effect" -print0 "similar parameters, but will save the results to a specified list file;
-fprintf <listing file> <output format>
-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 the group name matches the specified file or directory;
-help or --help: online help;
-ilname <style template>: similar to the effect of this parameter and specify "-lname" parameter, but ignores the character case of difference;
-iname <style template>: effect of this parameter and specify "-name" similar parameters, but ignores the character case of difference;
-inum <inode number>: Find conform to the specified file or directory inode number;
-ipath <template style>: effect of this parameter and specify "-path" similar parameters, but ignores the character case of difference;
-iregex <style template>: the effect of this parameter and specify "-regexe" parameters are similar, but ignores the character case difference;
-links <number of connections>: Find comply with the specified file or directory number of hard-wired;
-iname <style template>: template pattern looking as specified string symbolic link;
-ls: Suppose find instructions Return value Ture, will be listed in the file or directory name to the standard output;
-maxdepth <hierarchical directory>: directory set the maximum level;
-mindepth <hierarchical directory>: directory set minimum level;
-mmin <min>: Find specified time has been changed file or directory units in minutes;
-mount: this parameter and the specified effect "-xdev" same;
-mtime <number 24 hours>
-name <style template>: 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 find a file or directory of the local host group ID;
-noleaf: do not consider the presence of at least two directories need to have hard-wired;
-nouser: not find local host user identification code files or directories;
- ok <executing instructions>: this parameter and the specified effect "-exec" similar, but before the instruction will first ask the user if the answer "y" or "Y", the command execution is abandoned;
-path <style template>: as specified string template directory to find the style;
-perm <permission value>: Find a file or directory permissions in line with the value specified;
-print: find instructions assume that the return value Ture, will file or directory name to the list standard output. Format is a name of each column, the name of each of the front Jie "./" string;
-print0: Suppose find instructions Ture return value, 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 to the standard output. You can specify the format itself;
-prune: do not seek to find a file or directory as a string template style;
-regex <style template>: Specifies the string to find the file or directory as a template style;
-size <file size>: Find comply with specified the file size of the file;
-true: the return value of the find command are set to True;
the -type <type>: only looking for compliance with the specified file types;
-uid <user ID>: Find files or directories that match the specified user identification code;
-Used <number of days>: Find files or directories after the change had been accessed in the specified time the file or directory, a daily calculation unit ;
-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 type>: this parameter and the specified effect similar "-type" parameter, the difference is that it is connected to check for a symbol.

-o: parallel query condition
-i <Options>: Ignore case
! : Negative parameter value negated.

4, an example of command

The current list of all the files and folders in the directory and subdirectories:

1 [root@localhost ~]# find .
2 3 [root@localhost ~]# find . -print

In the  /tmp Find a .txt file name ending in directory:

1 [root@localhost ~]# find /tmp/ -name "*.txt"

As above, but ignore the case:

1 [root@localhost ~]# find /tmp/ -iname "*.txt"

Find all files ending in .txt and .bak in the / tmp directory and subdirectories:

1 [root@localhost ~]# find /tmp/ -name "*.txt" -o -name "*.bak"
2 或者
3 [root@localhost ~]# find /tmp/ \( -name "*.txt" -o -name "*.bak" \)

Find the matching file path or file:

1 [root @ localhost ~] # the Find / tmp / -path " * local * " # search path or file path or file name contains the local

Based on regular expressions to match the file path:

1 [root@localhost ~]# find /tmp/ -regex ".*\(\.txt\|\.bak\)$"

As above, but ignore the case:

1 [root@localhost ~]# find /tmp/ -iregex ".*\(\.txt\|\.bak\)$"

Negative parameters! :

Find out the / tmp directory instead of a file with a .txt ending:

1 [root@localhost ~]# find /tmp/ ! -name ".txt"

Search by file type:

find / tmp / -type type parameter

Type parameter list:
  F: regular file
  l: symbolic link
  d: directory
  c: character device
  b: block device
  s: socket
  p: Fifo

Based on the depth of directory hierarchy search:

Search directory hierarchy for the maximum file 3:

1 [root@localhost ~]# find /usr/ -maxdepth 3 -type f

Search the depth from the current directory of all the files in at least two subdirectories:

1 [root@localhost ~]# find /usr/ -mindepth 2 -type f

Search by file timestamps:

find / tmp -type f timestamps

Access time (-atime / day, -amin / min): Last user access time.
Modified (-mtime / day, -mmin / min): the file was last modified time.
State time (-ctime / day, -cmin / min): file status was last modified.

All files are accessed within the last seven days search, search all files happens to be visited during the first seven days, the search has been visited more than seven days all the files:

. 1 [the root @ localhost ~] # Find / tmp / F -type -atime - 7 days 7 # 
2 [the root @ localhost ~] # Find / tmp / F -type -atime 7 day 7 day # 
. 3 [the root ~ @localhost] # Find / tmp / F -atime + -type . 7 # than 7 days

Search all files accessed for more than 10 minutes:

. 1 [the root @ localhost ~] # Find / tmp / F -amin + -type 10 over ten minutes # 
2 [the root @ localhost ~] # Find / tmp / F -type -amin 10 # equal to ten minutes 
. 3 [the root @ localhost ~] # the Find / tmp / The -type f -amin - 10 # less than ten minutes

Find out the ratio a.txt file in the / tmp directory of all files modified for longer:

1 [root@localhost ~]# find /usr/ -type f -newer /tmp/a.txt

Matching based on file size:

find / tmp -type f size file size unit

File Size Unit:
  B: a block (512 bytes)
  C: Byte
  w: word (2 bytes)
  K: kilobytes
  M: megabytes
  G: G bytes

Search for files larger than 10KB, the search for the file is less than 10KB, and 10KB file search is equal to:

1 [root@localhost ~]# find /tmp/ -type f -size +10k     #大于10k
2 [root@localhost ~]# find /tmp/ -type f -size -10k     #小于10k
3 [root@localhost ~]# find /tmp/ -type f -size 10k      #等于10k

Delete files that match:

Delete all .txt files in the current directory:

1 [root@localhost ~]# find /tmp/ -type f -name "*.txt" -delete

According file permissions, ownership of the match:

In the / tmp directory permissions to search out the 777 files, search the permissions in the / tmp directory that is not 644 files:

1 [root@localhost ~]# find /tmp/ -type f -perm 777
2 
3 [root@localhost ~]# find /tmp/ -type f ! -perm 644

Find out the / tmp directory of all files owned by user liuzgg:

1 [root@localhost ~]# find /tmp/ -type f -user liuzgg

Find all the file / tmp directory owned by user group job:

1 [root@localhost ~]# find /tmp/ -type f -group job

With -execoption in conjunction with other commands:

Find out the root of all files in the / tmp directory, and change the ownership to the user liuzgg:

. 1 [the root @ localhost ~] # Find / tmp / F -type -user the root -exec chown liuzgg {} \; 
above example, {}  for the -exec option to match all of the files used in combination, and will be replaced the file name.

Find all .txt files in your home directory and delete:

. 1 [the root @ localhost ~] $ # Find the HOME / -name. " * .Txt " - OK RM {} \;
 2 [the root @ localhost ~] $ # Find the HOME / -name " * .txt " - OK {RM } \;
 . 3 [the root @ localhost ~] $ # Find the HOME -name " * .txt " - ; RM {} \ OK
 . 4 [the root @ localhost ~] ~ # Find -name " * .txt " -OK RM {} \; 
above all in mode 4, pay attention: the hidden files with .txt also be deleted.
The example above, -ok and -exec behavior the same, but it will prompt you whether appropriate action.

Find / tmp directory at all .txt files and splicing them together to write all.txt file:

1 [root@localhost ~]# find /tmp/ -type f -name "*.txt" -exec cat {} \;> /tmp/aall.txt

Copy the .log file 30 days prior to the old directory:

1 [root@localhost ~]# find /tmp/ -type f -mtime +30 -name "*.txt" -exec cp {} /tmp \;

Find / tmp directory at all .txt files and to: print out the form in the "File name" of:

1 [root@localhost ~]# find /tmp/ -type f -name "*.txt" -exec printf "File: %s\n" {} \;
2 File: /tmp/11111.txt
3 File: /tmp/aall.txt
4 File: /tmp/all.txt
5 [root@localhost ~]# 

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 the search out of the specified directory, find the current directory or subdirectories all .txt files, but skip the subdirectory / tmp / work:

1 [root@localhost ~]# find /tmp/ -path "/tmp/work" -prune -o -name "*.txt"

 

 

 

Guess you like

Origin www.cnblogs.com/liuzgg/p/11699199.html