Find command is used for searching files in a directory, also we can use it for some other purpose by adding additional options and parameters

Installing find software/package

For ubuntu, debian based users

apt-get install findutils

Here I’m explaining some useful find command with practical examples

1. Find the specified file in and under the specified directory. The below command finds “linuxinternetworks.txt” file under the directory “/home/linuxinternetworks”

# find /home/linuxinternetworks -type f -name linuxinternetworks.txt
/home/linuxinternetworks/linuxinternetworks.txt

where “-type f” refers regular file, “-name” refers filename (case sensitive)

2. Find the specified file (case insensitive) in and under the specified directory. The below command finds “linuxinternetworks.txt” file under the directory “/home/linuxinternetworks”

#find /home/linuxinternetworks -type f -iname linuxinternetworks.txt
/home/linuxinternetworks/linuxinternetworks.txt
/home/linuxinternetworks/LINUXINTERNETWORKS.txt

where -iname refers filename (case insensitive)

3. Find files in present working directory

#find $pwd -type f -iname linuxinternetworks.txt
./linuxinternetworks.txt
./LINUXINTERNETWORKS.txt

you can also use “.” instead of “$pwd”

#find . -type f -iname linuxinternetworks.txt
./linuxinternetworks.txt
./LINUXINTERNETWORKS.txt

For information: The directory “/home/linuxinternetworks/” contains following files

# ls -l
total 116
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir1
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir2
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir3
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir4
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir5
-rw-r–r– 1 root root 0 Jan 3 20:19 lin1.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin2.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin3.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin4.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin5.txt
-rw-r–r– 1 root root 45442 Jan 3 20:19 linuxinternetworks.txt
-rw-r–r– 1 root root 45442 Jan 3 20:24 LINUXINTERNETWORKS.txt

4. Find regular files in and under the directory

# find /home/linuxinternetworks/ -type f
/home/linuxinternetworks/lin4.txt
/home/linuxinternetworks/lin1.txt
/home/linuxinternetworks/dir4/lin4.txt
/home/linuxinternetworks/lin5.txt
/home/linuxinternetworks/dir5/lin5.txt
/home/linuxinternetworks/linuxinternetworks.txt
/home/linuxinternetworks/lin3.txt
/home/linuxinternetworks/lin2.txt
/home/linuxinternetworks/dir2/lin2.txt
/home/linuxinternetworks/dir3/lin3.txt
/home/linuxinternetworks/LINUXINTERNETWORKS.txt
/home/linuxinternetworks/dir1/lin1.txt

5. Find directories in and under the directory

# find /home/linuxinternetworks/ -type d
/home/linuxinternetworks/
/home/linuxinternetworks/dir4
/home/linuxinternetworks/dir5
/home/linuxinternetworks/dir2
/home/linuxinternetworks/dir3
/home/linuxinternetworks/dir1

6. Find character files under the directory “/dev”

# find /dev/ -type c
/dev/hidraw0
/dev/sg2
/dev/ttyUSB4
/dev/ttyUSB3
/dev/ttyUSB2
/dev/ttyUSB1
/dev/ttyUSB0
……
……
……

7. Find socket files under the directory “/dev”

# find /dev/ -type s
/dev/log

8. Find block files under the directory “/dev”

# find /dev/ -type b
/dev/sdb
/dev/sr0
/dev/sda11
/dev/sda10
/dev/sda9
/dev/sda8
/dev/sda7
/dev/sda6
/dev/sda5
/dev/sda3
/dev/sda2
/dev/sda1
….
….
….

9. Execute a command after finding a file or directories

# find / -type f -user syslog -exec ls -l {} \;
-rw-r—– 1 syslog adm 24574 Dec 5 22:47 /var/log/syslog.7.gz
-rw-r—– 1 syslog adm 53956 Dec 8 17:17 /var/log/syslog.6.gz
-rw-r—– 1 syslog adm 505 Dec 28 19:00 /var/log/mail.log.2.gz
-rw-r—– 1 syslog adm 512175 Dec 31 22:11 /var/log/kern.log.1
-rw-r—– 1 syslog adm 65532 Dec 11 21:48 /var/log/syslog.5.gz
-rw-r—– 1 syslog adm 1494 Jan 3 19:06 /var/log/mail.log
-rw-r—– 1 syslog adm 519 Dec 8 16:58 /var/log/mail.log.3.gz
-rw-r—– 1 syslog adm 56587 Dec 31 22:17 /var/log/auth.log.1
-rw-r—– 1 syslog adm 2807 Dec 2 20:17 /var/log/auth.log.4.gz
-rw-r—– 1 syslog adm 135793 Dec 2 20:13 /var/log/kern.log.4.gz
-rw-r—– 1 syslog adm 3160 Dec 8 17:17 /var/log/auth.log.3.gz

where “-exec ” option is used to execute a command after find completes

10. Find file name based on last access days. Here below command finds the files access 4 days ago

