find command in Linux with examples

1. Check the local system version

[root@node ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Second, the help of the find command

[root@node ~]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT
actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.

Third, the basic use of find

1. Search by file name


[root@node ~]# find /etc/ -name  "*.conf" |head
/etc/resolv.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf
/etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf
/etc/fonts/conf.d/57-dejavu-sans.conf
/etc/fonts/conf.d/65-0-ttf-arphic-uming.conf
/etc/fonts/conf.d/10-hinting-slight.conf
/etc/fonts/conf.d/59-liberation-serif.conf

2. List all files in the current directory and its subdirectories

[root@node ~]# find /home/ -type f
/home/admin/.bash_logout
/home/admin/.bash_profile
/home/admin/.bashrc
/home/huawei/.bash_logout
/home/huawei/.bash_profile
/home/huawei/.bashrc
/home/lisi/.bash_logout
/home/lisi/.bash_profile
/home/lisi/.bashrc
/home/lisi/.cache/abrt/lastnotification
/home/lisi/github/lisi/README.md
/home/lisi/github/lisi/file1
/home/lisi/github/lisi/file2
/home/lisi/github/lisi/aa.txt
/home/lisi/github/lisi/docker.yaml
/home/lisi/github/lisi/.git/refs/heads/main
/home/lisi/github/lisi/.git/description
/home/lisi/github/lisi/.git/hooks/applypatch-msg.sample
/home/lisi/github/lisi/.git/hooks/commit-msg.sample
/home/lisi/github/lisi/.git/hooks/post-update.sample
/home/lisi/github/lisi/.git/hooks/pre-applypatch.sample
/home/lisi/github/lisi/.git/hooks/pre-commit.sample
/home/lisi/github/lisi/.git/hooks/pre-push.sample
/home/lisi/github/lisi/.git/hooks/pre-rebase.sample
/home/lisi/github/lisi/.git/hooks/prepare-commit-msg.sample
/home/lisi/github/lisi/.git/hooks/update.sample
/home/lisi/github/lisi/.git/info/exclude
/home/lisi/github/lisi/.git/HEAD
/home/lisi/github/lisi/.git/config
/home/lisi/github/lisi/.git/objects/83/c831f0b085c70509b1fbb0a0131a9a32e691ac
/home/lisi/github/lisi/.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
/home/lisi/github/lisi/.git/objects/b2/cee5da3e56febea01ccf3b2c27901d83fb4a43
/home/lisi/github/lisi/.git/objects/94/d3ea2377148473fbf58b33afa7b0a06e010c22
/home/lisi/github/lisi/.git/index
/home/lisi/github/lisi/.git/COMMIT_EDITMSG
/home/lisi/github/lisi/.git/logs/refs/heads/main
/home/lisi/github/lisi/.git/logs/HEAD
/home/lisi/.gitconfig
/home/lisi/.ssh/id_rsa
/home/lisi/.ssh/id_rsa.pub
/home/lisi/.bash_history
/home/test01/file1
/home/test02/file1

3. Query according to iname

[root@node ~]#  find /etc -iname *.Conf |head
/etc/resolv.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf
/etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf
/etc/fonts/conf.d/57-dejavu-sans.conf
/etc/fonts/conf.d/65-0-ttf-arphic-uming.conf
/etc/fonts/conf.d/10-hinting-slight.conf
/etc/fonts/conf.d/59-liberation-serif.conf

4. Find files with a size of 0 in the file

[root@node ~]# find /home -type f -size 0 -exec ls -l {} \;
-rw-rw-r-- 1 lisi lisi 0 Nov 11 15:15 /home/lisi/github/lisi/file1
-rw-rw-r-- 1 lisi lisi 0 Nov 11 15:15 /home/lisi/github/lisi/file2
-rw-rw-r-- 1 lisi lisi 0 Nov 11 15:15 /home/lisi/github/lisi/aa.txt
-rw-rw-r-- 1 lisi lisi 0 Nov 11 15:15 /home/lisi/github/lisi/docker.yaml
-rw-r--r-- 1 root root 0 Nov 12 23:02 /home/test01/file1
-rw-r--r-- 1 root root 0 Nov 12 23:02 /home/test02/file1

5. Find files by file modification time

—(+n)----------|----------(n)----------|---------- (-n)—
(n+1) before 24H| (n+1) between 24H~n 24H|n within 24H

 -ctime -n -- Find files that have been modified within n*24H from now
 -ctime n -- Find files modified in n*24H before the present (n+1)*24H
 -ctime +n -- Find files modified before now (n+1)*24H
find /data -ctime 30

5. Find files by uid


[root@node ~]# find /home -uid 1000
/home/admin
/home/admin/.mozilla
/home/admin/.mozilla/extensions
/home/admin/.mozilla/plugins
/home/admin/.bash_logout
/home/admin/.bash_profile
/home/admin/.bashrc

Fourth, the basic options of find

-mount, -xdev : Only check files in the same file system as the specified directory, avoid listing files in other file systems
-amin n : has been read within the past n minutes
-anewer file: a file that has been read later than file
-atime n : files that have been read in the past n days
-cmin n : Modified in the last n minutes
-cnewer file: a file newer than file file
-ctime n : files created within the past n days
-mtime n : files modified in the past n days
-empty: empty file -gid n or -group name: gid is n or group name is name
-ipath p, -path p : file whose path name matches p, ipath will ignore case
-name name, -iname name : file whose name matches name. iname ignores case
-size n : The file size is n units, b represents a 512-byte block, c represents the number of characters, k represents kilo bytes, and w is two bytes.
-type c : files whose file type is c.
              d: directory
              c: font device file
              b: block device file
              p: named column
              f: general file
              l: symbolic link
Post a Comment (0)
Previous Post Next Post