Linux "find" command - A Complete Guide
Linux users cannot just rely on GUI to perform various tasks in their system. Rather they need to have a good knowledge of the various commands available.
One of the very useful commands for any Linux user is the find command. This command is used to locate files in one or more directories.
Using this command, Linux users can set a specific search area, filter or find the files on the specific area or directory, and perform actions on the files that match the search.
Users can search from several criteria like the name of the file, the date in which the file was created, owner and permissions, the time range in which the file got created, or even the modified date.
Syntax of find command
Let us see the syntax of the find command below.
find [path] [options] [expression]
The attributes in the find command are explained below.
- [path] - The directory when we begin the searching.
- [options] - The criteria like searching a file by its name or date or time range.,etc
- [expression] - The actions which perform the file.
The above attributes are optional and they can be used according to the user’s need.
Examples of Linux find command
Here are some basic examples to learn bout the usage of find command in Linux.
- Find the file in the same directory
- Find file under home directory
- Find files using the name ignoring the case
- Find directories using the name
- Find PHP file using the name
- Find all PHP files in the directory
- Find a specific file based on the user
- Find all files based on the user
- Find all the files based on the group
- Find particular files of the user
- Find the last 30 days modified files
- Find the last 30 days accessed files
- Find modified files within the specific days
- Find the changed files in the last 1 hour
- Find modified files in the last 1 hour
- Find accessed files in the last 1 hour
- Find files with/without 777 permissions
- Find files with 777 permissions and chmod to 644
- Find files with 777 permissions and chmod to 755
- Find read-only files
- Find executable file
- Find all the hidden files
- Find all the empty files
- Find all empty directories
- Find SUID files
- Find SGID files
- Find SGID files with 644 permissions
- Find sticky bit files with 551 permissions
- Find and remove a single file
- Find and remove multiple files
- Find files based on size
- To find specific files and delete them
1. Find the file in the same directory
The following command will help us in finding the file named test.txt in the current working directory.
find . -name test.txt
2. Find file under home directory
The file with the name test.txt which is under the home directory can be found using the following command.
find /home -name test.txt
3. Find files using the name ignoring the case
The following command gets us the files with the name test.txt ignoring the case, i.e. whether it is uppercase or lowercase, our command gets all the files under the name test.txt under the home directory.
find /home -iname test.txt
4. Find directories using the name
This command will help us find the directories named Test under the / directory.
find / -type d -name Test
5. Find PHP file using the name
To find the php files with the name test.php use the following command.
find . -type f -name test.php
6. Find all PHP files in the directory
To display all the PHP files in a directory the following command is used.
find . -type f -name "*.php"
7. Find a specific file based on the user
To find a file named test.txt under the root directory / of the owner root, this command is used.
find / -user root -name test.txt
8. Find all files based on the user
The previous command displayed a single file from the root directory. The below command will find all the files that /home
belong to user userName
say for example, ubuntu, under the /home
directory.
find /home -user userName
9. Find all the files based on the group
To find all the files that belong to the group Devops
under the directory
find /home -group groupName
10. Find particular files of the user
If we want to find the particular type of files that belongs to the user, say that we need to find all the .txt files
of user ubuntu
under the /home
directory.
find /home -user userName -iname "*.txt"
# Example
find /home -user ubuntu -iname "*.txt"
11. Find the last 30 days modified files
We can also find the files which are modified 30 days back using the command,
find / -mtime 30
12. Find the last 30 days accessed files
Similar to finding the modified files, we can find the accessed files from the last 30 days.
find / -atime 30
13. Find modified files within the specific days
Let’s say that we need to find the modified files which are more than 20 days and less than 50 days, type the following command.
find / -mtime +20 -mtime -50
14. Find the changed files in the last 1 hour
We need to enter the hour in minutes, like 60 minutes for 1 hour like in the following command.
find / -cmin -60
15. Find modified files in the last 1 hour
The files which are modified in the last 1 hour can be found using the following command
find / -mmin -60
16. Find accessed files in the last 1 hour
The files which are accessed in the last 1 hour can be found using the following command
find / -amin -60
17. Find files with/without 777 permissions
To find the files with 777 permissions, the following command is used.
find . -type f -perm 0777 -print
To find the files with 777 permissions, the following command is used.
find / -type f ! -perm 777
18. Find files with 777 permissions and chmod to 644
To find the files with permissions 777 and set their permissions to 644 using the chmod command.
find / -type f -perm 0777 print -exec chmod 644 {} \;
19. Find files with 777 permissions and chmod to 755
To find the files with permissions 777 and set their permissions to 755 using the chmod command.
find / -type d -perm 777 print -exec chmod 755 {} \;
20. Find read-only files
To find the read-only files, the following command is used.
find / -perm /u=r
21. Find executable file
This command finds all the executable files
find / -perm /a=x
22. Find all the hidden files
This command is enough to find all the hidden files.
find /tmp -type f -name ".*"
23. Find all the empty file
To find all the empty files existing in a certain path, the following command is used
find /tmp -type f -empty
24. Find all empty directories
To find all the empty directories existing in a certain path, the following command is used
find /tmp -type d -empty
25. Find SUID files
The following command finds all the SUID set files.
find / -perm /u=s
26. Find SGID files
The following command finds all the SGID set files.
find / -perm /g=s
27. Find SGID files with 644 permissions
To find all the SGID files whose permissions are set to 644, then the following command is needed.
find / -perm 2644
28. Find sticky bit files with 551 permissions
To find all the sticky bit files whose permissions are set to 551, the following command is used.
find / -perm 1551
29. Find and remove a single file
To find the file named “test.txt” and remove it, use the following command
find . -type f -name "test.txt" -exec rm -f {} \;
30. Find and remove multiple files
To find and remove multiple files with different types like .txt and .pdf, use the commands below.
find . -type f -name "*.txt" -exec rm -f {} \;
The above command removes all the files with the .txt extension.
find . -type f -name "*.pdf" -exec rm -f {} \;
The above command removes all the files with the .pdf extension.
31. Find files based on size
If we need files based on size, say files with 30MB, then use the following command
find / -size 30M
If we need to find files within the specified range, say from 30-50MB, then the command looks like this.
find / -size +30M -size -50M
To find and delete files with a specific size say 50MB, then the command looks like this
find / -type f -size +50M -exec rm -f {} \;
32. To find specific files and delete them
For example, if we need to find all the .txt files with more than 10MB and delete them, use the command as follows.
find / -type f -name *.txt -size +10M -exec rm {} \;
Conclusion
In this article, we learned about the syntax of the find command along with various examples of how to make the most of the find command in Linux. As seen in the above examples, the find command can be used to filter and search the files and folders with various options and criteria.
Being a Linux user, having good knowledge of the find command will be very helpful as we depend on the terminal for most of the tasks rather than the GUI.
If you want to know more about the other useful commands in Linux click here.
Monitor Your Entire Application with Atatus
Atatus is a Full Stack Observability Platform that lets you review problems as if they happened in your application. Instead of guessing why errors happen or asking users for screenshots and log dumps, Atatus lets you replay the session to quickly understand what went wrong.
We offer Application Performance Monitoring, Real User Monitoring, Server Monitoring, Logs Monitoring, Synthetic Monitoring, Uptime Monitoring and API Analytics. It works perfectly with any application, regardless of framework, and has plugins.
Atatus can be beneficial to your business, which provides a comprehensive view of your application, including how it works, where performance bottlenecks exist, which users are most impacted, and which errors break your code for your frontend, backend, and infrastructure.
If you are not yet a Atatus customer, you can sign up for a 14-day free trial .