How to Find a File in Linux [Different Ways]

Written by: Bobbin Zachariah   |   Last updated: June 4, 2023

1. Introduction

To find Files in Linux you can either use inbuilt command line tools, GUI file managers, or some modern apps. Based on your preference you can choose between CLI or GUI for file search.

In this guide, we learn different ways to find files in Linux.

2. Using the find command

When you look for searching files, find is the built-in and primary command for searching files in Linux. However, there are other commands and apps available in Linux that can also be used to search for files:

Syntax:

find [path] [options] [expression]
  • [path] - Gives a start point and performs a recursive search.
  • [options] - pass specific requirements (e.g., -type f for regular files, -type d for directories).
  • [expression] Defines additional conditions or actions such as "-and", "-or" or "-not" for the search. Expressions are used to further refine the search based on various criteria.

It's worth mentioning fzf (fuzzy finder tool) and fd which are two popular and powerful alternatives to find. It provides an interactive and intuitive way to search for files.

2.1. Finding File by name

For example, to find files with the name “meta.db" in the current working directory and subdirectories:

$ find -type f -name "meta.db"
Finding File by name

The option ‘-type -f’ is used for finding regular files (or can use ‘-type d’ if you want to search only directories). Note: To restrict the search to a specific directory use find /path -type f -name string.

To find files with a specific name pattern - for example all text files with .txt extension

$ find -type f -name "*.txt"
find files with .txt extension

The asterisk (*) is a wildcard character that matches any sequence of characters.

To find files that contain the string "tracker" in its name, run the following command:

$ find -type f -name "*tracker*"
find files that contain the string "tracker

Find a file by name, ignoring the case sensitivity in the search:

$ find . -iname "filename"

2.2. Finding Hidden Files

Hidden files are files or directories which have names starting with a dot. To find all files with hidden filenames in the current directory and its subdirectories:

$ find -type f -name ".*"
finding hidden files

2.3. Finding Files in Specified Directory

To restrict the file search to a specific directory and its subdirectories use find /path -type f -name string.

To search files that include the string “err” in their filename in ‘/var/log’ directory (including its subdirectories), type:

$ find /var/log/ -name "*err*"
find files with name "err" in /var/log

To limit the search only up to a specific directory you can use depth option such as mindepth and maxdepth. This helps reduce resource consumption because by default find work on the entire tree recursively.

2.4. Finding by File size

To find files in Linux based on their size, you can use the find command along with the -size option

For example to search files with size 1M, type:

$ find -size 1M
find file with 1M size

Search for files larger than 2M and less than 5M:

$ find -size +2M -size -5M
Find files files larger than 2M and less than 5M

You may also use regular expressions to search for files based on complex patterns or conditions.

2.5. Finding Files by type

In Linux, there are different types of files. If you are specific about searching those files, use find with -type option. For example, to find all character device files, type:

$ find / -type c
find character device files

Common file types in Linux and corresponding type options are:

Regular files-type f
Directories-type d
Symbolic links-type l
Character devices-type c
Block devices-type b
Sockets-type s

2.6. Finding Files by modification time

To find files in Linux based on their modification time, use the find command with the -mtime option. For example to search for files that were modified within 5 days.

$ find / -mtime 5
find files that were modified within 5 days

Search for files that were modified more than 10 days ago.

$ find  -mtime +5 –mtime -10

You may also use file timestamps such as atime, ctime, amin, cmin, mmin to search files by time.

2.7. Finding Files by Owner or Permissions

The -user option allows you to search for files owned by a specific user.

Example - This will search the /home directory and its subdirectories for files owned by the user "ubuntu" and display the file paths that match.

$ find /home -user ubuntu
Find files owned by the user "ubuntu"

The -perm option allows you to search for files that have specific permission settings.

Example: The following command search for regular files (-type f) in the current directory and its subdirectories that have the permission setting of 755.

$ find -type f -perm 755

3. Using the locate command

In addition to the find command, we will learn about locate, which is a fast and efficient file search utility. It does not directly find files on the drive through each run, rather it searches through a pre-built database.

The locate utility is not preinstalled in many Linux distributions. You can easily install it using the Distro-specific package managers.

The syntax for the locate command

$ locate [options] [pattern]

Where [pattern] is any string or filename to search.

For example, to find a file named “ufw.log”, type:

$ locate ufw.log
locate ufw.log file

This command will search the system's locate database for any file or directory with the name "ufw.log" and display the corresponding file paths.

4. Using the which command

The which command is generally not used for searching files. But you can use it to locate the executable file associated with a given command.

Syntax: 

which <program name01>  <program name02> ….

Example:

$ which systemctl

This command will display the full path of the systemctl command if it is found in one of the directories listed in the $PATH environment variable

locate the executable file of systemctl command

Use -a option displays all occurrences of the specified command in the environment variable.

$ which docker


$ which - a docker

5. Using the whereis command

The ‘whereis’ is a command that will help you find the binary, the source, and the man page files for a program in Linux.

Syntax:

whereis [options] [COMMAND]

Where the [COMMAND] is the command you want to search its binary, source and page files.

Example:

$ whereis find
whereis find

In this example, the find command is located at /usr/bin/find, and the corresponding manual page file can be found at /usr/share/man/man1/find.1.gz and other locations.

To search only the binary files use ‘-b’ option:

$ whereis -b which

6. Using the grep command

While grep is primarily used for searching patterns within the contents of files, it can also be utilized to find files in Linux based on certain criteria. By combining grep with other commands such as find or ls, you can construct more complex file search operations.

Related Read: Grep and Regex: A Deep Dive into Text Searching

For example to search for files in a directory and its subdirectories, filtering them based on a search pattern, type:

$ find /path/to/directory -type f -exec grep -l "search_pattern" {} +
find with grep to search files by contents of the files.

The output will be a list of file paths where the pattern was found.

7. GUI

Linux also offers graphical file managers with built-in search functionality. The file managers like Nautilus, Dolphin, or Thunar support this. You may also find many efficient apps that support file searches such as Cerebro, Fsearch, Krunner, and Recoll.

On Nautilus - the default file manager on Ubuntu, you can use the top search button to search files.

search file in Nautilus

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment