The find command in linux is a command that helps you find files or directories and perform subsequent operations on them.
find <dir> [-options] [what to find]
Find Files with a specific file name in Linux
Let's look at some examples of how we can find files with specific names
# search for laravel.log file in current dir find . -name laravel.log # search for laravel.log file in /home dir find /home -name laravel.log
Find Files with a specific permission in Linux
Let's look at some examples of how we can find files with specific permissions:
# search for all files with 777 permissions in current dir find . -perm 0777 -print # search for all files with 777 permissions in /home dir find /home -perm 0777 -print
Find files in multiple directories in Linux
Let's look at some examples of how we can find files with specific permissions:
# search for all files starts with failed_ and ends with .log in /var/log and /home dir find ./var/log ./home -type f -name "failed_*.log"
Find Files with time in Linux
Let's look at different examples for files to search using their time:
# find all the files in /home dir which were modified in last two days find /home -mtime -2 # find all the files in /home dir which were changed/modified in last hour find /home -mmin 60 find /home -cmin -60 # find all the files in /home dir which were accessed in last 10 days find /home -atime -10
Find Files with size in Linux
Let's look at different examples for files to search using their file size:
# find files in /home with exact 200 bytes find /home -size 200c -print # find files in /home with size greater then 20 MB find /home -size +20M # find files in /home with size range between 100MB to 200MB find /home -size +100M -size -200M
Followings are some more examples for reference:
Command | Summary |
---|---|
find . -name laravel.log | find laravel.log in current working directory |
find /home -name laravel.log | find laravel.log file in /home directory |
find /home -iname Laravel.log | search laravel.log file ignoring case of the file in /home dir |
find /home -type d -name Logs | search Logs directory in /home directory |
find /home -type f -name "*,log" | search for any file that has log extention in /home dir |
find /home -type f -perm 0777 -print | show all the files in /home dir with permissions 777 |
find /home -type f -perm 0777 -print -exec chmod 644 {} \; | show all the files in /home dir with permissions 777 and then change their file permissions to 644. |
find /home -type f -perm 0777 -print -exec rm -f {} \; | show all the files in /home dir with permissions 777 and then remove those files. |
find /home -type d -perm 0777 -print -exec rm -f {} \; | show all the directories in /home dir with permissions 777 and then remove those directories. |
find /home -type f -name "*.log" -print -exec rm -rf {} \; | search for all the logs file in /home dir and then delete them all |
find /home -type f -empty | search for all empty files in /home directory |
find /home -user sandip | find all the files whose owner is sandip in /home directory |
find /home -mtime -2 | find all the files who was modified in last two days in /home directory |
find /home -mmin 60 | find all the files who were modified in last hour in /home directory |
find /home -atime -10 | find all the files who were accessed in last 10 days in /home dir |
find /home -cmin -60 | find all the files which were changed in last hour in /home dir |
find /home -size 1M | find all the file with size is 1 MB in /home dir |
find /home -size +10M | find all the file with size is more then 10 MB in /home dir |
find ./home -name test.txt -exec rm -i {} \; | find all test.txt files in /home dir and delete them with confirmation |
find /home -type f -name "*.log" -exec grep 'ERROR' {} \; | find all the log files and search for keyword ERROR in them |