A typical problem when working with computers is that you can't find the files you have stored somewhere. Many GUI programs allow you to search for files while working under Linux, regardless of the distribution. However, in some situations, you only have access to the command line terminal, especially if you manage servers or use SSH to access the system. If you can't find your files, you'll have to search for them using command-line utilities on Linux.
In this tutorial, you'll learn how to use Terminal, the command line utility in Ubuntu OS, to find files. In the shell, there are three well-known ways to search for files:
The Find and Locate commands are used to search for files on the system, while grep is used to search for files based on the text they contain. Find is a slower method with several search conditions, while Locate does not contain as many conditions but is much faster than Find.
Let's start with the method. We use Ubuntu 22.04 LTS to describe the procedure in this article, but it also works with other Ubuntu versions.
Find is a highly flexible command that searches files based on various 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 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
You can search for all files in a specific directory using the Find command. 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/
To search for files in a current directory, use
$ find .
To search for a file named e.g "softwares" under the current directory, use the following command in Terminal:
$ find . -iname Softwares
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*
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
Using the Find command, you can also search for files depending upon when they were accessed or modified. There are types of time you can use to search files:
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
For instance, to search file whose size is larger than 5MB size, we can use the following command in Terminal:
$ find . -size +5M\
It is used to find files with specific permission. The general syntax would be:
$ find /path/to/file/ -type -perm mode
Where:
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
There is another command, Locate, that you can use to search for files under Linux. It does not offer as many search conditions as the Find utility, but it is much better and faster than the Find utility. This is because the background process runs in your system and searches and stores new files in its own database. Unlike the Find command, it does not search your local hard drive for files and directories but looks for them in its own database. The database must be updated regularly for the search program to work.
Locate is not installed by default in the Linux OS. You will need to install it manually. 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 immediately use the Locate utility.
The general syntax of the command is:
$ locate -i filename
Where -i is used to ignore case distinctions.
For instance, to search for a filename "centos", use the following command in the Terminal:
$ locate -i centos
It will search for all the files that include the string "centos" in their filenames.
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
You can also use the wildcard character to find the files that match the query. For instance, to search for all the files that end in ".iso", use the following command in Terminal:
$ locate -i *.iso
The 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
Grep is not used to search files in your system directly but for text search. However, you can use it to display the names of files that contain a particular string that matches your search query. To search for a string, you must enclose it in quotes.
The general Syntax of the command is:
$ grep [options] [pattern] [/path/to/file]
the [options] parameter contains generic options to control the search, and [pattern] contains a string we want to search.
If you are looking for a file containing a word you suspect is in a particular directory, you can search for it in the terminal using the above command syntax.
For example, to search for a file that contains the word "tintin" and that we assume is in our Downloads folder, we can use the following command to find the file.
$ grep -r –i "tintin" /home/tin/Downloads
Where
-i is used to ignoring case distinctions
-r is used to search for the specified directory recursively
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 a brief overview of how you can search for files in Ubuntu using the command line. This article taught us three useful command-line methods for searching for files. Depending on your search speed and conditions, you can choose one of these methods.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…