Every time we use an operating system, it becomes full of files and archives. Whether they are downloaded from the internet or created by the user himself. Then it becomes as complicated as finding them. So in this post, we will help you to find files in Ubuntu. However, this post can help you with any system.
Specifically, Ubuntu and the Unix family has a command called find
that basically looks for files but does it pretty fast. This command comes installed in Ubuntu so we do not have to install anything else.
On the other hand, thanks to the find
command it is possible to find many files efficiently. This is appreciated when our Ubuntu works as a server and has no graphical interface. Also, it is useful if you have a modest computer because with this command it can be faster than using a graphical interface.
The find command
The basic syntax of how to use the find command is as follows
find [directory-to-find] [options] [search-term]
So you have to define a directory from which the search will start. Then there are the search options and finally a search term.
By default, the directory where the search will start will be the current one. Also, you might want to start a system-wide search, in that case, the directory would be /
which is the symbol representing the root directory of the system. On the other hand, if you wanted to start searching in the home directory, you would use ~
as a reference. So, if you do not know the current location of the terminal, you can find out by running the pwd
command.
Finding files in Ubuntu
The most typical search in a system is by the name of the file. So, let’s say I am looking for a file called report.odt
throughout the system.
To search for a file by its name, add the -name option to the command, like this:
:~$ find / -name "report.odt"
The above command will search for exactly the file report.odt
on the system. However, the search should not be case sensitive. In this case, the option is -iname
.
:~$ find / -iname "report.odt"
On the other hand, there are times when we only remember what the file is not called. With the help of the -not
command, we can filter the search.
:~$ find / -not -name "report.odt"
The above command will search the system for all files that are not called report.odt
.
Another useful feature of the find command is that it allows us to search only by extension. For example, we only want to search for JPG images.
:~$ find / -name "*.jpg"
Also, you can search for files containing two names. In that case, you have to add the option -a
:~$ find / -name "report" -a "july"
That is, this command will search for all files whose names contain the words report
and july
.
In the opposite case, it is possible to change the condition and define some words or other as search criteria.
:~$ find / -name "report" -or "july"
The command will now search for files whose names contain the words report
or july
.
Searching for files by type
A very useful option in the find command is the search by type. Also, it should be noted that for many users a search by name is sufficient, but by type, we can further refine the results.
So, to do this we have to use the -type
argument that offers several possibilities:
- f: for common files.
- d: for folders.
- l: symbolic links.
- c: character devices.
- b: block devices.
Moreover, you can combine -type with -name and refine the search further, for example:
:~$ find / -type f -name "report"
In this case, the find
command will only look for files that are called report
but excluding folders and links.
Find files by size
It is also possible to search by file size to add more precision to the search. For this, the -size
command is indicated.
This command also has different size units that we have to specify:
- c: bytes
- k: kilobytes
- M: megabytes
- G: gigabytes
An example of the use of the -size
option is as follows:
:~$ find / -size 100M
This command will search for any file or folder on the system that weighs 100M. It is possible to use + or – operators to define criteria of greater than and less than.
:~$ find / -size +100M
The above command will search for files that weigh more than 100 Megabytes.
Or, conversely, to look for files smaller than 100 Megabytes.
:~$ find / -size -100M
Of course, you can combine this command with -type
and -name
for better results. For example:
:~$ find . -size 2G -type f -name "report"
Find files by date of access or modification
Doing a file search in Ubuntu for the time of modification is another very useful criterion for us. In this case, three commands will help us with the task.
- -atime: the recent date the file was read or written.
- -mtime: this command specifies the most recent date the file was modified.
- -ctime: the most recent date when the file metadata was updated.
The usage is quite similar in all three. You have to add it to the command:
:~$ find / -atime 1
This command will return all files accessed the previous day. And to know the modified files, change -atime
to -mtime
:~$ find / -mtime 1
Also, you can use the + or – operators to define a more precise criterion as in the previous section. You can also combine them with -type
and -name
.
Conclusion
Searching for files in Ubuntu is one of the operations that can complicate our daily work. However, thanks to the find command it can be done quite efficiently. In this post, we have learned how to use the command and thus have this tool ready for our work.