There are times when you want to access a specific file but can't find it on your system due to lack of folder organization. Luckily, Linux provides you with some handy utilities that allow you to easily search for files on your computer.

The find command is one such tool that can be used to search for a file using its file name, permissions, extension, size, etc. This guide will explain the Linux Find command and provide some examples that demonstrate how powerful this utility is.

What Is the Find Command?

As the name suggests, the find command allows a user to search for files present on their local storage. Unlike normal search features present in Linux file managers, the find command has additional functions that can filter the files according to certain conditions.

Also, the find command provides several criteria to locate files on a computer. You can even use regular expressions to match a file's name with a specific pattern.

How to Find Files in Linux

The find command has numerous options and functions that filter the files based on the specified conditions.

Find Command Syntax

The basic syntax of the find command is:

        find [path] [options] [expression]
    

For example, the following command will search for text files in the /home directory.

        find /home -type f -name "*.txt"
    

Keep in mind that before searching for files on your storage, you need to have read permissions for that particular directory.

Search for Files by Name

The most common use of the find command is searching for a file by its name. To find a file using the filename, use the -name flag with the default command.

        find /home -type f -name filename.txt
    

The aforementioned command will search for a file named filename.txt in the /home directory. The -type f option tells the system that we're looking for a File.

If you want to ignore the character case in the file name, replace the -name option with -iname.

        find /home -type f -iname FileName
    

This command will locate a file that has either of the following names: Filename, filename, FileName, FiLename, etc.

Like any other Linux command, you can use . (period) to specify the relative path of the current directory as well.

        find . -type f -name filename.txt
    

Similarly, / for /root and ~ for /home can be used as well.

Find Files by Extension

Searching for files with a particular extension can help in narrowing down your search results. To find a file by its extension, use the following regular expression with the -name and -iname flag.

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

This command will display a list of all the files that have the .pdf extension. Note that you will have to escape the asterisk (*) character with either quotes ("") or a backward slash (\) so that the terminal interprets it as a wildcard character.

You can also inverse the above command by using the -not flag. The following command will search for files that do not have the .pdf extension.

        find /home -type f -not -name "*.pdf"
    

You can even pipe the find command with other Linux commands. For example, to change the moderation permissions for each file that fits the condition:

        find /home - type f "*.pdf" -exec chmod -777 {} \;
    

This command will search for all PDF files in the /home directory and change their permissions so that anyone can read, write, and execute those files.

Search for Specific File Types

In addition to files, the find command can search for other type of files as well. Directories, symbolic links, sockets, and character devices are some of the file types that are supported by find.

Till now, we have been using the -type f option in the find command. The f stands for File. To search for other file types in Linux, replace f with other reserved characters.

  • f: regular files
  • d: directories
  • l: symbolic links
  • c: character devices
  • b: block devices
  • p: named pipe
  • s: sockets

To search for sub-directories present in the /home directory:

        find /home -type d
    

Find Files by Size

The -size flag allows you to search for files that take up a particular amount of space on the disk. The following suffixes denote the various file sizes:

  • b: 512-byte blocks
  • c: bytes
  • w: two-byte words
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

To find all the files that have a file size of 1GB:

        find /home -type f -size 1G
    

To search for files less than 1GB, add the minus (-) character before specifying the size:

        find /home -type f -size -1G
    

Similarly, use the plus (+) operator to locate files that are greater than 1GB:

        find /home -type f -size +1G
    

To search for files within a size range:

        find /home -type f -size +1M -size -10M
    

Find Files Using Timestamps

You might already know that Linux assigns specific timestamps to each and every file on your storage. These timestamps contain the modification time, change time, and access time.

To find files with a particular modification time:

        find /home -type f -name "*.txt" -mtime 5
    

The above-mentioned command will print all the files that were modified in the last five days. Similarly, you can also use -atime and -ctime to filter the files according to the access time and change time.

You can also use the plus and minus signs to find files greater than or smaller than a specific timestamp.

        find /home -type f -name "*.txt" -mtime +5
    

Search for Files With Specific Permissions

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

        find /home -type f -perm 777
    

Use the forward-slash character (/) to list the file if at least one category has correct the set of permissions provided.

        find /home -type f -perm /777
    

Find Files by Owner

Use the -user flag to get files that belong to a particular user.

        find /home -user randomuser
    

Find and Delete Files

To delete all the filtered files using find, add the -delete flag at the end of the command.

        find /home -type f -name "*.pdf" -delete
    

The aforementioned command will delete all the PDF files that are present in the /home directory.

You won't be able to delete non-empty directories with find. You will have to use the rm command to delete such folders on your Linux system.

Organizing Files in Linux

Finding files is tough if you have hundreds of directories on your system with no appropriate names. The find command comes in handy when you want to filter out files in a directory according to one specific criterion.

To get the most out of your storage on a Linux system, file organization and management are a must. Proper grouping of folders and removal of redundant data can help you in quickly accessing the files that you want.