One typical issue when working with computers is that you can’t find the files you’ve saved somewhere. Many GUI programs allow you to search for files while working under Linux, independent of distribution. However, in some situations, you will only have access to the command line terminal, particularly if you manage servers or use SSH to access the system. If you can’t find your files, you must use command-line programs on Linux to look for them.
This tutorial will show you how to use the Terminal, which is the command-line program in Ubuntu OS, to find files. On the shell, there are three well-known ways for searching for files:
- Find files using Find command
- Using Locate command to find files
- Using Grep command to search in files
Find and Locate commands are used to search for the files in the system while grep is used to search file based in the text that it contains. Find is a slower method but contains several search conditions while Locate does not include as many conditions but is much faster than the Find.
Let us get started with the procedure. We will use Ubuntu 20.04 LTS for describing the procedure mentioned in this article.
Method 1: Using Find command
Find is a highly flexible command used to search files based on a variety of conditions. It is a very helpful tool when searching a file for which you do not remember the name of the file. Using the Find command you can search based on file and folder name, creation date, modification date, and permissions. You can combine these multiple conditions in a one Find command. It is a very powerful but slower search tool.
The general syntax of the command is:
$ find /path/to/file/ -iname filename
Search files in a specific directory
You can use the Find command to search for all files in a specific directory. The general syntax would be:
$ find /path/to/file/
For instance, to find all the files under the /home/tin/Downloads/ directory, launch the Terminal by pressing Ctrl+Alt+T and then run the following command in Terminal:
$ find /home/tin/Downloads/
Search files in the current directory
To search for files in a current directory, use
$ find .
To search for a file named e.g “softwares” under current directory, use the following command in Terminal:
$ find . -iname Softwares
Search using a wildcard
You can also use the wildcard characters to find the files that match the query. The general syntax would be:
$ find /path/to/file/ -iname filename*
For instance to search files that start with the word “cent”, run the following command in Terminal:
$ find /home/tin/ -iname cent*
Search for empty files
You can also search for empty files using the Find command. The general syntax would be:
$ find /path/to/file/ -iname -empty
For instance to find empty files under the /home/tin directory, run the following command in Terminal:
$ find /home/tin/ -empty
Search based on date and time
Using Find command, you can also search for files depending upon when they were access or modified. There are types of time you can use to search files:
- mtime (Modification time): when the file’s content was modified last time.
- atime (Access time): when the file was accessed last time.
- ctime (Change time): when the file attributes were modified last time.
For instance, to search for files in a current directory that were modified less than 2 days ago, run the following command in Terminal:
$ find . -mtime -2
To search for files that were accessed less than 2 days ago, run the following command in Terminal:
$ find . –atime -2
To search for files that were changed less than 2 days ago, run the following command in Terminal:
$ find . –ctime -2
Search based on file size
For instance, to search file whose size is larger than 5MB size, we can use the following command in Terminal:
$ find . –size +5M\
Search based on file permissions
It is used to find files with specific permission. The general syntax would be:
$ find /path/to/file/ -type -perm mode
Type parameter includes d or f value that are used for specifying type of the file. d for directories and f for files only.
mode can be either with numeric permission (e.g 777, 655.. etc) or symbolic permission (e.g u=x, a=r+x).
For instance, to search for a file with the permission of 644, we can use the following command in Terminal:
$ find . –type f –perm 644
Method 2: Using Locate command
There is another command Locate that can be used to search files in Linux. It does not have as many search conditions as the Find utility offers but it is much better and faster than the Find utility. The reason behind is the background process that actually runs in your system and searches and stores new files in its own database. Unlike Find command, it does not search your local hard disk for files and directories but instead, it searches them in its own database. Its database should be regularly updated for the search utility to work.
Locate is not installed by default in the Linux OS. You will need to manually install it. Press Ctrl+Alt+T to launch the Terminal and then type the following command as sudo to install the Locate utility:
$ sudo apt-get install locate
Once the installation is completed, you can use the Locate utility right away.
The general syntax of the command is:
$ locate –i filename
Where -i is used to ignore case distinctions.
Searching for a file
For instance, to search for a filename “centos”, use the following command in Terminal:
$ locate –i centos
It will search for all the files that include the string “centos” in their filenames.
Search for Multiple files
You can also search for multiple file names simultaneously. For instance, use the following command in Terminal to search for two files “sdn.txt” and “centos”:
$ locate –i sdn.txt centos
Search using Wildcard
You can also use the wildcard character to find the files that match the query. For instance to search for all the files that ends in “.iso”, use the following command in Terminal:
$ locate –i *.iso
Update locate database
Locate command relies on its database to work, so it needs to be updated regularly. Run the following command in Terminal to update the Locate utility database:
$ sudo updatedb
Method 3: Using Grep command
Although Grep is not for directly searching files in your system, instead it is used to search text. However, you can use it to display the names of files that contain the particular string of characters that matches your search query. To search for a string, you must enclose it in double quotes.
The general Syntax of the command is:
$ grep [options] [pattern] [/path/to/file]
where [options] parameter contains generic options to control the search and [pattern] contains string that we want to search.
If you are looking for a file that contains a word, which you suspect might be in your any specific directory, you can search for it using the above command syntax in Terminal.
For instance, to search for a file that contains the word “tintin” and we think that file might be in our Downloads folder, we can use the following command to locate that file.
$ grep -r –i “tintin” /home/tin/Downloads
-i is used to ignore case distinctions
–r is used to search for the specified directory recursively
Search for Multiple words
You can also search for multiple strings simultaneously. You have to use backslash “\” and pipe sign “|” characters among your strings.
For instance to search for two strings “tintin” and “ping”, we can use the following command in Terminal:
$ grep –r –I “tintin\|ping” /home/tin/Downloads
So, that was the brief overview of how you can search for files using the command line in Ubuntu. In this article, we have seen three useful command line methods to search for files. You can choose any one of these methods depending upon the search speed and conditions.