Linux Commands and Scripts

Steps To Find Files on Linux with Find and Locate

In this article, we will learn how to find files on Linux with find and locate commands.

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

We’ll navigate into a directory with the following files to display what will return by using different commands. The directory includes one file after another:

File1  file1 server

How to Use the Find Command On Linux

If you need any additional information about the command, you can use the man command to find out more options.

# man find

Finding Files by Name

In order to find a file by name, simply 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 search 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? Then we will use:

# find -not -name “file”

This will return all files that don’t contain the string “file” in them, and is applicable to other strings.

Finding Files by Type

If you want to search for files by type, you can do so 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 “findlist.txt”

# find / -group mc -name “findlist.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 exactly 1GB in size, simply type in the phrase:

# find / -size 1G

If it is 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 {} \;

How to Use the Locate Command On Linux

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

It can be installed by running the following:

# yum install locate

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 also use the wildcard characters, 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

We have learnt how to find files on Linux with find and locate commands.

[Need assistance to fix this error or install tools? We’ll help you.]

Related Articles