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_LIKE="rhel fedora"
PRETTY_NAME="CentOS Linux 7 (Core)"


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 or, if you have no web access, by sending
email to <>.

Third, the basic use of find

1. Search by file name

[root@node ~]# find /etc/ -name  "*.conf" |head

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

[root@node ~]# find /home/ -type f

3. Query according to iname

[root@node ~]#  find /etc -iname *.Conf |head

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

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