find command in detail

Keywords: network less Google socket

Today, let's share how the find command is used.
Find: Compared with other search commands, such as locate, find is real-time and accurate, but its disadvantage is that the search speed is slow.
find usage can be summarized as follows:

find Find find Path Finding Standard Processing Action

Find Path: The file path you want to find defaults to the current path
 Finding criteria: Finding conditions, default is to find all files in the path
 Processing action: The action to be done after finding the file, default to display

Finding criteria:

  • - name FILE: Find by filename FILE
    • * Represents any length of characters
    • ?: Any single character
    • []: Any of the characters in parentheses
  • iname: case-insensitive
  • regex PATTERN: Search based on regular expression PATTERN
  • user: Find by file owner
  • Group: Find by file group
  • Uid: Find by the uid of the file owner
  • gid: Find by gid of file group
  • nouser: Find files without ownership
  • nogroup: Find files that do not belong to a group
  • Type: Find by file type
    • f: Ordinary documents
    • d: Directory
    • c: Character file
    • l: Link file
    • s: socket file
    • p: Pipeline files
    • b: Block device file
  • - size: Find by file size
    • [+ |-] k: [Files larger than | less than] KB
    • [+ |-] M: [Files greater than | less than] KB
    • [+ |-] G: [Files greater than | less than] KB
  • - mtine [+ |-] #: days [outside | inside] files whose contents have been modified
  • - ctime [+ |-]#: days [outside | inside] changed files
  • - atime [+ |-]#: days [outside | inside] files accessed
  • - File whose mmin [+ |-]#: minutes [outside | inside] content has been modified
  • - cmin [+ |-] #: minutes [outside | inside] changed files
  • - Files accessed by amin [+ |-]#: minutes [outside | inside]
    Note: For the difference between ctime and mtime, stamp here: The difference between ctime and mtime
  • Perm-MODE: Search by MODE permissions, each must contain
  • Perm/MODE: Find by MODE permission, any one of them can be included.
  • perm MODE: Find by MODE permission, must match files, each must be the same as MODE

Processing actions:

  • - print:
  • - ls: Display details
  • - ok COMMAND {};: Operation requires user confirmation
  • - exec COMMAND {}:: Operation without user confirmation

Combination conditions:

  • -a: and
  • -o: or
  • -not: non

Note: Default and operation

Give an example:

1. Find out / etc / all files named passwd and files starting with ifcfg:

[root@DesktopComputer /tmp/test 13:31:31 ]$ find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@DesktopComputer /tmp/test 13:31:50 ]$find /etc/ -name ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-Profile_1
/etc/sysconfig/network-scripts/ifcfg-enp3s0
[root@DesktopComputer /tmp/test 13:34:48 ]$ 

2. Find out / bin / lower, z-terminated files

[root@DesktopComputer /tmp/test 13:50:08 ]$ find /bin/ -regex '.*z$' 
/bin/unxz
/bin/xz
/bin/lz
/bin/dwz
/bin/tgz
/bin/uz
/bin/rz
/bin/sz
[root@DesktopComputer /tmp/test 13:50:45 ]$ 

3. Find out that there are no master or group files in the system.

[root@DesktopComputer /tmp/test 13:53:45 ]$ find / -nouser -o -nogroup
[root@DesktopComputer /tmp/test 13:55:38 ]$ 

Note: The waiting time is long. If you find it, please delete the useless files.

[root@DesktopComputer /tmp/test 13:53:45 ]$ find / -nouser -o -nogroup -exec rm {} \;
[root@DesktopComputer /tmp/test 13:55:38 ]$ 

4. Find links with read and write permissions under / etc/

[root@DesktopComputer /tmp/test 14:17:14 ]$ find /etc/ -type l -a -perm -666
/etc/mtab
/etc/fonts/conf.d/65-0-lohit-telugu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-wqy-microhei.conf
/etc/fonts/conf.d/59-liberation-mono.conf
/etc/fonts/conf.d/65-0-lohit-marathi.conf
...

Note: All link files are 777, which can be verified by the following way:

[root@DesktopComputer /tmp/test 14:17:24 ]$ find /etc/ -type l -ls
134320324    0 lrwxrwxrwx   1 root     root           17 Oct 17 18:54 /etc/mtab -> /proc/self/mounts
2835461    0 lrwxrwxrwx   1 root     root           55 Oct 17 19:15 /etc/fonts/conf.d/65-0-lohit-telugu.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-telugu.conf
495591    0 lrwxrwxrwx   1 root     root           56 Oct 17 18:56 /etc/fonts/conf.d/59-liberation-sans.conf -> /usr/share/fontconfig/conf.avail/59-liberation-sans.conf
2835462    0 lrwxrwxrwx   1 root     root           53 Oct 17 19:15 /etc/fonts/conf.d/65-wqy-microhei.conf -> /usr/share/fontconfig/conf.avail/65-wqy-microhei.conf
547486    0 lrwxrwxrwx   1 root     root           56 Oct 17 18:57 /etc/fonts/conf.d/59-liberation-mono.conf -> /usr/share/fontconfig/conf.avail/59-liberation-mono.conf
2835467    0 lrwxrwxrwx   1 root     root           56 Oct 17 19:15 /etc/fonts/conf.d/65-0-lohit-marathi.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-marathi.conf
553660    0 lrwxrwxrwx   1 root     root           59 Dec 19 07:07 /etc/fonts/conf.d/10-scale-bitmap-fonts.conf -> /usr/share/fontconfig/conf.avail/
...

5. Find files that are modified within one day in your home directory

[root@DesktopComputer /tmp/test 14:34:32 ]$ find /root/ -ctime -1
/root/
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.config/gconf
/root/.xauthqWsH0f
[root@DesktopComputer /tmp/test 14:34:37 ]$ 

6. Find files with 777 permissions in your home directory

[zhoupan@DesktopComputer ~ 14:38:00 ]$ find ~ -perm 777
/home/zhoupan/.config/google-chrome/SingletonLock
/home/zhoupan/.config/google-chrome/SingletonSocket
/home/zhoupan/.config/google-chrome/SingletonCookie
/home/zhoupan/.config/menus/gnome-applications-merged
...

7. Find the file named passwd under / etc / and display its contents

[root@DesktopComputer /tmp/test 15:51:32 ]$ find /etc/ -name passwd -exec cat {} \;
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

8. Find the file and delete it. When deleting it, the user needs to confirm whether deletion is necessary.

[zhoupan@DesktopComputer /tmp/test 16:27:25 ]$ ls
11.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt
[zhoupan@DesktopComputer /tmp/test 16:27:26 ]$ find -name '1*' -ok rm {} \;
< rm ... ./1.txt > ? n
< rm ... ./11.txt > ? y
[zhoupan@DesktopComputer /tmp/test 16:27:52 ]$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt
[zhoupan@DesktopComputer /tmp/test 16:27:53 ]$ 

Posted by Davy on Fri, 05 Apr 2019 23:03:31 -0700