find Command in Linux with Examples

Find command

Find command is used to search directories and any type of file format through the whole system.
 

Syntax

find <options> <path-to-search>
 

To Find Files Using Name in Current Directory

This command helps to find files using name in current working directory.

[root@linuxhelp test]# find . -name new 
./new

 

To Find Files under Home Directory

The following command is used to find files using name under home directory,

[root@linuxhelp test]# find /home  -name test  
/home/user1/Desktop/test

The above command can be used to find files in a different directory.
 

To Find Directories Using Name

The following command is used to find directories using the name.

[root@linuxhelp test]#find /root/Desktop/ -name LinuxManual 
/root/Desktop/LinuxManual

 

To Find Files Using Name and Ignoring Case

The following command is used to find files using name and ignoring case in a directory.

[root@linuxhelp test]#find /home -iname test.txt
./Test.txt
./test.txt

 

To Find all text Files in Directory

The following command is used to find all text files using name in a current working directory.

[root@linuxhelp test]# find . -type f -name “;*.txt”; 
./1.txt
./Test.txt
./2.txt

 

To Find Files with 755 Permissions

The following command is used to find files in current working directory with 755 permissions.

[root@linuxhelp test]#find . -type f -perm 755 
./1.txt
./2.txt

To Find Files without 755 Permissions

The following command is used to find files in current working directory without 755 permissions.

[root@linuxhelp test]#find . -type f ! -perm 755 
./test.txt
./Test.txt

 

To find SUID set files

The following command is used to finds SUID Files, SGID Files, and Executable Files & Read Only Files.

[root@linuxhelp test]#find . -perm /u=s 
./1.txt
./2.txt

 

To find SGID set files

The following command is used to find SGID set files.

[root@linuxhelp test]#find .  -perm /g=s 
./example.txt
./test.txt
./Test.txt

 

To find read only files

The following command is used to find read only files.

[root@linuxhelp test]# find . -perm  /u=r 
./example
./test.txt
./new
./sample
./1..txt
./Test.txt
./2.txt

 

To find executable files

The following command is used to find executable files.

[root@linuxhelp test]# find . -perm /a=x 
./example
./test.txt
./new
./sample
./1..txt
./Test.txt
./2.txt

 

To Find Files with 755 Permissions and Chmod to 744

The following command is used to find files with 755 permissions and change its permission to 744.

[root@linuxhelp test]# find . -type f -perm 755 -print -exec chmod 744 {} ; 
-rwzr-xr-x.   1 root root         0 Feb  2  00:15 1.txt
-rwzr-xr-x.   1 root root         0 Feb  2  00:15 12.txt
drwxr-sr-x.   2 root root      4096 Feb  1  17:27 example 
drwxr-sr-x.   2 root root      4096 Feb  1  17:27 new
drwxr-sr-x.   2 root root      4096 Feb  1  17:27 sample
-rwzrwSr-x.   1 root root         0 Feb  1  23:41 test.txt
-rwzrwSr-x.   1 root root         0 Feb  1  23:41 Test.txt

 

To Find text Files Using Name

The following command is used to find text files using name in a current working directory.

[root@linuxhelp test]# find . -type f -name test.txt 
./test.txt

 

To find sticky bit files

The following command is used to find sticky bit files.

[root@linuxhelp test]# find . -perm 1661 
./test

 

To Find Multiple directories

The following command is used to find multiple directories at the same time.

[root@linuxhelp Desktop]# find ./test ./new/ -name "abc*" -type d 
./test/abc
./new/abcd

 

To Find and Delete Directories recursively

The following command is used to find and delete directories recursively.

[root@linuxhelp]# find . -type d -name test -exec rm -r {} ; 
Before Deleting:

[root@linuxhelp Desktop]# ll 
total 20
-rw-r--r--. 1 root      root       150 Feb  5  2016 download
drwxr-xr-x. 6 user1 user1 4096 Feb 17  2016 LinuxManual
drwxr-xr-x. 2 root      root      4096 Feb  2 00:00 test


