Find Files and Directories in Linux Like a Pro

Need to know how to find a file in Linux? Let me show you how to do this.

1
Find Files and Directories in Linux

This guide will show you how to use “find” and “locate” commands to find files and directories on your Linux file systems.

Table of Contents

There are times when you want to access a specific file but can’t find it on your Linux system. While there are many ways with which we can search and locate files and directories on Linux, the easiest and quickest is probably through the terminal.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

The find command is one of the most important and frequently used command line utility in Linux.

Find Files and Directories in Linux Using find Command

The Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria. It supports searching by file, folder, name, creation date, modification date, owner and permissions.

The find command is used to find files and directories and perform subsequent operations on them. It searches for files and directories in each path recursively. Thus, when the find command encounters a directory inside the given path, it looks for other files and directories inside it.

Find Files by Name

The general syntax of the find command is:

find [path] [options] [expression]

Let’s break down this syntax:

  • path: Defines the starting directory where find will search the files.
  • options: Controls the find process’s behavior and optimization method.
  • expression: This attribute is made up of options, search patterns, and actions separated by operators.

The most common use of the find command is searching for a file by its name. The -type f option tells the system that we’re looking for a File. To find a file using the filename, use the -name flag with the default command.

For example, to search for a file named report.pdf in the /home directory, you would use the following command:

find /home -type f -name report.pdf

As you know, Linux is case sensitive about file names, so if you’re looking for a file named Report.pdf, the command shown above will return no results. In that case you will need to use the -iname option instead of -name.

The -iname option runs a case insensitive search, so we can do this:

find /home -type f -iname report.pdf

This command will locate a file that has either of the following names: report.pdf, Report.pdf, REPORT.pdf, etc.

Find Files by Partial Name

You can use filename meta-characters such as asterisk (*), but you should either put an escape character (\) in front of each of them or enclose them in quotes.

For example, to find all files ending with a .pdf, you could run:

find /home -type f -name '*.pdf'

which is the same as:

find /home -type f -name \*.pdf

Similarly, to find all the files on your Linux system whose names begin with report, you could run:

find /home -type f -name 'report*'

Find Files by Size

Using find command, we can also easily achieve something that might look complicate: to find files bigger or smaller than given size.

The -size option on find allows us to search for files of a specific size. The + and - prefixes signify “greater than” and “less than”, as usual.

The below example will search for all files greater than 1 Gigabyte. Note the use of + sign:

find /home -type f -size +1G

In the example above, the suffix G denotes Gigabytes. The other popular available suffixes are:

  • c: bytes.
  • k: Kilobytes.
  • M: Megabytes.

The above find command was used to search for all files greater than specified size. Next, find command example will search for all files with less than 200 Kilobytes in size. Note the use of - sign:

find /home -type f -size -200k

Now you probably wonder how to find files in Linux between a certain size. For example, you can find files between 100 Megabytes and 120 Megabytes in size, using the following command:

find /home -type f -size +100M -size -120M

Find Files Using Timestamps

Linux assigns specific timestamps to each and every file in the file system. The find command can also search for files based on their last modification, access, or change time.

The -mtime option is used to specify the number of days old that the file is. The expression can be used in two ways:

  • -mtime +N finds the files modified more than N days ago (for greater than).
  • -mtime -N finds the files modified less than N days ago (for less than).

For example, if you enter +3, it will find all the files from the /home directory older than 3 days.

find /home -type f -mtime +3

Similarly, if we want to find all the files that have changed in the last 24 hours:

find /home -type f -mtime -1

In the same way, you can use the -mmin N expression to rely on minutes instead of days.

For example, this command finds all the files from the /home directory modified in the last minute.

find /home -type f -mmin -1

Furthermore, we can even compose expressions. Here’s how to find files in Linux that have been changed less than 60 minutes ago and more than 30 minutes ago:

find /home -type f -mmin -60 -mmin +30

Find Files by Owner

To find files owned by a particular user or group, use the -user and -group options.

For example, to find files owned by the user john in the /home folder of the server:

find /home -type f -user john

Find all files belongs to a group called www-data in the /home directory:

find /home -type f -group www-data

Find Files by Permissions

The -perm option allows users to search for files with a particular set of permissions.

The below command will find the files with permission of exactly 644 in the current directory.

find /home -type f -perm 644

The use of the - option means “at least this permission level is set, and any higher permissions.”

find /home -type f -perm -644

This example displays all resources in the /home directory with at least 644 permissions. This means that files with the permissions such as 777, 745, 666, 655, 654, etc, would match, while files with permissions 642, 611, 600, 544, etc, would not match.

Find Directories by Name

All the examples we’ve seen so far return files. However, if you need to search for directories only, you can use the -type d switch.

In other words, you can prevent the find command in Linux from searching for other file types except directories by using -type d flag (d means directory).

To find a folder named scripts in the /home directory, run:

find /home -type d -name scripts

Similarly, all other options shown so far are also applicable when searching for directories.

For more about the find command in Linux, consult its manual page.

Find Files and Directories in Linux Using locate Command

While find is no doubt one of the most popular as well as powerful command line utilities for file searching in Linux, it not fast enough for situations where you need instantaneous results.

The locate command is faster than the find command because it uses a previously built database, whereas the find command searches in the real time, through all the actual directories and files.

If locate is not installed, you can easily install it using the package manager of your Linux distro.

On Ubuntu or any other Debian-based system, type the following command:

sudo apt install mlocate

If you are using Fedora or CentOS, use this command instead:

sudo dnf install mlocate

Arch Linux users need to execute:

sudo pacman -S mlocate

Before locate can be used, the database will need to be created. This is done with the updatedb command, which (as the name suggests) updates the database.

sudo updatedb

The database will be updated automatically on daily basis, but you can also update it yourself at any time so you can obtain up-to-the-minute results.

Related: How to Use the Cron Job Format to Schedule Task in Linux

Find Files by Name

The locate command is very easy to use. All you have to do is to pass it the filename you want to search.

locate report.pdf

Just like the find command, locate is configured to process queries in a case sensitive manner. To have locate command ignore case sensitivity and show results for both uppercase and lowercase queries, you need to use the -i option.

locate -i rEpoRT.pdf

Find Files by Partial Name

If you want to search for all filenames that have the string .pdf in them, then you can do that using locate in the following way:

locate '*.pdf'

Limit Search Results

You can limit your search returns to a required number to avoid redundancy with your search results using the -n option.

For example, if you want just 10 results from your queries, you can type the following command:

locate -n 10 '*.pdf'

Display the Number of Matching Entries

To count the number of occurrences of a file name or search pattern using the locate command, invoke the -c option as shown.

locate -c '*.pdf'

Get Information About locate Database

To gather more insights on the locate database mlocate.db that locate has cataloged, use the -S option:

locate -S
Database /var/lib/mlocate/mlocate.db:
	72376 directories
	1142596 files
	98622214 bytes in file names
	33950606 bytes used to store database

Just in case you need, here’s the man page for the locate command.

Conclusion

There is two most widely used file searching utilities accessible to Linux users are called find and locate. Both are good ways to find files on your system. It is up to you to decide which of these tools is appropriate in each situation.

This article should give you a fundamental understanding of how to find files on your Linux systems. Even if option were presented individually, you should remember that they can be combined in order to be able to specify even more the files you are looking for.

If this guide has helped you, please consider buying us a coffee.

Buy me a coffee!

Your support and encouragement are greatly appreciated!

1 Comment

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge