Linux find command with example
Shining Future Technology is one of the leading industry-based training institutes in Noida, and it provides the students with the knowledge and hands-on training for the computers and IT-based fields. Started in the year 2021, Shining Future Technology is a private institute that has been creating professionals for big MNC's and providing them with the job-specific workforce. The main corporate office is in Noida, and the courses they offer include IT/Software (Python, AWS, Linux, Shell Scripting, SQL, PlSql, Devops, Jira), animation and Business. They provide a practical approach for all the training modules and a student gains an in-depth knowledge of the subject and also learn complex programming codes and structures more simply. WhatsApp contact number: 7838238895/8909068089/8920228066
The find command is used for locating files and directories based on various criteria such as name, type, size, modification time, permissions, and more.
Below is an example of how to use the find command:
for example, you want to find all the text files in your home directory and its subdirectories.
find /home/yourusername -type f -name "*.txt"
Explanation:
findis the command itself./home/yourusernameis the directory from where the search will begin.-type fspecifies that only regular files should be searched, not directories or other special files.-name "*.txt"specifies that only files ending with.txtshould be returned.
Another example could be finding all files in the current directory modified in the last 24 hours:
find . -type f -mtime -1
Explanation:
.is the starting directory, which in this case is the current directory.-type fspecifies that only regular files should be searched, not directories or other special files.-mtime -1specifies that only files modified within the last 24 hours should be returned. The-1argument means less than one day old.
Here are some commonly used options for the find command with examples:
-name: Search for files with a specific name or pattern.
find /home -name myfile.txt
-type: Search for files of a specific type.
find /home -type f -name "*.txt" # search for text files
find /home -type d -name "docs" # search for directories named "docs"
-mtime: Search for files modified within a specific time frame.
bashCopy codefind /home -type f -mtime -7 # search for files modified within the last 7 days
find /home -type f -mtime +7 # search for files modified more than 7 days ago
-size: Search for files of a specific size.
find /home -type f -size +10M # search for files larger than 10 MB
find /home -type f -size -1G # search for files smaller than 1 GB
-user: Search for files owned by a specific user.
find /home -type f -user john # search for files owned by user "john"
-group: Search for files owned by a specific group.
find /home -type f -group users # search for files owned by the group "users"
-perm: Search for files with specific permissions.
find /home -type f -perm 644 # search for files with permissions set to 644
-exec: Execute a command on the search results.
find /home -type f -name "*.txt" -exec rm {} \; # delete all text files
-print: Print the search results.
find /home -type f -name "*.txt" -print # list all text files
-inum: Search for files with inode number 'N'.
find /home -type f -inum 45578825 #search file using inode number
-links: Search for files with 'N' links.
find /home -type f -links 2 #search for file with 2 links
-empty: Search for empty files and directories.
find /home -empty #search for empty files and directories
-mindepth: Themindepthoption in thefindcommand specifies the minimum depth at which to start the search. By default,findstarts the search at the current directory, but withmindepth, you can specify a higher level directory to start the search. Here's an example of how to use themindepthoption:Suppose you want to search for all text files in the
/homedirectory and its subdirectories, but you want to start the search from a higher level directory, such as the root directory. The command would be:
find / -mindepth 2 -type f -name "*.txt"
maxdepth: Themaxdepthoption in thefindcommand specifies the maximum depth at which to search for files. By default,findsearches for files in all subdirectories, but withmaxdepth, you can limit the depth of the search to a certain level.Here's an example of how to use the
maxdepthoption:Suppose you want to search for all text files in the
/homedirectory and its immediate subdirectories only. The command would be:find /home -maxdepth 2 -type f -name "*.txt"
We can refer to the man page for find for a complete list of options and their descriptions.
These are just a few examples of how to use the find command in Linux. If you have further questons then you can reach out to us on whatsapp numbers: 7838238895/8909068089/8920228066