Finding data in a system with thousands of files becomes very difficult for administrators, especially for system users who are not familiar with the command line. While searching via the graphical user interface is possible, it has certain limitations in terms of speed and functionality. The command line provides flexibility to quickly search files using various search criteria. In Linux, there are several commands you can use to find files and folders on your system.
In this article, we will learn how to use various commands to find files and folders in your Debian system. We will use the following commands to find files:
- Find command
- Locate command
- Grep command
Note that we used Debian 10 and Debian 11 to run the commands and procedures mentioned in this article.
Using Find command
Find is the most effective command for searching files in a system. It comes in handy even if you do not know the name of the file by allowing you to search files based on various conditions such as file creation date, modification date, permissions, etc.
The following syntax can be used to search a file using the Find command:
$ find /path/to/file/ -iname filename
For instance, to search for a file named “license.pdf” in the ~/Downloads directory, use the following command:
$ find ~/Downloads/ -iname license.pdf
Remember that, if you do not specify the directory, it will search the files in your current directory.
Search file using wildcard
The wildcard characters can also be used to find the files that match the query.
$ find /path/to/file/ -iname filename\*
For instance, to search all files in a directory that starts with the word “test”, the following command can be used:
$ find ~/Downloads -iname test\*
Search for empty files
To search for empty files in a directory, use the following command syntax:
$ find /path/to/file/ -empty
For instance, to find all the empty files in the ~/Downloads directory, the following command will be used:
$ find ~/Downloads/ -empty
Search files based on date and time
You can also search for files based on when they have been accessed or changed. You can search for files based on the following conditions:
- mtime (Modification time in days)
- atime (Access time in days)
- ctime (Change time in days)
To find files that were modified less than 3 days ago, use the following command in Terminal:
$ find /path/to/file -mtime -3
Similarly, to find files that were modified more than 3 days ago, use +3.
To find files that were accessed less than 3 days ago, use the following command in Terminal:
$ find /path/to/file -atime -3
To find files that were changed less than 3 days ago, use the following command in Terminal:
$ find /path/to/file -ctime -3
Search based on file size
To search for files based on size, use -size switch followed by the file size. To find the file with a size of 5kb, use:
To find files with a size less than 5M, use:
$ find /path/to/file -size -5M
To find files with sizes more than 5M, use:
$ find /path/to/file -size +5M
Search based on file permissions
To search files with particular permissions, use the following syntax:
$ find /path/to/file/ -type -perm mode
Enter d or f after the type parameter to mention the type of file. (d for directories and f for files). Replace mode with numeric (e.g 777, 655.. etc )or symbolic permissions( e.g u=x, a=r+x).
For instance, to search for a file with the permission of 655, use the following command in Terminal:
$ find /path/to/file -type f -perm 777
Using locate command
Another command Locate can also be used to search for files in Linux. It does not offer as many search criteria as the Find command does however it is much quicker and more efficient than the Find command. It maintains its own database by keeping a record of new files added on your system. So whenever you search for a file, it does not search it in your hard disk. Instead, it searches for the file in its own database.
Locate does not come pre-installed in the Linux distributions. You will have to manually install it. Run the following command in your command line terminal in order to install Locate utility.
$ sudo apt-get install locate
Once installed, you can start using it to search for files in your system.
The following syntax can be used to search files:
$ locate –i <path/to/file or filename>
-i is used to ignoring the case of the file name.
Searching for a file
For instance, to search for a filename “license”, enter the following command in Terminal:
$ locate –i license.pdf
Search for Multiple files
It can also be used to search for multiple filenames at once. For instance, use the following command in Terminal to search for two separate files “license.pdf” and “testfile1” simultaneously:
Search using Wildcard
You can also find the files that match the query using the wildcard character. For instance to search for all the files that end in “.ttf”, use the following command in Terminal:
$ locate –i ~/Downloads/*.ttf
Update locate database
Locate command depends on its own database to function. So in order to function properly, the database needs to be regularly updated. To do so, run the following command in Terminal:
$ sudo updatedb
Using Grep command
Grep command is basically used to print text from files that match a specific pattern. However, you can use it to find files in a directory as well. For instance, we are looking for a file but we do not know the file name. In that case, we can search its location using a keyword it contains.
$ grep OPTIONS PATTERN /path/to/file
Where OPTIONS holds some search control options and PATTERN holds the keyword we wish to search for.
In the following example, we will use grep to find the files that contain the keyword “account”.
$ grep –r -i “account” ~/Downloads
-i is used to ignoring the case of mentioned keywords
-r is used to recursively look in the specified directory
In this article, we have discussed some command-line ways using which you find files in a Debian system. You may use GUI for file searching but the command line offers more efficiency in terms of speed and functionality.