How to Find Files on Linux with find:

The find command works exactly how you would expect. It performs a real-time search in your Linux server, scanning and evaluating directories based on the rules you define. By using rules, you can narrow your search down to exactly what you need.

Many administrators use “find” for locating:

  • Configuration Files
  • Log Directories
  • Checking Permissions
  • Locating Large Files

The best thing about “find” is that, unlike “locate”, this command scans the system directory at the time of submission. This means that even file changes that occurred seconds before issuing the command will be clearly reflected in the output.

Here’s how the syntax looks:

find [path] [options] [expression]

Here’s also a quick breakdown:

DirectoryThe directory where the search will take place./home
OptionsSearch behavior modifiers, tightening the search.-type, -name
ExpressionAdditional conditions applied to the file search.-size +100M

Here’s a quick example of a “find”:

find /folder1 -name "textfile14.txt"

This command uses “find” to search folder1 and all of its subdirectories for a file with the name “textfile14”. The output will include the exact location of the file, its size, last modification date, and the file type (in this case “.txt”).

Quick Tip: A quick way to explore what you can do with the “find command” is to run “man find” to reveal the manual with all flags, expressions, and usage examples.

Finding Files by Name

The most common use of the find command is to locate files by their name. The search works by specifying the exact file name. You can also make the search case sensitive by adding an “i” before “name”, looking like this “-iname”.

Here’s an example of general and case-sensitive searches:

find / -name "textfile14"
find / -iname "textfile14"

The output should show the directory where “textfile14” is located, along with all the variables.

See Also: How to Rename a Directory in Linux

Searching with Wildcards

The so-called Wildcards can help you search with patterns, rather than with a specific name. In many cases, you would just want to check whether a file exists, without even knowing its name.

Here are the wildcards you can use to narrow your search down to specific file types:

Pattern:Meaning:Example:Result:
*Any number of characters*.logMatches error.log, server.log
?Searches a single characterfile?.txtMatches file1.txt, fileA.txt
[abc]Matches only one characterfile[12].txtMatches file1.txt, file2.txt
[a-z]Matches a character rangefile[a-z].txtMatches filea.txt, fileb.txt
[!abc]Excludes listed charactersfile[!1].txtMatches file2.txt, fileA.txt

Here’s an example of finding a file with a specific suffix:

find /var/log -name "log[6-11].txt"

Here we search the directory “/var/log” for the file name “log” with suffixes “7, 8, 9, 10, and 11”. 

The output would look like this:

log7.txt
log8.txt
log9.txt
log10.txt
log11.txt

You can also use “*” to find all “log” files, or even “?” to search for a single character. There are endless possibilities, allowing you to narrow down your search and find exactly what you need.

Excluding Files From Results

There is another way to use the “find” command. You can search a directory and only show the files you’re interested in by excluding files from the search
This is possible by using the “-not” operator.

find /home -not -name "log"
find /var -not -name "*.log"

The first command finds all files that don’t contain the name “log”. Similarly, the other command excludes all files with the extension “.log”.

Note: This only works in the specified directory (in our example, we search “/var”).

Finding Files by Their Type

Looking for files by their type is probably one of the most interesting functionalities of the find command. It allows you to find anything by its file category, e.g, file, directory, link, or device.

Here are the operators that make this possible:

fRegular file
dDirectory
lSymbolic link
cCharacter device
bBlock device

Here’s an example:

find /home -type f -name "file14"
find /var/www -type d

The first command here finds a regular item named “file14”, while the second command finds a directory within the “/var” directory. Using those operators, you can specify your search further.

Finding Files by Time

Another interesting method of finding files is by time. This means you can narrow your search down to files that were last accessed, last modified, or even the time when metadata changed.

Here are the operators and what they do:

-atimeLast access time
-mtimeLast modification time
-ctimeMetadata change time

Here are some examples:

find / -ctime +7
find / -ctime -3
find / -mmin +30

The first command here locates any files modified more than a week ago, while the second command locates files modified within the last 3 days. The third command in the example finds files that were modified more than half an hour ago.

Note: Finding files by time is particularly helpful when trying to get rid of old logs or organize files based on their usage frequency.

Finding Files by Size

When you’re trying to clean up large files, the “find” command offers a few specific operators that allow you to specify and narrow down your search by size.

You can filter data by size, using those operators:

cBytes
kKilobytes
MMegabytes
GGigabytes
b512-byte blocks

For example:

find / -size 1G
find / -size +1G
find / -size -1G
find /var -type f -size +300M

As we can see, the first command finds files exactly 1 GB. The second and third find all files that are larger or smaller than 1 GB, respectively. The example final command identifies files over 300 megabytes in the directory “/var”.

Finding Files by User or Group

Another way to find files is to filter them by permission. On a Linux system (the distribution does not matter), a file always belongs to a certain user or a group. This way, you can find all files that belong to the same user or group by using this command.

