The Linux search command is one of the most important and frequently used command-line utilities in Unix-like operating systems. The find command is used to find and locate the list of files and directories based on the conditions specified for the files that match the arguments.
The Find command can be used in a variety of conditions, such as you can search for files by permissions, users, groups, file types, date, size, and other possible criteria.
[ You May Also Like: 5 Command-Line Tools to Find Files Quickly
on Linux ] Through this article,
we share our daily experience of Linux search commands and their use in the form of examples
.
In this article, we will show you the 35 examples of Find Commands most commonly used in Linux. We have divided the section into five parts from basic use to advanced use of the find command.
Part
- I: Basic Search Commands to Find Files with Names
- Based on Owners and Groups
- Files and Directories by Size
- Part VI: Search for Multiple Filenames in Linux
Part II: Search for Files Based on Their Permissions Part III: Search for Files
Part IV: Search for Files and Directories by Date and Time Part V: Search for
1. Search for files using
the name in the current directory
Find all files named tecmint.txt in a current working directory. # find . -name tecmint.txt ./tecmint.txt
2. Find
files in the home directory Locate all files in the /home directory with the name tecmint.txt. # find /home -name tecmint.txt /
home/tecmint.txt
3. Search for files using Name and ignoring
case
Find all files named tecmint.txt and contain uppercase and lowercase letters in the /home directory. # find /home -iname tecmint.txt ./tecmint.txt ./tecmint.txt
4. Search
for directories using the name
Look for all directories named Tecmint in /directory. # find
/ –type d -name Tecmint /Tecmint
5. Find PHP files using the
name
Find all php files named tecmint.php in a current working directory. # find . -type f -name tecmint.php ./tecmint.php
6.
Find all PHP files in the directory
Find all php files in one directory
. # find . –type f -name “*.php” ./tecmint.php ./login.php ./index.php
7.
Find files with 777 permissions
Find all files whose permissions are 777. # find . -type f -perm 0777 –
8.
Search for files without permission 777
Find all files without permission 777.
# find / -type f ! -perm 777
9.
Find SGID files with 644 permissions
Find all SGID bit files whose permissions are set to 644. # Search / -Perm 2644
10.
Find sticky bit files with 551 permissions Find
all sticky bit set files whose permission is 551. # Search / -Perm 1551 11. Search
SUID files
Find all SUID set files
.
# find / -perm /u=s
12. Search
SGID files
Finds all SSID set files
. # find / -perm /g=s
[ You may also be interested in: How to find files with SUID and SGID permissions on Linux ]
13. Find
read-only files
Search for all read-only files
. # find / -perm /u=r
14. Find
executable files
Search for all executable files
. # find / -perm /a=x
15. Find files with
777 and chmod to 644 permissions
Locate all 777 permission files and use the chmod command to set permissions to 644. # find / -type f -perm 0777 -print -exec chmod 644
{} \;
16. Find
directories with permissions 777 and chmod to 755 Locate the 777
permission directories and use the chmod command to set permissions to 755. # find / -type d -perm 777 -print -exec chmod 755
{} \; 17
. Find
and delete a single file
To find a single file named tecmint.txt and delete it. # find . -type f -name “tecmint.txt“
–exec rm -f {} \;
18. Find
and delete multiple files
To find and delete multiple files, such as .mp3 or .txt, use them. # search .
-type f -name “*.txt” -exec rm -f {} \; Or #find . -type f -name “*.mp3″ -exec rm -f {} \;
[ You may also be interested: 4 useful tools to find and remove duplicate files in Linux ]
19. Find
all empty files
To find all empty files in a given path
. # find /tmp -type f -empty
20. Find
all empty directories
To archive all empty directories under a given path
. # find /tmp -type d -empty
21. Archive
all hidden files
To find all hidden files, use the following command
. # find /tmp -type f -name “.*”
22. Find a single user-based file
To find all or individual files named tecmint.txt in /root directory of the owner’s root. # find / -user root -name tecmint.txt
23. Search
all files by user
To find all files belonging to the user Tecmint in the
/home directory. # find /home -user tecmint
24. Find
all files by group
To find all files that belong to the Developer group in the /home directory
. # find /home -group developer
25. Search for particular
user files
To find all .txt files of the Tecmint user in the /home directory
. # find /home -user tecmint -iname “*.txt”
26. Search for the last
50 days modified files
to find all files that were modified
50 days ago. # find / -mtime 50
27. Search the last
50 days accessed
Files to find all files that were accessed 50 days ago
. # find / -atime 50
28. Search for the last
50-100 days of modified files
To find all files that are modified more than 50 days ago and less than 100 days. # find / -mtime +50 -mtime –100
29. Search for files modified
in the last
1 hour to find all files that have been changed in the last 1 hour
. # find / -cmin -60 30
. Search for
files modified in the last 1 hour
To find all files that were modified in the last 1 hour
. # find / -mmin -60
31. Search for
files accessed in the last
1 hour To find all files accessed in the last 1 hour
. # find / -amin -60
32. Search
for 50MB files
To find all 50MB files, use
. # find / -size 50M
33. Search size between
50MB – 100MB
to find all files that are larger than 50MB and smaller than 100MB. # find / -size +50M -size –100M
34. Find
and delete 100 MB files
to find all 100 MB files and delete them with a single command
. # find / -type f -size +100M -exec rm -f {} \;
35
. Find specific files and delete
Find all .mp3 files larger than 10 MB and delete them with a single command.
# find / -type f -name *.mp3 -size +10M -exec rm {} \;
[ You may also be interested in: How to find a specific string or word in files and directories ] That’s
it, we’re finishing this post here, In our next article, we’ll discuss more other Linux commands in depth with practical examples. Let us know your thoughts on this article using our comments section.