How to find files on the Ubuntu command line

s2 0

A typical problem with working with computers is not being able to find the files that you have stored somewhere. Many GUI programs make it possible to search for files on Linux regardless of the distribution. However, in some situations you will only have access to the command line terminal, especially if you are administering servers or using SSH to access the system. On Linux, if you can’t find your files, you’ll need to use command line tools to find them.

This tutorial shows you how to use Terminal, the command line utility in Ubuntu OS, to find files. There are three known ways to search for files on the shell:

  1. Search files with 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 the files on the system, while grep is used to search files based on the text they contain. Find is a slower method, but it contains multiple search conditions, while Locate does not contain as many conditions, but is much faster than Find.

Let’s start with the procedure. We will use Ubuntu 20.04 LTS for describing the procedure mentioned in this article.

Method 1: using the Find command

Find is a very flexible command used to search files based on a variety of conditions. It is a very helpful tool when you are looking for a file for which you do not remember the name of the file. You can use the Find command to search for file and folder names, creation date, modification date, and permissions. You can combine these multiple conditions into a single Find command. It’s a very powerful but slower search tool.

The general syntax of the command is:

$ find /path/to/file/ -iname filename

Find files in a specific directory

You can use the Find command to find all files in a specific directory. The general syntax would be:

$ find /path/to/file/

For example, to find all files under the / home / tin / Downloads / Directory, start the terminal by pressing Ctrl + Alt + T and then run the following command in Terminal:

$ find /home/tin/Downloads/

Find files in the current directory

To search for files in a current directory, use

$ find .

Find files in the current directory

To look for a file named “softwares” in the current directory, use the following command in the terminal:

$ find . -iname Softwares

Find files

Search with wildcards

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 example, to search for files that begin with the word “cent”, run the following command in Terminal:

$ find /home/tin/ -iname cent*

Search with wildcards

Search for empty files

You can also use the Find command to find empty files. The general syntax would be:

$ find /path/to/file/ -iname -empty

For example to find empty files under the / home / tin Directory, run the following command in the terminal:

$ find /home/tin/ -empty

Search for empty files

Search by date and time

You can also use the Find command to find files based on when they were accessed or modified. There are time types you can use to browse files:

  • mtime (modification time): when the content of the file was last changed.
  • atime (access time): when the file was last accessed.
  • ctime (change time): when the file attributes were last changed.

For example, to search for files in a current directory that were changed less than 2 days ago, run the following command in Terminal:

$ find . -mtime -2

Search by date and time

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

$ find . –atime -2

Search for last access time

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

$ find . –ctime -2

Search files by modification time

Search by file size

For example, to find a file that is larger than 5MB in size, we can use the following command in the terminal:

$ find . –size +5M

Search by file size

Search based on file permissions

It is used to find files with certain permissions. The general syntax would be:

$ find /path/to/file/ -type -perm mode

Where:

Type Includes parameters D. or F. Value used to indicate the type of file. D. for directories and F. only for files.
mode can be either with numerical permission (e.g. 777, 655 .. etc) or symbolic permission (e.g. u = x, a = r + x).

For example, to look for a file with the privilege of 644 we can use the following command in the terminal:

$ find . –type f –perm 644

Search based on file permissions

Method 2: using the locate command

There is another Locate command that can be used to search files in Linux. It doesn’t have 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 is actually running in your system looking for and storing new files in its own database. Unlike the Find command, it doesn’t search your local hard drive for files and directories, but instead searches them in its own database. The database should be updated regularly for the search utility to work.

Install Locate

Locate is not installed by default in the Linux operating system. You have to install it manually. Press Ctrl + Alt + T to start the terminal and then enter the following command as sudo to install the locate utility:

$ sudo apt-get install locate

Install the command

Once the installation is complete, you can use the Locate utility immediately.

The general syntax of the command is:

$ locate –i filename

Where -i is used to ignore case distinctions.

Search for a file

For example, to search for a file name “centos”, use the following command in the terminal:

$ locate –i centos

Search with the command

A search is made for all files whose file names contain the character string “centos”.

Search for multiple files

You can also search for multiple file names at the same time. For example, use the following command in Terminal to look for two files:sdn.txt “ and “Centos “:

$ locate –i sdn.txt centos

Search for multiple files with locate

Search with wildcard

You can also use the wildcard character to find the files that match the query. For example, to search for all files that end in “.iso”, use the following command in Terminal:

$ locate –i *.iso

Search with wildcard

Update localize database

The Locate command relies on its database, so it needs to be updated regularly. Run the following command in Terminal to update the Locate utility database:

$ sudo updatedb

Update localize database

Method 3: using the grep command

Although grep is not intended to directly search files on your system, it is used to search text instead. However, you can use it to display the names of files that contain the particular string that matches your search query. To search for a string, you need to 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 a string that we want to search through.

If you are looking for a file that contains a word that you suspect may be in a certain directory, you can use the command syntax above to search for it in Terminal.

For example, to look for a file that contains the word “tintin” and we think that this file is in our Downloads Folder we can use the following command to find this file.

$ grep -r –i “tintin” /home/tin/Downloads

Search for text in files with the grep command

Where

-i is used to ignore case distinctions

–R is used to search recursively for the specified directory

Search for multiple words

You can also search for multiple strings at the same time. You must use the backslash “” and the pipe character “|”. Characters between your strings.

For example, to look for two strings “tintin” and “ping” we can use the following command in the terminal:

$ grep –r –I “tintin|ping” /home/tin/Downloads

Search for multiple words

So that was the quick overview of how to use the command line to search for files in Ubuntu. In this article, we saw three useful command line methods for finding files. You can choose one of these methods depending on your search speed and conditions.

How to find files on the Ubuntu command line