How to Find Files in Linux Terminal

Linux, regardless of the distro you use, comes with a number of GUI tools which allow searching for files. Many modern file managers support file searching right in the file list. However, there are a number of situations when you need to use the console, for example, during an SSH session or when the X server doesn't start. Here is how you can find files quickly using the terminal.

If you can use the GUI, searching for files is not a problem. In my favorite XFCE desktop environment, the Thunar file manager allows searching for files by typing the file name directly in the file list.

Search Files Linux

Also, there's Catfish, a popular search tool with a search index, which can find your files really quickly.

Catfish Linux

I would like to share the methods I use myself when I work in terminal.
The first method involves the find utility, which exists in any distro, even in embedded systems built on busybox. The other method is the locate command.

To find files in Linux terminal, do the following.

  1. Open your favorite terminal app. XFCE4 terminal is my personal preference.
  2. Type the following command:
    find /path/to/folder/ -iname *file_name_portion*

    The arguments above are as follows:
    /path/to/folder/ - the folder where to begin searching. If not specified, the search will be started in the current directory.
    Switches I use:
    -iname - search for files and folders which contain the specified portion in the name and ignore text case.

    An example:

    Here is the command I can use to find my articles I have written about the Opera browser:

    find ~/Documents/winaero/ -iname *opera*.txt

    Find Files In Linux

  3. If you need to find only files or only folders, add the option -type f for files or -type d for directories. Here are a few examples:
    Find Only Files In LinuxFind Only Dirs In Linux
  4. To find recently modified files and directories, you can use the following options of the find command:
    -mmin n - find files which were modified n minutes ago.-mtime n - find files which were modified n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -mtime +1, a file must have been modified at least two days ago.
  5. It is possible to execute a command for files found by your search query. See the following example:
    find ~/Documents/winaero/ -iname opera45.txt -type f -exec vim {} \;

    Here, we use the -exec option to run the vim text editor for all files in the search results. The "{} " portion stands for files found by the find command. The "\;" ending specifies the end of the command for the -exec option.Terminal Find Open In Vim File Opened In Vim

 

The locate command

The locate search tool uses a special file database to find files instantly. The index for the command can be created and updated by the updatedb command. While the search results appear instantly, you need to maintain the search index and keep it current, otherwise the locate command can find files which were deleted or moved to another directory.

In the general case, the syntax is as follows.

locate -i file_name

The -i option means "ignore text case".

Here is an example:

Find With Locate Linux Original

Find With Locate Linux 2

 

Bonus tip: Another method I often use is Midnight Commander (mc), the console file manager app. Unlike find or locate, mc is not included by default in all Linux distros I've tried. You may need to install it yourself.

Find files with mc

To find files containing some specific text using Midnight Commander, start the app and press the following sequence on the keyboard:
Alt + Shift + ?
This will open the search dialog.

Find Files With Mc Linux Original

Fill in the "File name:" section and press the Enter key. It will find all files which match the criteria.

Find Files With Mc Linux Results

You can place these files in the left or right panel using the Panelize option and copy/move/delete/view/do whatever you want with them.

That's it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.