SYNOPSIS

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

Example 1 : the simple one, find a file then print it out

root@ubuntu2004:~# find /etc -name hosts
/etc/avahi/hosts
/etc/hosts

Example 2 : find files using regular expression

root@ubuntu2004:~# find /etc -name "*switc*.conf"
/etc/usb_modeswitch.conf
/etc/nsswitch.conf

Example 4 : find files with name case insensitive

root@ubuntu2004:~# find /etc -iname "*SWITC*.conf"
/etc/dbus-1/system.d/net.hadess.SwitcherooControl.conf
/etc/usb_modeswitch.conf
/etc/nsswitch.conf

Example 5: find file’s type is file or directory or symblic link then print

j@ubuntu2004:~$ find tmpdir -type f,d,l
tmpdir
tmpdir/filelink
tmpdir/file
tmpdir/dir1
tmpdir/dir3
tmpdir/dir2
j@ubuntu2004:~$ tree tmpdir/
tmpdir/
├── dir1
├── dir2
├── dir3
├── file
└── filelink -> file

3 directories, 2 files
j@ubuntu2004:~$

Example 6 : find files , exclude 2 directories

j@ubuntu2004:~$ tree tmpdir
tmpdir
├── dir1
│   └── file1
├── dir2
│   └── file2
└── dir3
    └── file3

3 directories, 3 files
j@ubuntu2004:~$ find tmpdir \( -path "tmpdir/dir1" -o -path "tmpdir/dir2" \) -prune -o -print
tmpdir
tmpdir/dir3
tmpdir/dir3/file3
j@ubuntu2004:~$

Example 7 : find files then delete them

j@ubuntu2004:~$ ls tmpdir/
1.txt  a.txt  dir1  dir2  dir3
j@ubuntu2004:~$ find tmpdir -name "*.txt" -delete
j@ubuntu2004:~$ ls tmpdir/
dir1  dir2  dir3

Example 8: find files ,delete them but ask user first

j@ubuntu2004:~$ ls tmpdir
a.txt  b.txt  dir1  dir2  dir3
j@ubuntu2004:~$ find tmpdir -name "*.txt" -ok rm '{}' \;
< rm ... tmpdir/a.txt > ? y
< rm ... tmpdir/b.txt > ? n
j@ubuntu2004:~$ ls tmpdir
b.txt  dir1  dir2  dir3

Examples 9: find files larger than 2M and sort by size

j@ubuntu2004:~/dir1$ seq 10 | xargs -n1 -I '{}' truncate -s '{}'m file'{}'
j@ubuntu2004:~/dir1$ ls -lrth
total 0
-rw-rw-r-- 1 j j 3.0M Jan 14 08:56 file3
-rw-rw-r-- 1 j j 2.0M Jan 14 08:56 file2
-rw-rw-r-- 1 j j 1.0M Jan 14 08:56 file1
-rw-rw-r-- 1 j j 6.0M Jan 14 08:56 file6
-rw-rw-r-- 1 j j 5.0M Jan 14 08:56 file5
-rw-rw-r-- 1 j j 4.0M Jan 14 08:56 file4
-rw-rw-r-- 1 j j 8.0M Jan 14 08:56 file8
-rw-rw-r-- 1 j j 7.0M Jan 14 08:56 file7
-rw-rw-r-- 1 j j 9.0M Jan 14 08:56 file9
-rw-rw-r-- 1 j j  10M Jan 14 08:56 file10
j@ubuntu2004:~/dir1$ find . -size +2M  -printf "%p \t%s bytes\n"  | sort -k2n
./file3 	3145728 bytes
./file4 	4194304 bytes
./file5 	5242880 bytes
./file6 	6291456 bytes
./file7 	7340032 bytes
./file8 	8388608 bytes
./file9 	9437184 bytes
./file10 	10485760 bytes

Example 10: find files which were modified with 5 minutes

j@ubuntu2004:~/dir1$ pwd
/home/j/dir1
j@ubuntu2004:~/dir1$ echo "Test" >> file8
j@ubuntu2004:~/dir1$ find . -mmin -5
./file8
j@ubuntu2004:~/dir1$
       +n     for greater than n,
       -n     for less than n,
       n      for exactly n.
       -amin n
              File was last accessed n minutes ago.
       -atime n
              File  was  last  accessed n*24 hours ago.  When find figures out how many 24-hour periods ago the file was
              last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed  at
              least two days ago.
       -cmin n
              File's status was last changed n minutes ago.
       -ctime n
              File's status was last changed n*24 hours ago.  See the comments for -atime to understand how rounding af‐
              fects the interpretation of file status change times.
       -mmin n
              File's data was last modified n minutes ago.

       -mtime n
              File's  data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding af‐
              fects the interpretation of file modification times.

Example 11: find files modify time between 10 minutes before and with 15 minutes

j@ubuntu2004:~$ find . -mmin -15  -mmin +10
./.local/share/gnome-shell
./.local/share/gnome-shell/application_state
./.bash_history
j@ubuntu2004:~$

Example 12 : Find files with specifying -maxdepth

j@ubuntu2004:~$ tree tmpdir
tmpdir
├── dir1
│   ├── file1
│   └── level2
│       ├── aaa
│       └── level3
└── dir2
    └── file2

4 directories, 3 files
j@ubuntu2004:~$ find tmpdir -maxdepth 1
tmpdir
tmpdir/dir1
tmpdir/dir2
j@ubuntu2004:~$ find tmpdir -maxdepth 2
tmpdir
tmpdir/dir1
tmpdir/dir1/file1
tmpdir/dir1/level2
tmpdir/dir2
tmpdir/dir2/file2
j@ubuntu2004:~$ find tmpdir -maxdepth 3
tmpdir
tmpdir/dir1
tmpdir/dir1/file1
tmpdir/dir1/level2
tmpdir/dir1/level2/aaa
tmpdir/dir1/level2/level3
tmpdir/dir2
tmpdir/dir2/file2