Find Command And Its Usage
The find command is used for searching for files and directories in the Linux command line. Find is one of the most powerful and frequently used commands.
It is impossible for a sysadmin or software developer to avoid the find command while working in the command line. Instead of being afraid of it, you should embrace its power.
Syntax
find [directory to search] [options] [expression]
Few use-cases available for find command :
How to find files and directories by name ?
Answer : find /home/ubuntu -type f -name "hostname.txt"
find /home/ubuntu -type d -name "hostname"
-f looks out for files with specified filename . -d looks out for directories with specified directory name . If either of these two arent mentioned it will filter out both files and directories within the mentioned location .
How to do a case insensitive search ?
Answer : find .-type d -iname mystuff
How to find file by their particular extension ?
Answer : find . -type f -name "*.txt"
How to search for multiple files with multiple extensions ?
Answer : find . -type f -name "*.cpp" -o -type f -name "*.txt"
How to look for a file in a specific directory ?
Answer : find /home/ubuntu -name mystuff
How to look for a file in multiple directories ?
Answer : find ./location1 /secondlocation -type f -name "pattern"
eg: find /home/ubuntu /home/alexa -type f -name "shutdown.py"
How to find empty files and directories ?
Answer : find /home/ubuntu -empty
find . -empty -type f
find . -empty -type f -name "*.cpp"
How to find big files or small files ? Search based on file size ?
Answer : find . -size 50k
find . -size +1G
find . -size -20c
find / -size +500M -name "*.log"
c
: bytesk
: kilobytesM
: MegabytesG
: Gigabytes
How to find recently modified files ?
You know the concept of mtime, atime and ctime, right?
mtime: last modification time of file
ctime: creation time of the file
atime: last access time of the file
To find all the files modified within 3 days (3*24H), use:
find . -type f -mtime -3
To find all the files created at least 5 days (5*24H) ago, use:
find . -type f -ctime +5
To find all the files that were modified in the last 5 minutes, use:
find . -type f -mmin -5
You can specify upper and lower limits along with the search name. The command below will search for all the .java files that have been modified between last 20 to 30 minutes.
find . -type f -mmin +20 -mmin -30 -name "*.java"
How to find files and directories with specific file permissions ?
Answer : find -perm mode
eg : find . -perm 644
How to findfiles owned by a user ?
Answer : find . -type f -user John
find -type f -user John -name "*.cpp"
How to search for files only in parent directories and not in sub directories ? Dont find recursively ?
Answer : By default, the find command searches recursively in all the subdirectories of your current location. If you don't want that, you can specify the depth of your search to 1. This will restrict the search to only the current directory and excludes any subdirectories.
find . -maxdepth 1 -type f -name "*.txt"
How to exclude a directory from search ?
find . -path "./directory_exclude/*" -prune -o -name SEARCH_NAME
How to use grep with find cmd ?
Answer : Combining the
find
command withgrep
allows you to search for files based on their content.find . -type f -exec grep -l "pattern" {} \;
Subscribe to my newsletter
Read articles from Shraddha Bandhakavi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by