After Deleting:

[root@linuxhelp Desktop]# ll 
total 16
-rw-r--r--. 1 root      root       150 Feb  5  2016 download
drwxr-xr-x. 6 user1 user1 4096 Feb 17  2016 LinuxManual

 

To Find and Copy Files

The following command is used to find and copy files to a different location.

[root@linuxhelp]# find . -type f -name "*.txt" -exec cp {} /root/Desktop/txt/ ;  

 

Before Copying:

[root@linuxhelp txt]# ll 
total 0


After Copying:

[root@linuxhelp txt]# ll 
total 0
-rw-r--r--. 1 root root 0 Feb  2 00:31 test.txt


To Find and copy one file too many directories

The following command is used to find and copy one file to many directories.

[root@linuxhelp]# find 1 2 3 -type d -exec cp test.txt {} ; 


Before Copying:

[root@linuxhelp 1]# ll 
total 0
[root@linuxhelp 2]# ll 
total 0
[root@linuxhelp 3]# ll 
total 0


After Copying:

[root@linuxhelp 1]# ll 
total 0
-rw-r--r--. 1 root root 0 Feb  2 00:35 test.txt
[root@linuxhelp 2]# ll
total 0
-rw-r--r--. 1 root root 0 Feb  2 00:35 test.txt
[root@linuxhelp 3]# ll 
total 0
-rw-r--r--. 1 root root 0 Feb  2 00:35 test.txt

 

To Find files by modification time

The following command is used to find files which are modified before one hour.

[root@linuxhelp]# find / -mtime 1 
/var/cache/PackageKit
/var/cache/PackageKit/groups.sqlite
/var/log/httpd
/var/log/httpd/error_log-20160201
/var/log/httpd/error_log
/var/run/httpd/httpd.pid

 

To find files that is modified before 7 days

The following command is used to find files which are modified before 7 days.

[root@linuxhelp]# find / -mtime -7 -type f 
./2/test.txt
./3/test.txt
./LinuxManual/SSH-Commands.odt
./LinuxManual/filelinks.odt

 

To find directories this is modified before 7 days

The following command is used to find directories which were modified before 7 days.

[root@linuxhelp Desktop]# find . -mtime -7 -type d 
.
./2
./3
./LinuxManual
./LinuxManual/ps_commands
./1
./new
./new/abcd
./txt
./txt/2
./txt/3
./txt/1

 

To Find and Tar

The following command is used to find files and create a tar file of the content.

 

[root@linuxhelp]# find . -type f -name "*.txt" | xargs tar cvf new.tar 
./2/test.txt
./3/test.txt
./1/test.txt
./test.txt
./txt/test.txt
[root@linuxhelp test]# ll
-rw-r--r--. 1 root     root      10240 Feb  2 01:02 new.tar

 

To Invert match

The following command is used to find the files that do not match a given name.

[root@linuxhelp]# find . -not  -name  "*.php" 
./test

 

To Find hidden files

The following command is used to find a list of hidden files.

[root@linuxhelp]# find ~ -type f -name  ".*" 
/LinuxManual/edited/.~lock.find command edited.docx#
./LinuxManual/Completed/.~lock.top_command.odt#
./.~lock.ifconfig#

 

To Find files of a particular user

The following command is used to find the files of a particular user.

[root@linuxhelp]# find . -user  user1 
./abc.txt
./abc

 

To Find all files belonging to a particular group

The following command is used to find all files which belong to a particular group.

[root@linuxhelp Desktop]# find . -group user1 
./LinuxManual
./1

 

To Find files of given size

The following command is used to find all 90MB files.

[root@linuxhelp Desktop]# find . -size 90M 
./app

 

To Find files in a size range

The following command is used to find all the files which are greater than 80MB and less than 100MB.

[root@linuxhelp]# find / -size +80M -size -100M 
./new

 

To Find largest files

