How to Use the Find Command On Linux

Getting Started

Always remember that Linux has your back! There is a handy manual page for every command. If you are ever lost, just type the phrase:

man find

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

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 “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 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

Conclusion

You are now familiar with the find and locate commands on Linux. With this information,  you can now quickly locate files on your ServerMania hybrid smart server or dedicated server.  Please contact the ServerMania support team if you need any help!