Use of the Find command under Linux

Source: Internet
Author: User

Why use the Find command?

Linux system has thousands of files, if you want to find the file you want, a search software is essential, and locate is based on its database to find, although the speed is slightly faster, but not real-time lookup, some new files or directories are not matched, And locate is a fuzzy match, and the Find command is real-time lookup, and for exact matching, if you have enough permissions on the directory, what to look for!

Syntax format for the Find command:

Find [Find path] [find condition] [processing action]

         

[[email protected] test]# find -type f -ls //  If you do not specify a lookup path, find 461307 in the current path     4 -rwxrwxrwx   1 root     root            25 aug 15 21:41 ./f3461333     0 -rwxr--r--   1 root     root             0 aug 15 16:46 ./f1   1700    4 -RW---x--x   1 root      root            6 Aug 15  17:50 ./f2461335    4 -rw-r--r--   1 root      root           23 Aug 15  17:49 ./f1.gz

1) Search by file name and Inode:

-name "file name": Support for using Glob
*, ?, [], [^]

[[email protected] test]# find/root-name F3//Find/root directory under file name F3/root/f3/root/test/f3[[email protected] test]# find/root -name f? Find when the last use "" to cause the file name, otherwise it may be problematic find:paths must precede expression:f2Usage:find [-h] [-l] [-P] [-olevel] [-D Help|tree|searc H|STAT|RATES|OPT|EXEC] [path ...] [Expression] [[email protected] test]# find/root-name "F?"//Add quotation marks to find out? Represents any single character/root/f3/root/test/f3/root/test/f1/root/test/f2/root/f1/root/vmware-tools-distrib/etc/messages/fr/root /desktop/f1/root/desktop/f2/root/etc2016-08-11/vmware-tools/messages/fr/root/etc2016-08-11/etc/vmware-tools/ Messages/fr

2) Search according to the owner and the genus Group

         -user USERNAME: Find files that belong to the specified user (UID)
         -group GRPNAME: Finding files with a group of specified groups (GID)
        - Uiduserid: Find the file with the specified UID number as the owner
        -gidgroupid: Find a file that belongs to the specified GID number
        -nouser: Finding files that are not owned by the master
         -nogroup: Finding files that are not owned by a group

[[email protected] test]# find /var/ -user xiaoshui //Find/var/ Directories subordinate to Xiaoshui files or directories/var/spool/mail/xiaoshui[[email protected] test]# find /tmp - group xiaoshui[[email protected] test]# find /var -nouser -ls //Find/Var /directory without the owner of the file or directory 67109563    0 -RW-RW----   1 1002      mail            0 Jul  27 15:46 /VAR/SPOOL/MAIL/SHUI67109834    0 -RW-RW----   1  1005     mail             0 jul 30 15:54 /var/spool/mail/user167109549    0 - RW-RW----   1 1006     mail             0 aug  1 15:46 /VAR/SPOOL/MAIL/GENTOO67401595    4 -RW-RW----    1 4330     mail           646 aug 16 09:19 /var/spool/mail/12

3) Search by file type

-type Type:
F: Normal file
D: Catalog file
L: Symbolic Link file
S: Socket file
B: Block device files
C: Character device file
P: Pipeline File

[[email protected] test]# Find-type f//Find the normal file under the current directory./f3./f1./f2./f1.gz

4) According to the file size to find:

-size [+|-] #UNIT
Common units: K, M, G
#UNIT: (#-1, #], such as: 6k
-#UNIT: [0,#-1], such as: -6k
+ #UNIT: (#,∞), such as: +6k

[[email protected] test]# find-size-2k//Find files in the current directory with a small file size of 2k. /f3./f1./f2./f1.gz

5) Based on time stamp:

-atime[+|-]#,
#: [#,#+1]
+#: [#+1,∞]
-#: [0,#]
-mtime//with "Day" as unit;

-ctime//"Day" as the unit;

-amin//in "minutes":

-mmin in "minutes":

-cmin in "minutes":

[[email protected] test]# find-atime-7//means to find files accessed within seven days of the current directory. /f3./f1./f2./f1.gz

6) Search by permissions

-perm [/|-]mode

MODE: Exact permission match
/mode: The permission of any class (U,g,o) object can be a match, or a relationship, + from the CENTOS7 start elimination
-mode: Each class of objects must have the specified permission, and the relationship
0 indicates no concern