find / -user exampleuser

You just need to replace “exampleuser” with the real name of your Linux system user. You can also add additional filters to this command, like group and name:

find / -group exampleuser -name "examplefile.txt"

Again, you need to replace the “exampleuser” with the name of your “group” to find all files that belong to that group with the name “examplefile.txt”.

Note: You can also add permissions as filters, for example, “find / -perm -777”.

See Also: How to Unzip Files in Linux

How to Find Files on Linux with locate:

The “locate” command is much faster in finding files than “find”, just because it goes through a pre-defined database, rather than scanning directories in real time. This approach makes the searches nearly instant, and it’s mostly used for troubleshooting by the system administrators.

Here’s the syntax:

locate [options] examplefilemame

The ”examplefilename” here would be the exact file name you would like to find. The [options] would be the filters that you’ll be adding (optional) to your search.

Here’s an example:

locate filename14.txt

Here, we’re locating “filename14”, and the output could look something like this:

/usr/local/filenames/filename14.txt

Installing locate

Unfortunately, some Linux distributions don’t come with a pre-installed locate. The tool is usually a part of the “plocate” or “mlocate” package, which might be missing from your Linux installation.

However, the installation is quick and easy:

Ubuntu and Debian:

sudo apt install plocate

CentOS, AlmaLinux, and Rocky:

sudo yum install mlocate

When the installation is ready, pre-define the database:

sudo updatedb

This is the initial command that scans the filesystem of your Linux server, allowing you to locate files within your database. The “updatedb” should be run every time a file change takes place.

Note: It’s recommended to update the database every time before you run the locate command.

Sensitive & Insensitive Searches

By default, the “locate” command will do a case-sensitive search when you’re looking for an exact and specified file name. Similar to the “find” command, you need to specify when the search doesn’t need to be case-sensitive.

Here’s an example of a case-sensitive and case-insensitive search:

locate examplefile14
locate -i examplefile14

The first command locates the file with the exact name “examplefile14”. The second command here adds “-i”, which disables the case-sensitivity.

Limiting the Number of Outputs

If you’re locating files in a large system, the number of outputs could be overwhelming. There is a way to limit the number of responses by specifying it in your command using the “-n” operator.

It works like this:

locate -n 30 log

Here “-n 30” tells the locate command to only return “30” outputs that include “log” in their name.

Searching with locate Patterns

If you didn’t know, the wildcards we’ve reviewed earlier, while exploring the find command, can also be used in combination with locate. This provides additional functionality to your command, allowing you to narrow your search down to specific variables.

Here are a few examples:

locate "*.log"
locate "*.conf"
locate "backup[0-9].tar"
locate "config?.php"
locate "python3.?.so"
locate "/var/log/*.log"

By using these wildcards, you can locate “.log” files, “.configuration” files, “.tar” files, and much more. These patterns are ideal when you’re troubleshooting and trying to find specific files that could lead you to the root of a problematic directory.

find vs. locate: Key Differences & Best Practices

We now know how both find and locate commands work, and how they serve different purposes based on the nature of your search. To wrap things up and understand their notable differences and best practices, we’ve compared them side-by-side:

Feature:find:locate:
Search MethodComplete, real-time system scans in the filesystem.Only searches the pre-scanned indexed file database.
Search SpeedSlower on large systems.Extremely fast in all cases.
Search AccuracyAlways accurateAccurate whenever the database has been updated.
Database NeedNoYes
Advanced FiltersYes, extensive filtering options.Limited filtering.
Metadata SearchYes, supports permissions, time, size, and ownership.No metadata filtering is available with the locate command.
Automation SupportStrong support with -exec actions.Not designed for any sort of Linux automation.
Best Use CaseSystem administration and scripting.Quick file lookups when troubleshooting errors.

The bottom line here is that each tool fits a different scenario.

See Also: How to Check Disk Space and Usage in Linux

ServerMania Database & Storage Solutions 

We know how fast a Linux system can grow in size. Logs, backups, assets, and all kinds of files pile up in your system to a point where find and locate might not be enough. Enterprise systems introduce many challenges that not only require daily usage of find and locate, but also a strong database and storage infrastructure, designed to handle it.

In many server environments, locating files is only the first step. Teams often pair commands like find and locate with scripted automation to handle recurring maintenance tasks more efficiently. For that, see our guide on automating Linux server scripts.

Production Ready Database & Storage Infrastructure

Here at ServerMania, we deliver Database Servers and Storage Servers, designed for heavy data handling and an environment polished for enterprises.

If you’re looking for reliable infrastructure through dedicated servers, we strongly encourage you to explore the ServerMania solutions and find a way to strengthen your operations. If you have any questions, get in touch with our 24/7 customer support.

Alternatively, schedule a free consultation with our database experts. 
💬 We’re available right now!