The Linux find command

Linux find command to find the file 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

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

Parameters  :

judge path and find expression in accordance with the following rules, listed first in a command -! (), as part of the previous path, after that expression. If the path is an empty string is using the current path, if the expression is an empty string is used as the default -print expression.

expression can be used in as many as twenty or thirty options, this only describes the most commonly used parts.

-mount, -xdev: only checks the file and specify a directory in the same file system, avoid listing files in other file systems

-amin n: it has been read in the past n minutes

-anewer file: the file is later than the file has been read file

-atime n: it has been read in the last n days Files

-cmin n: it has been modified in the last n minutes

-cnewer file: file than the file file update

-ctime n: it has been modified in the last n days Files

-empty: Empty file -gid n or -group name: gid is a group name is a name or n

-ipath p, -path p: p line with the path name of the file, ipath ignore case

-name name, -iname name: file name matches the name of the file. iname ignore case

-size n: n is the file size units, b represents the block group 512 yuan, c denotes the number of characters, k denotes kilo bytes, w is two bytes. -type c: c is the file type of file.

d: Contents

c: means font file

b: block file means

p: named storage column

f: General Files

l: symbolic link

s: socket

-pid n: process id is n file

Search by file name

[root @ localhost ~] # find the search path [options] Search Content 
options: 
-name: search by file name 
-iname: search by file name, file names are not case-sensitive 
-inum: Search by inode number

Search by file size

[root @ localhost ~] # the Find search path [options] Search Content 
options:
 -size [+ | -] Size: Specifies the size of the search by file

Here, " + " means searching bigger than the size of the file is specified, " - " means that the search size smaller than the specified file.

 -size n[cwbkMG]
              File uses n units of space.  The following suffixes can be used:

              `b'    for 512-byte blocks (this is the default if no suffix is used)

              `c'    for bytes

              `w'    for two-byte words

              `k'    for Kilobytes (units of 1024 bytes)

              `M'    for Megabytes (units of 1048576 bytes)

              `G'    for Gigabytes (units of 1073741824 bytes)

              The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.  Bear in mind that the `%k' and
              `%b' format specifiers of -printf handle sparse files differently.  The `b' suffix always denotes 512-byte blocks and  never  1  Kilobyte  blocks,
              which is different to the behaviour of -ls.
 

In accordance with the modified search
Linux files have access time ( atime ), data modification time ( mtime ), state modification time ( ctime ) three
times, we can follow the time to search for files.

[root @ localhost ~] # the Find search path [options] Search Content 
options:
 -atime [+ | - ] Time: Search by file access time
 -mtime [+ | - ] Time: Search by file modification time data
 -ctime [+ | -] time: Search by file modification time status

 

The difference between the three of us in time stat command already explained, here mtime modification time data, for example, focus on that
say " [+ -] " the meaning of time.
-5 : representatives 5 files modified days.
5 : representatives before 5 ~ 6 days that one day the modified file.
+5 : on behalf of 6 file days before the modification.

Search by permission

[root @ localhost ~] # the Find search path [options] Search Content 
options:
 - Perm permission modes: Find the file permissions exactly equal "right model" file
 -perm - permission modes: Find all included file permissions "permission mode" file
 Find any file permissions to a file that contains permissions "permission mode": -perm + permission mode

Search by owner and group

[root @ localhost ~] # the Find search path [options] Search Content 
options:
 - uid User ID: Find accordance with the user ID of the owner of the file ID is specified
 - gid Group ID: Find your user group ID is set in accordance with the specified ID file
 - the user username: Find the owner name is specified by the user the user's files
 - group group name: Find your group by group name is specified user group file
 -nouser: find files that have no owner 

According to owner and group when searching, " -nouser " option is more commonly used, mainly used to find junk files
with one exception, that is, external documents. Such as CD-ROM and U disk file if it is a Windows copy of the
Linux view is not the owner of the file; another example hand-source package installed files, there may be no owner by file type search
 