The following command is used to find 10 largest files in the current directory and its sub directories.

[root@linuxhelp Desktop]# find . -type f -exec ls -s {} ; | sort -n -r | head -10
91324 ./new 
68 ./LinuxManual/linux file system.odt
48 ./LinuxManual/scp.odt
44 ./LinuxManual/edited/ifconfig.odt
44 ./LinuxManual/cp cmd.odt
44 ./LinuxManual/Completed/Grep command.odt
40 ./LinuxManual/ls command.odt
40 ./LinuxManual/df command.odt
40 ./LinuxManual/Completed/SED command .odt
40 ./LinuxManual/Completed/ping command.odt

 

To Find smallest files

The following find command is used to find 10 smallest files in the current directory and its sub directories.

[root@linuxhelp]# find . -type f  -exec ls  -s  {}  ; | sort -n | head  -10 
0 ./1/test.txt
0 ./2/test.txt
0 ./3/test.txt
0 ./4/test.txt
0 ./ifconfig~
0 ./test.php
0 ./test.txt
0 ./txt/test.txt
4 ./download~
4 ./LinuxManual/Completed/.~lock.top_command.odt#

 

To Find all empty files

The following find command is used to find all empty files in a directory.

[root@linuxhelp Desktop]# find . -type f -empty 
./2/test.txt
./3/test.txt
./4/test.txt
./1/test.txt
./test.txt
./txt/test.txt
./ifconfig~
./test.php

 

To Find all empty directories

The following find command is used to find all the empty directories in particular location.

[root@linuxhelp Des]# find . -type d -empty 
./new1

 

To Find changed files in last 2 hours

The following find command is used to find all the changed files in last two hours.

[root@linuxhelp Desktop]# find . -cmin -120 
./1
./1/test.txt
./myfile.tar
./new
./txt
./txt/test.txt

 

To find accessed files in last 2 hours

The following find command is used to find all the files that are accessed in last 2 hours.

[root@linuxhelp]# find . -amin -120 
/1
./1/test.txt
./myfile.tar
./new
./txt
./txt/test.txt

 

To find modified files in last 2 hours

The following find command is used to find all modified files in last 2 hours.

[root@linuxhelp]# find . -mmin 120 
/1
./1/test.txt
./myfile.tar
./new

 

To Find Files and execute commands

The following find command is used to find files and execute commands with it.

[root@linuxhelp Desktop]# find . -name "*.txt" -exec ls -ld {} ; 
-rw-r--r--. 1 root root 0 Feb  1 23:07 ./test.txt

 

To Find Command with AND operation

The following find command is used to find multiple commands and perform AND operation.

[root@liuxhelp]# find . -name '2' ! -name '*txt' 
./2

 

To Find Command with OR operation

The following command is used to find multiple commands and perform OR operation.

[root@liuxhelp]# find ./test –name ‘;2’; -o -name ‘;*.txt’; 
./2
./2/test.txt
./3/test.txt
./1/test.txt
./test.txt
./test/test.txt
./1.txt
Tag : find command
FAQ
Q
How to find an empty file within the current directory?
A
You can use the following syntax to find an empty file within the current directory. For Ex: "find . -type f -empty".
Q
How to find the file which gets with SUID bit set with permissions 755 the current directory?
A
Execute the following command to find the files with SUID bit set with permissions 755 in the current directory. For Ex: "find / -perm 4755​".
Q
How to find the file which gets certain user's ownership within a particular directory?
A
You can use the following syntax to find the files which get certain user ownership in a particular directory. For Ex: "find / -user username -name test.txt"
Q
How to search the files which were modified in between the two-time ranges, like more than 90 days and less than 180 days using the find command?
A
You can use the following command to search the files which were modified in between the two-time ranges, like more than 90 days and less than 180 days using the find command. For Ex: "find /
Q
How to find the newly created files in a certain directory using the find command?
A
You can use the following syntax to find the newly created files in a certain directory using the find command. For Ex: "find / -newer test.txt"