[Email protected] test]# find-perm-002//means matching when others have W permissions./f3[[email protected] test]# lltotal 12-rwxr--r--1 root root 0  16:46 f1-rw-r--r--1 root root 17:49 f1.gz-rw---x--x 1 root root 6 in 17:50 f2-rwxrwxrwx 1 root root 21:41 F3[[email protected] test]# find-perm 644//indicates the exact search for a file or directory with permission 644 in the current directory. /f1.gz

Combination conditions


With:-A
Or:-O
Non:-not,!


Default to-A

De Morgan Law:
Non (p and q) = (not P) or (not Q)
Non (p or q) = (not P) and (non-Q)

650) this.width=650; "alt=" 2016-08-16_143250.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471329400783690.png "title=" 1471329400783690.png "style=" border:0px; "/>


Specific performance is

! A-a! B =! (A-o B)
! A-o! B =! (A-a B)

[[email protected] ~]# find/var-not-user root-not-user lp-not-user gdm|wc-l124//means to find files or directories that belong to the owner and not ROOT,LP,GDM [email Protected] ~]# find/var-not-user root-not-user lp-not-user gdm|wc-l124//The use of Morgan's law is to the owner is not root or LP, or GDM to reverse.


Handling actions

-print: The default processing action, display to the screen;
-ls: Similar to performing a "ls-l" command on a found file
-delete: Delete the found file;
-flsfile: Long format information for all files found is saved to the specified file
-ok COMMAND {} \; Executes commands specified by the command for each file found;
Users are interactively asked to confirm the command before each file executes

-exec COMMAND {} \; Executes commands specified by command on each file found
{}: Used to reference the found file name itself

[[email protected] ~]# find -perm 640 -ls //performs ls -l67444969 on the found content]     4 -rw-r-----   1 root     ssh_keys      1679 jul 25 12:43 ./etc2016-08-11/ssh/ssh_host_rsa_ Key67444971    4 -rw-r-----   1 root      ssh_keys      227 jul 25 12:43 ./etc2016-08-11/ssh/ssh _host_ecdsa_key67444973    4 -rw-r-----   1 root      ssh_keys      387 jul 25 12:43 ./ Etc2016-08-11/ssh/ssh_host_ed25519_key134369098    4 -rw-r-----   1  root     lp            3091 jul 25 20:19 ./et[[email protected] test]# lltotal 12-rwxr--r-- 1 root root  0 aug  15 16:46 f1-rw-r--r-- 1 root root 23 Aug 15 17:49  F1.GZ-RW---x--x 1 root root  6 aug 15 17:50 f2-rwxrwxrwx 1  root root 25 aug 15 21:41 f3[[email protected] test]# find  -perm 644 -delete /perform a delete operation on the found content [[email protected] test]# ls //using LS view , and did delete the F1  F2  F3

After-school practice


1. Find the main root of the/var directory and all files belonging to the group Mail

650) this.width=650; "alt=" 2016-08-12_102514.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330456947343.png "title=" 1471330456947343.png "style=" border:0px; "/>

2. Find all files in/var directory that do not belong to root, LP, GDM

650) this.width=650; "alt=" 2016-08-16_093158.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330472666317.png "title=" 1471330472666317.png "style=" border:0px; "/>

3, find in the/var directory in the last week of its content has been modified, and the owner is not root, nor postfix files

650) this.width=650; "alt=" 2016-08-16_145430.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330580506065.png "title=" 1471330580506065.png "style=" border:0px; "/>

4. Find files that are not owned by the master or group on the current system and have been visited in the last week

650) this.width=650; "alt=" 2016-08-16_094118.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330602358010.png "title=" 1471330602358010.png "style=" border:0px; "/>

5. Find all files that are larger than 1M in the/etc directory and are of normal file type

650) this.width=650; "alt=" 2016-08-16_094214.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330617148620.png "title=" 1471330617148620.png "style=" border:0px; "/>

6. Find files with no write permission for all users in/etc directory

650) this.width=650; "alt=" 2016-08-16_094337.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330631325968.png "title=" 1471330631325968.png "style=" border:0px; "/>

7. Find at least one class of users who do not have permission to execute files in/etc directory

650) this.width=650; "alt=" 2016-08-16_095010.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330648413471.png "title=" 1471330648413471.png "style=" border:0px; "/>

8, find/etc/init.d directory, all users have execute permission, and other users have write permission files

650) this.width=650; "alt=" 2016-08-16_095257.png "src=" http://www.178linux.com/ueditor/php/upload/image/20160816/ 1471330662270486.png "title=" 1471330662270486.png "style=" border:0px; "/>


This article is from the "Endless Learning" blog, please be sure to keep this source http://dashui.blog.51cto.com/11254923/1946895

Use of the Find command under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.