How to find files on the Ubuntu command line

Find Files in Ubuntu

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:

  1. Find files using Find command
  2. Using Locate command to find files
  3. Using Grep command to search in 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 but contains 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 works with other Ubuntu versions as well.

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 for files using find command

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 a specific directory

Search files in the current directory

To search for files in a current directory, use

$ find .

Search files in the current directory

To search for a file named e.g “softwares” under current directory, use the following command in Terminal:

$ find . -iname Softwares

Search files

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 using a wildcard

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 for empty files

Search based on date and time

Using the 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

Search based on date and time

To search for files that were accessed less than 2 days ago, run the following command in Terminal:

$ find . –atime -2

Search by last access time

To search for files that were changed less than 2 days ago, run the following command in Terminal:

$ find . –ctime -2

Find files by modification time

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 size

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

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

Search based on file permissions

Method 2: Using Locate command

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. The reason for this is the background process that 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 it looks for them in its own database. The database must be updated regularly for the search program to work.

Installing Locate

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

Install locate command on Ubuntu

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 ignoring case distinctions.

Searching for a file

For instance, to search for a filename “centos”, use the following command in Terminal:

$ locate –i centos

Search for files using locate command

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 for Multiple files using locate

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

Search using Wildcard

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

Update locate database

Method 3: Using Grep command

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]

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 and that 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

Search for text in files with grep command

Where

-i is used to ignoring 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

Search for Multiple words

So, that was a brief overview of how you can search for files in Ubuntu using the command line. In this article, we learned about three useful command-line methods for searching for files. Depending on your search speed and conditions, you can choose one of these methods.