# date
Fri Jan 3 21:09:09 IST 2014
# find / -type f -used 2 -exec ls -l {} \;
find: `/home/sathishkumar/.gvfs': Permission denied
-rw-rw-r– 1 sathishkumar sathishkumar 1128 Dec 29 22:32 /home/sathishkumar/.cache/ubuntuone/log/syncdaemon-exceptions.log.2013-12-29_22-32-12
-rw-rw-r– 1 sathishkumar sathishkumar 4544 Dec 28 21:53 /home/sathishkumar/.cache/ubuntuone/log/credentials.log.2013-12-28_21-53-35
-rw-rw-r– 1 sathishkumar sathishkumar 4167 Dec 29 22:47 /home/sathishkumar/.cache/ubuntuone/log/syncdaemon.log.2013-12-29_22-47-11
-rw-rw-r– 1 sathishkumar sathishkumar 14199 Dec 29 19:32 /home/sathishkumar/.cache/ubuntuone/log/syncdaemon.log.2013-12-29_19-32-44
-rw-rw-r– 1 sathishkumar sathishkumar 2272 Dec 11 21:12 /home/sathishkumar/.cache/ubuntuone/log/credentials.log.2013-12-11_21-12-34

11. Find files based on username. Here my username is “postfix”

# find / -type f -user postfix -exec ls -l {} \;
-rw——- 1 postfix postfix 33 Jan 3 19:05 /var/lib/postfix/master.lock
-rw——- 1 postfix postfix 8192 Dec 29 18:14 /var/lib/postfix/smtp_scache.db
-rw——- 1 postfix postfix 1024 Dec 29 19:32 /var/lib/postfix/prng_exch
-rw——- 1 postfix postfix 8192 Dec 29 18:14 /var/lib/postfix/smtpd_scache.db

12. Find all the symbolic link file in a directory

#find /proc/ -xtype c -exec ls -l {} \;
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/task/1/fd/0 -> /dev/null
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/task/1/fd/1 -> /dev/null
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/task/1/fd/2 -> /dev/null
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/task/1/fd/15 -> /dev/ptmx
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/task/1/fd/22 -> /dev/ptmx
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/fd/0 -> /dev/null
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/fd/1 -> /dev/null
lrwx—— 1 root root 64 Jan 3 20:45 /proc/1/fd/2 -> /dev/null

13. Find all “.txt” files in a directory

# find -type f -name “*.txt”
./lin4.txt
./lin1.txt
./dir4/lin4.txt
./lin5.txt
./dir5/lin5.txt
./linuxinternetworks.txt
./lin3.txt
./lin2.txt
./dir2/lin2.txt
./dir3/lin3.txt
./LINUXINTERNETWORKS.txt
./dir1/lin1.txt

14. In this command, I’m limiting the directory search by using “maxdepth” option

#find /proc -maxdepth 4 -name cpuset
/proc/14976/task/14976/cpuset
/proc/14976/cpuset
/proc/15885/task/15885/cpuset
/proc/15885/cpuset
/proc/15899/task/15899/cpuset
/proc/15899/cpuset
/proc/15911/task/15911/cpuset
/proc/15911/cpuset

Here “-maxdepth 4″ searches the content in the nested 4 directories under /proc. Descend at most levels (a non-negative integer) levels of directories below the command line arguments. “-maxdepth 0″ means only apply the tests and actions to the command line arguments.

15. Find all files having 0755 permission

#find /home/linuxinternetworks -type f -perm 0755 -exec ls -l {} \;
-rwxr-xr-x 1 root root 0 Jan 3 20:19 /home/linuxinternetworks/lin1.txt
-rwxr-xr-x 1 root root 0 Jan 3 20:19 /home/linuxinternetworks/lin3.txt

16. Find all files having 0644 permission

# find /home/linuxinternetworks -type f -perm 0644 -exec ls -l {} \;
-rw-r–r– 1 root root 0 Jan 3 20:19 /home/linuxinternetworks/lin4.txt
-rw-r–r– 1 root root 0 Jan 3 20:40 /home/linuxinternetworks/dir4/lin4.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 /home/linuxinternetworks/lin5.txt
-rw-r–r– 1 root root 0 Jan 3 20:40 /home/linuxinternetworks/dir5/lin5.txt

17. Find all directories having 0755 permission

# find /home/linuxinternetworks -type d -perm 0755
/home/linuxinternetworks
/home/linuxinternetworks/dir4
/home/linuxinternetworks/dir5
/home/linuxinternetworks/dir3

18. Find example with AND operator

# ls -l
total 116
drwxr-xr– 2 root root 4096 Jan 3 20:40 dir1
drwxr-xr– 2 root root 4096 Jan 3 20:40 dir2
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir3
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir4
drwxr-xr-x 2 root root 4096 Jan 3 20:40 dir5
-rwxr-xr-x 1 root root 0 Jan 3 20:19 lin1.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin2.txt
-rwxr-xr-x 1 root root 0 Jan 3 20:19 lin3.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin4.txt
-rw-r–r– 1 root root 0 Jan 3 20:19 lin5.txt
-rw-r–r– 1 root root 45442 Jan 3 20:19 linuxinternetworks.txt
-rw-r–r– 1 root root 45442 Jan 3 20:24 LINUXINTERNETWORKS.txt

# find . -type f -name “lin*.txt” -and -name “linux*.txt” -exec ls -l {} \;
-rw-r–r– 1 root root 45442 Jan 3 20:19 ./linuxinternetworks.txt

19. Find example with OR operator

# find . -name “lin*.txt” -or -name “dir*”;
./lin4.txt
./lin1.txt
./dir4
./dir4/lin4.txt
./lin5.txt
./dir5
./dir5/lin5.txt
./linuxinternetworks.txt
./lin3.txt
./lin2.txt
./dir2
./dir2/lin2.txt
./dir3
./dir3/lin3.txt
./dir1
./dir1/lin1.txt

20. Find example with NOT operator

# find . -not -name “lin*”;
.
./dir4
./dir5
./dir2
./dir3
./LINUXINTERNETWORKS.txt
./dir1


Subscribe Our Free Newsletter

Delivered by FeedBurner. We Respect Your Privacy

Tagged with:
 

Leave a Reply

%d bloggers like this: