The Find command is one of the most commonly used commands in Linux. It saves a lot of time to find specific files and folders.
We can search files and folders with different parameters like permission, name, extension, etc. In this article, we will learn different uses of the find command.
Find command execute in the following manner:
$ find [target directory] [expression determines what to find] [-options] [what to find]
Find command in Linux with examples
1. Find Files in Current Directory by using file name
If you want to search all files contain namestring in the file name. Execute following command:
# find . -name filename.txt ./filename.txt
2. Find Directories Using Name
It will search all directories with name “oixiesoft”
# find / -type d -name oixiesoft ./oixiesoft
3. Find all html Files in Directory
It will search all files with .html extention
# . -type f -name "*.html" ./oixiesoft.html ./indext.html ./sample.html
4. Find Files With Permissions
It will search all files with 644 permission. If want to search files with another permission, just replace 644 with required permission.
# find . -type f -perm 0644 -print
5. Find Files Without 644 Permissions
We can search all files without 644 permission with this command:
# find / -type f ! -perm 644
6. Find empty files and directories
We can find all empty files and directories with this command:
$ find ./ -empty
7. Find and remove single File
You can find and delete the file with name sample.txt with this command:
# find . -type f -name "sample.txt" -exec rm -f {} \;
8. Find and remove Files contain specific extension
If you want to search and remove all files with a specific extension, execute the following command. Just replace .mp3 with your desired extension.
# find . -type f -name "*.mp3" -exec rm -f {} \;
9. Find all Hidden Files
If you want to search all hidden files, execute the following command:
# find / -type f -name ".*" [php] <h2>10. Find Last 30 Days Modified Files</h2> This command find all files modified in last 30 days, [php] # find / -mtime 30