Find Files in Linux Using the Command Line

The Linux Find Command Line full blog

Finding files in Linux can be slightly challenging for new Linux users. However, with the proper guidance, anyone can shortcut files or locate how many files are in their Linux system.

You must use the command line to find files or a specific file name. Also known as the terminal, the command line provides several tools for searching files, including find, locate, grep, and where is, to name a few.

This article covers the most basic usage of these commands and explains how to use them to find files in your Linux system or how to perform a Linux find file command.

So, let’s get started!

12 Ways to Find Files in Linux

1. Find Command

The Linux command 'find' is an extremely powerful tool that allows users to search for files and directories using various criteria such as file name, size, and modification time, and view file permissions. The syntax to use when searching for a file using find is as follows:

copyfind [path] -name [filename]

Here, the path is the directory where you want to begin your search, and the filename is the file name you want to find. For example, to find files named example.txt in the current directory structure and its sub-directories, you'll have to use this command:

copyfind . -name example.txt

This will search for all files named example.txt.

You can even use wildcards to search for files in your Linux filesystem that match a specific pattern. For instance, to search for all files ending with the .txt extension, you'll need to enter the following command:

copyfind . -name "*.txt"

This will find a file (or files) ending with the .txt extension in the current files and directories.

2. Locate Command

Lines of code.

The locate command is another handy tool for finding files in Linux. It searches a pre-built database of files and directories and their locations so that it can be faster than the find command for large file systems. To search for a file using locate, use the following syntax:

copylocate [filename]

Like before, to search for a file named example.txt, you can use the following command:

copylocate example.txt

This will search for all files named example.txt in your entire file system.

Note that the locate command uses a pre-built database and must be updated regularly. To update the database, just run the following:

copysudo updatedb

Congrats! You're ready to use the locate command to search for files and directories now.

3. Grep Command

Also known as the command to search for files inside files, the grep command is another command for finding file patterns. It can search for a specific string or pattern in a file or a set of files. To search for a string in a file, use this syntax:

copygrep [string] [filename]

For instance, to search for the string example in a file named example.txt (classic, right?), you can use the following:

copygrep example example.txt

This will search for all occurrences of the string example in the file example.txt (i.e., think of symbolic links). On top of this, you can also use wildcards to search for patterns in multiple files. 

For example, to search for all files in a directory and its subdirectories that contain the string example, enter the following in the terminal:

copygrep -r "example" .

Piece of cake! 

4. -type Option With Find

The -type option in the find command allows you to search for files based on their type. For example, to search for all directories, go for the following command:

copyfind . -type d

This searches all directories.

5. -size Option With Find

Someone coding on a laptop.

The -size option in the find command also allows you to search for files in your system, this time based on their size, though.

Now, to search for all files in the current directory, as well as its subdirectories that are larger than 1MB, use this command:

copyfind . -size +1M

This will give the command to find files that are larger than 1MB.

6. Find command With the Exec Option

Let’s get back to the find command for a bit. It enables you to execute a command on the files that match your search patterns. For instance, to delete all files with the .bak extension, enter this command:

copyfind . -name "*.bak" -exec rm {} \;

This tells the system to find files with the .bak extension and delete them.

7. Find Command With the mtime and ctime Options

The mtime and ctime commands allow you to search for files based on their modification and creation times. For example, to search for all files in the current directory and its subdirectories that were modified in the last 7 days, just enter the following command:

copyfind . -type f -mtime -7

This lets you find a file that was modified in the last 7 days. Pretty handy, right?

8. Find Command With the User Option

Lines of code.

With the user option, you can search for files a specific user owns. 

To search for all files in the current directory and its subdirectories that are owned by the user john, use this command:

copyfind . -type f -user john

This searches for all files the user john owns in the current directory and its subdirectories.

9. Locate Files With the Locate Command

The locate command is a super fast way to locate files in Linux. It uses a database of all the files on your system to find files quickly. However, the database must be updated periodically, so it might not always be up-to-date. To search for files using the locate command, type the following command:

copylocate filename

Replace the filename with the file name you want to search for. The locate command will return a list of all files that match your search.

