Hello friends. Many users, newcomers to Linux, are curious to use the terminal and it is always good to give them support. But not only them, but other users even being veterans, need to know how to use commands to streamline their tasks. Well, in this post we will teach you How to find files in Linux using the terminal.
The find
command is a command that searches for files and folders, from the command line, in a directory tree. It is used by many users who work in the terminal and can help even the most veteran users of the system.
So, let’s show you some examples of how to use the command to find files in any Linux distribution.
How to find files in Linux
The find
command is part of the GNU Utils so it is available in any Linux distribution you can think of. So like any command, it has a basic syntax which is as follows
find [path] [parameters] [values] [values]
From what we can see, it is simple to use and now we will show some examples
- Find a file of which we know the name and extension:
find / -name name.txt
Replace name
with the name of the file. In this case, a file named name.txt
will be searched for in the whole system.
- Search for files in a particular location.
find /home/angelo/ -name name.txt
- Doing case-insensitive searches
When searching by filename it is common to not remember whether it is upper or lower case. To do this, you can follow the following structure
find /home/angelo -iname name name.txt
Remember that you can modify the names and parameters to fit your criteria.
- Search for directories or folders
To search for directories we must use the type d
parameter, it is as simple as this.
find / -type d -name documents
This will search for all folders named documents
in the whole system. Also, you can change the search directory.
- Search files by extension
In this case, it is also useful to know how to search for files by extension. In this case, the option that will help us is type f
.
find / -type f -name *.sh
Thus, in the above example, all files with the extension .sh
in the system will be searched.
Other examples of the find command
It is also useful to know how to search for files by permissions, to do this use the perm
parameter and the permission you want to search for.
find / -type f -perm 0775
or even, permissions different from the specified one
find / -type f ! -perm 775
- Search for empty files or folders
In this case, we can easily do it with these two commands
find / -type f -empty find / -type d -empty
The first one works for files and the second one for directories or folders.
- Find files according to their modification date
For many sysadmins this can be very useful because we can perform a search according to the time it was modified.
find / -type f -mtime +21
In this case, all files that are older than 21 days since their last modification will be searched.
- Search files according to their size
Also, with the size
option we can further filter our search. For example, we can search for files that weigh more than 10Mb.
find / -type f -size +10M
That is to say, with this we can be more effective.
So, enjoy it.