35 Practical Find Command Examples in Linux

Find Command is one of the powerful Unix / Linux utility used for searching the files in a directory hierarchy from the command line. It can be used to find files based on various search criteria like permissions, user ownership, modification date/time, size etc.
In this post we shall learn to use the find command along with various options that it supports.


Find Command Best Examples


The basic syntax syntax of find command is:
find [pathnames] [conditions]


You have several options for matching criteria:

-atime n : File was accessed n days ago
-mtime n : File was modified n days ago
-size n : File is n in size (a block is 512 bytes)
-type c : Specifies file type: f=text file, d=directory
-fstype : Specifies file system type example nfs
-name : Name of the filen you want to search
-user : Specifies file's owner
-group : Specifies file's group owner
-perm p : File's access mode

Some examples based on the above criteria:

-mtime +7 : Matches files modified more than seven days ago.
-atime -2 : Matches files accessed less than two days ago
-size +100 : Matches files larger than 100 blocks (50KB)
-mmin -60 : find all the files which are modified in last 1 hour
-cmin -60 : find all the files which are changed in last 1 hour


Most Frequently Used Find Command Examples in Linux



Find Files Under Home Directory
Find all the files under /home directory with name foo.txt.
# find /home -name foo.txt
/home/foo.txt


Find Files Using Name in Current Directory
Find all the files whose name is foo.txt in a current working directory.
# find . -name foo.txt
./foo.txt


Find files starting from the root directory
# find / -name passwd
This command will find passwd file under all sub-directories starting from root directory.


Find Files Using Name and Ignoring Case
Find all the files whose name is ora.txt and contains both capital and small letters in /home directory.
# find /home -iname Foo.txt
./Foo.txt
./Foo.txt


Find and remove single File
To find a single file called ora.txt and remove it.
# find . -type f -name "ora.txt" -exec rm -f {} \;


Find and remove Multiple File with same extension
To find and delete all files of a specific extension such as .txt, .gz use.
# find . -type f -name "*.txt" -exec rm -f {} \; 


Find Directories Using Name
Find all directories whose name is foo in / directory.
# find / -type d -name foo
/foo


Find hidden files
Hidden files on linux begin with a period (.).  list all hidden files. This command will list all hidden files in the specific directory like temp etc.
# find /tmp -type f -name ".*"
# find ~ -type f -name ".*"
Note: Use proper directory path.


Finding top 10 largest files in Linux
The following command will display the top 10 biggest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files and size of the files.
# find . -type f -exec ls -s {} \; | sort -n -r | head -10 


Finding the Top 10 Small Files
The following command will display the top 10 smaller file in the current directory and its subdirectory. but the only difference the sort is ascending order.
# find . -type f -exec ls -s {} \; | sort -n -r | head -5 

In the above command, you will see ZERO byte files also ( empty files ). So,  you can use the following command to list the smaller files other than the ZERO byte.
# find . -not -empty -type f -exec ls -s {} \; | sort -n  | head -5 


Finding Files based on Modification and accessed time


Find files modified 20 days back
To find all the files which are modified 20 days back.
# find / -mtime 20


Find files accessed in last 20 days
Find all files that were accessed in the last 20 days.
# find / -atime 20


Find files modified in a range of days
Find all files that were modified between 50 to 100 days ago.
# find / -mtime +50 –mtime -100 


Find Changed Files in Last 1 Hour
To find all the files which are changed in last 1 hour.
# find / -cmin -60


Find Modified Files in Last 1 Hour
To find all the files which are modified in last 1 hour.
# find / -mmin -60 


Find Accessed Files in Last 1 Hour
To find all the files which are accessed in last 1 hour.
# find / -amin -60 


Finding Files based on Size
Using the -size option you can find files by size.


Find files bigger than 100M
# find ~ -size +100M 


Find files smaller than 100M
# find ~ -size -100M 


Find 50MB Files
To find all files matches the exact given size i.e, 50M
# find / -size 50M 


Find Size between 50MB – 100MB
To find all the files which are greater than 50MB and less than 100MB.
# find / -size +50M -size -100M 


Find and Delete 100MB Files
To find all 100MB files and delete them using one single command.
# find / -size +100M -exec rm -rf {} \; 


Find Files and Directories based on type using option -type


Find only the normal files
# find . -type f 


Find all directories
# find . -type d 


Find only the socket files.
# find . -type s 


Find all the hidden files
# find . -type f -name ".*" 


Find all the hidden directories
# find -type d -name ".*" 


Find Specific Files and Delete
Find all .mp3 files with more than 10MB and delete them using one single command.
# find / -type f -name *.mp3 -size +10M -exec rm {} \; 


Find all Empty Directories
To file all empty directories under specific path.
# find /tmp -type d -empty 


Finding Files based on Permissions

Find Files having 777 Permissions
Find all the files whose permissions are 777
# find . -type f -perm 0777 -print 


Find Files Without 777 Permissions
Find all the files without permission 777.
# find / -type f ! -perm 777 


Find SGID Files with 644 Permissions
Find all the SGID bit files whose permissions set to 644.
# find / -perm 2644 


Find Sticky Bit Files with 551 Permissions
Find all the Sticky Bit set files whose permission are 551.
# find / -perm 1551 


Find SUID Files
Find all SUID set files.
# find / -perm /u=s 


Find SGID Files
Find all SGID set files.
# find / -perm /g+s 


Find Read Only Files
Find all Read Only files.
# find / -perm /u=r 


Find Executable Files
Find all Executable files.
# find / -perm /a=x 


Find Files with 777 Permissions and Chmod to 644
Find all 777 permission files and use chmod command to set permissions to 644.
# find / -type f -perm 0777 -print -exec chmod 644 {} \; 


Find Directories with 777 Permissions and Chmod to 755
Find all 777 permission directories and use chmod command to set permissions to 755.
# find / -type d -perm 777 -print -exec chmod 755 {} \;

No comments:

Post a Comment

CONTACT

Name

Email *

Message *