How to Find Files on Linux with Find and Locate

Linux file navigation may seem daunting at first, but it’s quite easy once you know the right commands to use. This article will walk you through how to locate files on Linux using the find and locate commands.

We’re going to navigate into a directory with the following files to demonstrate what will be returned when various commands are used. The directory contains these files:

File1 file1 minecraft

Getting Started:

Always remember that Linux has your back with a handy manual page for every command. If you ar eever lost, just type:

man find

This will open the handy “find” manual page for you.

Finding Files by Name:

In order to find a file by name type:

find -name “File1”

This is a case sensitive search, so it returned just one file:

./File1

If we want to run a case insensitive search, we can do this:

find -iname “File1”

This will return both uppercase and lowercase results:

./file1

./File1

What if we only want to return files with names that don’t contain a certain string? Use:

find -not -name “file”

This will return all files that don’t contain the string “file” in them.

Finding Files by Type:

You can search for files by type with the following command:

find -type typequery

Some examples of file types are:

f: regular file
d: directory
l: symbolic link
c: character devices
b: block devices

In order to find a regular file called “file1” use:

find -type f -name “file1”

Finding Files by Time:

You can find files based on access time (-atime), modified time (-mtime), and change time (-ctime) flags.

Let’s find a file modified more than 5 days ago:

find / -ctime +5

Less than 1 day ago:

find / -ctime -1

More than 25 minutes ago:

find / -mmin +25

Finding Files by User or Group:

The -user and -group flags can be used to find a file located by a specific user or group

Find all files owned by user “mc”:

find / -user mc

Find all files owned by group “mc” with the case sensitive name “mirrorlist.txt”

find / -group mc -name “mirrorlist.txt”

We can even find files based on permissions. This will list all files with 755 permissions:

find / -perm -755

Finding Files by Size:

Find can filter files based on their size. SImply use the -size flag with the following size conventions:

c: bytes
k: Kilobytes
M: Megabytes
G: Gigabytes
b: 512-byte blocks

In order to find a file that is exactly1GB in size, simply type:

find / -size 1G

Greater than 1GB:

find / -size +1G

Less than 1GB:

find / -size -1G

Performing Actions based on Find Output:

The -exec command allows you to execute an action against all of the files that are output from the find command.

Find a file named file1 and change permissions to 644:

find / -name “file1” -exec chmod 664 {} \;

Using the Locate Command:

An alternative to the find command is the locate command. It builds a database of files on the system, so searches tend to be faster.

It can be installed by running the following:

yum install mlocate

The database can be manually updated by running the following command:

updatedb

To locate files by name, type:

locate filename

By default, this will return any file that has the string “filename” in its location. To locate files only on the name of the actual file, use the –basename option:

locate -b filename

You can use wildcard characters too such as *. To find all files ending in .html, type:

locate *.html

You can use pipe or redirect to take the standard output of the locate command and send it to the standard input of another command, or to file:

locate *.html | grep file1
or

locate *.html > listoffiles.txt