In this article, we would cover how to search files in Ubuntu using find command-line utility. In certain Ubuntu configurations, we don’t have the luxury of Graphical User Interface (GUI). Finding files through a GUI is just a matters of seconds. But, when it comes to command-line interface (CLI) – we need to know certain commands to proceed further. Only then, it would be possible for us to search required files.
One such command-line utility to search files is find. With find, we can use different file attributes as search option. It is worth mentioning here that, when searching for files which can only be accessed by a superuser. For those files, we would get a ‘Permission denied‘ error.
It would throw an error –
find: ‘/var/cache/lightdm/dmrc/’: Permission denied
But, when we do –
sudo find /var/cache/lightdm/dmrc/
It shows us the contents of the directory –
Search files in Ubuntu using find command
We will cover this in sections from here. Otherwise, things may get pretty confusing.
Section I. We know the file name but don’t remember its location. Then, in such a case – we can find files by name. For instance, to find a file import.txt in ~/Downloads directory. We can do –
find ~/Downloads -name import.txt
It is worth mentioning here that, -name option results are case sensitive. But, if we aren’t sure whether our file has lower case or upper case letters. Then, use -iname option.
find ~/Downloads -iname import.txt
Section II. To find all the files which start with a particular letter. Let’s say we want to list all file names which start with letter – i. This can be done through asterisk (*) –
find ~/Downloads -name i*
Similarly, we can put asterisk (*) in different combinations. Like *i or K***i etc. It just depends on patterns we look for.
Section III. Find files by the size. Let’s say we want to find all the files which are greater than or less than a specific size.
find ~/Downloads -size +10M
The above instruction would pull up all the files which are greater than 10 MBs in size. Similarly,
find ~/Downloads -size -10M
This will pull all the files which are less than 10 MBs in size.
In conclusion, we have covered few basics here. And, there is more to it, which we will discuss in coming articles.