How to Use the Find Command in Linux
How to use the find command to make life easier for all System Administrators.
What is Find?
The use of Find is pervasive I will divide the tutorial into five parts sorting the searches by names, permissions, users, groups, and size.
- Part 1. Basic search by the name of the file /directory.
- Part 2. Search for files based on your Permissions.
- Part 3. Filter searches by users and groups.
- Part 4. Perform search based on Dates.
- Part 5. Search for files and directories based on their size.
I will use some real examples that can happen in your day to day to speed up the search of files and directories.
Basic search by the name of the file /directory
Find all the files called solvetic.txt in the directory that you are.
find . -name solvetic.txt
Find all the files called solvetic.txt in the /home directory
find /home -name solvetic.txt
Find all the files named solvetic.txt inside / home regardless of uppercase or lowercase.
find /home -iname solvetic.txt
Search all directories called solvetic throughout the system.
find / -type d -name solvetic
Search only the php files called solvetic in the current directory.
find . -type f -name solvetic.php
Search all the php files in the current directory
find . -type f -name "*.php"
Now let's go to part 2 that would be looking for permissions.
Search for files based on the Permissions
Search all files in the current directory that have 777 permissions
find . -type f -perm 0777 -print
Search all files on the system that do not have 644 permissions
find / -type f ! -perm 644
Search all the files that SGID of 644
find / -perm 2644
Search all files with Sticky that have permissions 551 throughout the system.
find / -perm 1551
Search for all files that have special user permissions ( SUID )
find / -perm /u=s
Search all files that have Special Groups permissions ( SGID )
find / -perm /g+s
Search all files that have Read Only permission
find / -perm /u=r
Search all files that have executable permissions.
find / -perm /a=x
Search all files with permissions 777 and change them with chmod to 644.
find / -type f -perm 0777 -print -exec chmod 644 {} \;
Search all directories with 775 permissions and change them with chmod to 644
find / -type d -perm 775 -print -exec chmod 644 {} \;
Search the files called solvetic.txt in the current directory and delete them with rm.
find . -type f -name "solvetic.txt" -exec rm -f {} \;
Delete all files within a directory of the same extension
find . -type f -name "*.txt" -exec rm -f {} \;
Search all the blank files within a specific directory.
find /etc/html/www/ -type f -empty
Search for empty directories in a specific path (sub-folders are included).
find /etc -type d -empty
Search all hidden files in a certain directory.
find /tmp -type f -name “.*”
Now we go with the search filter.
Filter searches by Users and Groups
Search all the files and directories called solvetic belonging to the root user.
find / -user root -name solvetic
Search all the files belonging to the user so-and-so in all Home directories of the system.
find /home -user fulanito
Search all the files in the system belonging to the admin group
find / -group admin
Search all the .txt files of the jcarrillo user in the home directory.
find /home -user jcarrillo -iname “*.txt”
Now we go with searches with dates.
Perform search based on Dates
Search for modified files in the last 50 days.
find / -mtime 50
Search all files that were viewed or opened no matter if they were modified or not in the last 50 days.
find / -atime 50
Search the modified files between 50 and 100 days.
find / -mtime +50 -mtime -100
This last command is to see the files that were accessed in the previous hour.
find / -amin -60
Search for files and directories based on their size
Search for files that weigh exactly 50MB.
find / -size 50M
Search files between 50MB and 100MB
find / -size +50MB -size -100M
Search and delete files larger than 100MB (Be careful with this command you can have a hard time).
find / -size +100M -exec rm -rf {} \;
Search all .mp3 files with more than 10MB and delete them.
find / -type f -name *.mp3 -size +10M -exec rm {} \;