10. Find Command With the -name Option

The find command also allows you to search for files based on their name. To search for all files that have the word data in their name, use this command:

copyfind . -name "*data*"

This will search for all files with the word data in their name. The * characters before and after the word data are wildcards matching any characters before and after.

11. Find Command with Multiple Options

You can combine multiple options to search for files based on multiple criteria. For example, to search for all files in the current directory and its subdirectories that are larger than 1MB and were modified in the last 7 days, you can use the following command:

copyfind . -type f -size +1M -mtime -7

This gives the command to find files larger than 1MB and modified in the last 7 days, like the mtime and ctime commands.

12. whereis Command

You can also use whereis to search for other files, such as configuration files or libraries. For example, to search for the location of the Apache web server configuration file, you should use the following command:

copywhereis httpd.conf

This would output the location of the httpd.conf file, which typically contains the configuration settings for the Apache web server.

Conclusion

To sum up, the great thing about the command line is that when you master it, you can find files in seconds. The above were just a few examples of the countless commands and options available in Linux.

With some practice, you will locate the files you need on your Linux system using the terminal without breaking a sweat.

Last but not least, keep in mind that if your desktop doesn't offer the ability to search your system for files and folders, you can install a specific app, like a file manager, to easily search your directory hierarchy.

Konstantinos Akrivos
The author
Konstantinos Akrivos

Konstantinos, or Kon for short, is a seasoned freelancer passionate about Linux. Through his work, he helps users get the most out of their Linux machines and master the command line. When he is not trying out new Linux distros, he crafts sales copy for startups. Also, he's a proud guinea pig father and loves gardening. Konstantinos is based in Vólos, Magnisia, Greece, and is a business owner.

Frequently Asked Questions

What Does Find Command Return in Linux?

As discussed above, the Linux find command lists all the files and directories that match your specified search criteria. The basic syntax of the find command is the following (although, as shown in the previous example above, you should already know how to use the find command):

copyfind [path] [expression]

How Do I Search for a File in Linux?

In Linux OSs, you can use the grep command and the -l option to search for a file inside a file. This lists only the names of files that contain the search pattern. To use the grep command, enter the following in the terminal:

copygrep -l [pattern] [file...]

Where -l is the option to list the file names, [pattern] is the text pattern to search for, and [file...] is the name of the file or files to search in.

For example, to find files named example.txt inside a directory named mydir, you would type:

copygrep -l "example.txt" mydir/*

This should return a list of file names that contain the search pattern.

How Do I Search for Files in Linux?

In Linux, you can use various commands to search for files, including:

  • Find: It searches for files based on different criteria, like name, size, and modification time.

  • Locate: It locates files by name using a database of indexed files.

  • Grep: It searches for text patterns inside files.

  • Whereis: It searches and locates the binary, source code, and manual page files for a command.

These commands or tools, if you prefer, can be used in the terminal to locate matching files on the local system or remote servers, depending on the configuration and access permissions. Alternatively, you can use them to filter files, including jpg files, or locate an empty file.

In other words, consider the above commands as a file manager.

What Does Find Command Return in Linux?

In Linux systems, you should use the file command to search for a regular file, or db files and err files, as well as the root directory in a file system hierarchy. When you enter the command and execute the search, find will recursively traverse the directory tree from the specified starting point and give a handy list of all files and directors that match your search.

Moreover, the find command is customizable, meaning you can customize it with many options for better search and filtering and to manage files better.

For example, you can control the depth of the search, the type of files you want to include or exclude, like a specific file or conf files, or the time of the last file modification. In other words, you control the search results and search patterns.

Lastly, the find command output usually consists of a list of paths that match the specified search criteria, as discussed above. This can be further processed or passed as input to other commands.

How Do I Find a File in a Directory in the Terminal?

A CPU slot on a computer motherboard.

In the terminal, you can use the find command to search for a file or file type in a directory. Again, use the syntax mentioned above:

copyfind [path] -name [filename]

Where path is the starting directory for the search and filename is the file name you are searching for. Remember that you will always need to look for file permissions and empty files.