[root @ localhost ~] # the Find search path [options] Search Content 
options:
 - of the type d: Find directory
 - of the type f: Find a regular file
 -type l: Finding soft link file 

Logical Operators

[root @ localhost ~] # the Find search path [options] Search Content

Options:

-not : not logical not
1 ) -a : and logic and
find command also supports logical operators options, -a represents a logical AND operation, that is, -a two conditions are true, find search
search results was established. for example:

[the root @ localhost ~] # Find. -size 2K + -type -a F 
# in the current directory search is greater than 2KB, the file type and the file is an ordinary file

2 ) -o : or logical or
-o option represents a logical OR operation, that is, -o two conditions as long as one set up, the Find command to find the results. Example
such as:

[root@localhost ~]# find . -name cangls -o -name bols
./cangls
./bols

. 3 ) -not : Not logical not
-not a logical NOT, the meaning is negated. for example:

[root @ localhost ~] # the Find. -not -name cangls 
# in the current directory search for a file name is not cangls file

Other options
1 ) -exec option
here we explain the two options " -exec " and " -ok ", the basic role of these two options is very similar. We first look
to see " -exec Format" option.

[the root @ localhost ~] # Find search path [options] -exec command searches {2} \;

Secondly, the role of this option is actually to find the command results to the " -exec command" call 2 to deal with. " {} " On
behalf of find command search results.
2 ) -ok option
" -ok " option and the " -exec action" option is basically the same, except that: " -exec " command 2 will deal directly, without consultation
asked; " -ok " command 2 prior to treatment the user will be asked whether such treatment, after confirmation command, will be performed.

test:

[root@iZbp145axkc98giot5b448Z find]# touch abc
[root@iZbp145axkc98giot5b448Z find]# touch abcd
[root@iZbp145axkc98giot5b448Z find]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 19 13:48 abc
-rw-r--r-- 1 root root 0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# touch ababab
[root@iZbp145axkc98giot5b448Z find]# touch ABC
[root@iZbp145axkc98giot5b448Z find]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 0 Feb 19 13:48 abc
-rw-r--r-- 1 root root 0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root 0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# find . -name "ab*"
./abcd
./abc
./ababab
[root@iZbp145axkc98giot5b448Z find]# find . -iname "abc"
./abc
./ABC
[root@iZbp145axkc98giot5b448Z find]# vim abc
[root@iZbp145axkc98giot5b448Z find]# ll
total 4
-rw-r--r-- 1 root root   0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 251 Feb 19 13:50 abc
-rw-r--r-- 1 root root   0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root   0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# ll -h
total 4.0K
-rw-r--r-- 1 root root   0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 251 Feb 19 13:50 abc
-rw-r--r-- 1 root root   0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root   0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# vim abc
[root@iZbp145axkc98giot5b448Z find]# ll -h
total 4.0K
-rw-r--r-- 1 root root    0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 1.9K Feb 19 13:51 abc
-rw-r--r-- 1 root root    0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root    0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# find . -size +1k
.
./abc
[root@iZbp145axkc98giot5b448Z find]# find . -mtime -5
.
./abcd
./abc
./ababab
./ABC
[root@iZbp145axkc98giot5b448Z find]# find . -perm 777
[root@iZbp145axkc98giot5b448Z find]# find . -perm 644
./abcd
./abc
./ababab
./ABC
[root@iZbp145axkc98giot5b448Z find]# find . -type f
./abcd
./abc
./ababab
./ABC
[root@iZbp145axkc98giot5b448Z find]# find . -size +1k -a -type f
./abc
[root@iZbp145axkc98giot5b448Z find]# ll
total 4
-rw-r--r-- 1 root root    0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 1931 Feb 19 13:51 abc
-rw-r--r-- 1 root root    0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root    0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# find . -name "ab*" -exec rm -rf {} \;
[root@iZbp145axkc98giot5b448Z find]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 19 13:49 ABC
[root@iZbp145axkc98giot5b448Z find]# man find
[root@iZbp145axkc98giot5b448Z find]#

 

Guess you like

Origin www.cnblogs.com/dalianpai/p/12331567.html