Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
The find
command in Linux is a powerful tool used for searching files and directories based on different criteria such as name, size, type, modification date, permissions, and more. It is one of the most frequently used commands by system administrators and DevOps engineers. Hereโs an in-depth look at the top 20 find
command examples with detailed explanations.
1. Find Files by Name
Command:
find /home -name "myfile.txt"
Explanation:
This command searches for a file named myfile.txt
inside the /home
directory and its subdirectories. The -name
option is case-sensitive.
2. Find Files by Case-Insensitive Name
Command:
find /home -iname "myfile.txt"
Explanation:
Similar to the previous command but with the -iname
option, which makes the search case-insensitive. It will find myfile.txt
, MYFILE.TXT
, MyFile.txt
, etc.
3. Find Directories Instead of Files
Command:
find /var -type d -name "logs"
Explanation:
This command searches for directories named logs
inside /var
and its subdirectories. The -type d
option ensures only directories are matched.
4. Find Files by Extension
Command:
find /var/log -type f -name "*.log"
Explanation:
Finds all files inside /var/log
that have a .log
extension. The -type f
ensures that only files are searched.
5. Find Large Files (More than 100MB)
Command:
find / -type f -size +100M
Explanation:
This finds all files larger than 100MB (+100M
) in size across the entire system (/
). The -size
option supports different units:
c
(bytes)k
(kilobytes)M
(megabytes)G
(gigabytes)
6. Find Small Files (Less than 1KB)
Command:
find /home -type f -size -1k
Explanation:
Searches for files smaller than 1KB inside /home
. The -size -1k
means “files less than 1KB.”
7. Find Files Modified in the Last 7 Days
Command:
find /var/www -type f -mtime -7
Explanation:
Finds files inside /var/www
that have been modified in the last 7 days. The -mtime -7
option ensures it fetches files modified within the past week.
8. Find Files Not Modified in the Last 30 Days
Command:
find /backup -type f -mtime +30
Explanation:
Lists all files in /backup
that have not been modified in the last 30 days (+30
means “older than 30 days”).
9. Find Files Accessed in the Last 24 Hours
Command:
find /etc -type f -atime -1
Explanation:
Finds files inside /etc
that were accessed within the last 24 hours (-1
means “within 1 day”).
10. Find and Delete Files Older than 30 Days
Command:
find /tmp -type f -mtime +30 -exec rm {} \;
Explanation:
This searches for files older than 30 days (-mtime +30
) in /tmp
and deletes them using -exec rm {}
.
โ ๏ธ Warning: Be very careful when using rm
with find
as it permanently deletes files.
11. Find Empty Files
Command:
find /home -type f -empty
Explanation:
This finds all empty files (files with 0 bytes) in /home
. The -empty
flag helps in cleaning up useless files.
12. Find Empty Directories
Command:
find /var -type d -empty
Explanation:
Finds empty directories inside /var
.
13. Find Files with Specific Permissions (777 – Full Permissions)
Command:
find / -type f -perm 0777
Explanation:
Lists all files with full permissions (rwxrwxrwx
). These files are considered security risks.
14. Find and Change Permissions of Files
Command:
find /var/www -type f -perm 0777 -exec chmod 644 {} \;
Explanation:
Finds files with 777
permissions in /var/www
and changes them to 644
(rw-r--r--
).
15. Find Files Owned by a Specific User
Command:
find /home -type f -user myuser
Explanation:
Finds all files inside /home
that belong to myuser
.
16. Find Files Owned by a Specific Group
Command:
find /var -type f -group developers
Explanation:
Finds all files in /var
that belong to the developers
group.
17. Find Symbolic Links
Command:
find /usr -type l
Explanation:
Lists all symbolic links (-type l
) in /usr
.
18. Find Files Excluding a Specific Directory
Command:
find / -path "/proc" -prune -o -type f -name "*.conf" -print
Explanation:
Finds all .conf
files but excludes the /proc
directory using -prune
.
19. Find and Copy Files to Another Location
Command:
find /home -type f -name "*.jpg" -exec cp {} /backup/photos/ \;
Explanation:
Finds all .jpg
files inside /home
and copies them to /backup/photos/
.
20. Find and Move Files to Another Directory
Command:
find /downloads -type f -name "*.mp4" -exec mv {} /media/videos/ \;
Explanation:
Finds all .mp4
files inside /downloads
and moves them to /media/videos/
.
Final Thoughts
The find
command is an essential tool in Linux for searching files and directories efficiently. By combining options like -exec
, -prune
, and -type
, you can filter results based on multiple criteria, automate cleanup tasks, and even manage large file systems effectively.
Would you like me to create a downloadable cheatsheet for this? ๐