Finding Files Efficiently on Linux


The find command on Linux is used to search for files and directories and execute operations like searching by name, type, and date of modification.

Searching is one of the core operations that the operating system performs, and the find utility is a versatile tool to do it efficiently. In this guide, I will explore how to find files on Linux and how to utilize the find utility with different options.

The find Command

The find command searches the directory tree from a specified starting point by the given expression left to right until the desired result is achieved.

Basic syntax of the find command is given below:

find [Options] /[Starting-Point] [Expression] [Actions]

In this syntax:

[Starting-Point] is the path of the directory from where the search will start. If no starting point is given then the dot is assumed which signifies the current working directory.

. The current working directory
/ The entire hard drive
~ The home directory

[Options] is used to manage the symbolic links. For example, to include the symbolic links for search -P option never includes the symbolic links, while -L includes them.

[Expression] is used for the additional options to enhance the search. For example, to execute a command on the search results the -exec option is used.

[Actions] includes deleting, executing a command, and printing the output using modifiers.

[Optimization Levels] are user-defined levels used to set the filtering criteria for the find command. By default, it uses level 01 which means the find command will search the files by name and perform the other task.

01 The find command filters based on the file name; more efficient, less resource intensive
02 The find command filters based on the file name, and type without significantly affecting the resources
03 The find command automatically sort the search criteria based on resources; more aggressive filtering
The find command automatically sort the search criteria based on resources; more aggressive filtering

The optimization level cannot be applied explicitly, you may need to enhance the search by using different options.

We have understood the syntax, then proceed with examples to use the find command for searching files.

How to Find Files By Type

On Linux, there are various types of files, such as regular files, block device files, or character device files.

To find all the files in the current directory, the -type f option is used which signifies the regular files. For example, to list all the regular files with the paths in the current directory, the following command will be used.

find . -type f

To list all files in a specific directory such as in MyDirectory, use the command given below:

find MyDirectory -type f

Note that the output of the command will only list the files in the specified directory along with their paths.

Block devices are hardware devices such as hard drives which are controlled through files on Linux. To find the block device files, use the b flag with the -type option.

find / -type b

Here, the forward slash / signifies the root directory.

To find the character type files, use the c flag.

find / -type c

In the following sections, I will further use various options to enhance the search.

How to Find Files by Name

To find the files by name, the -name option will be used with the file name. For example, to find all the files with matching the pattern myFile in the directory MyDirectory, use:

find MyDirectory -type f -name "myFile*"

The asterisk (*) in the above command after the file name is the wildcard, which searches for results by pattern zero or more times. So, any file whose first 6 letters are myFile will be listed through the standard output.

Moreover, the find searches the directory tree, which means the outcomes of the command will be true if the file is in any subdirectory.

To ignore the case sensitivity the -iname option is used.

find MyDirectory -type f -iname "myfile*"

How to Find Files by File Extension

To find all the files by extension, use the -name option with the file extension and * wildcard. For example, to find all the files with .html extension in a directory MyDirectory.

find MyDirectory -type f -name "*.html"

Here, the wildcard * is mentioned before the extension pattern, which signifies that one or more files in the specified directory whose names end with .html will be listed.

Similarly, to find the log files use *.log, and to find configuration files use *.conf.

How to Find Hidden Files

On Linux, the hidden file names are started with a dot, so find the hidden file use -name option with the dot and asterisk wildcard.

find MyDirectory -type f -name ".*"

The above output lists the hidden files in the MyDirectory folder.

Note that by default, hidden files are not displayed when using the find command.

How to Find Files by Size

The find command has a built-in option to search files by size called -size. The files can be searched by the exact, greater, or smaller than the mentioned size. For sizes greater than the mentioned size, the + sign is prefixed and for the smaller – sign is used. For example, to find all the files greater than 2 Megabytes, the +2M is used with the -size option.

find . -type f -size +2M

The default unit is b (bytes), other units are listed below.

b Used for 512 bytes blocks
c Used for bytes
w Used for two-bytes words
k For Kilobytes
M For Megabytes
G For Gigabytes

The general syntax to find files by sizes in the mentioned starting point is given below:

find /[Starting-Point] -type f -size [+/-]N[bcwkMG]

The [Starting-Point] is the directory and N is the size. Skipping the + or – sign in the above syntax will give you the exact size of the files.

How to Find Empty Files

The -empty option is used to find the empty files in the mentioned directory. For instance, to search the empty files in the current working directory use.

find . -type f -empty

How to Find Files by Modification, Access, and Change Time

To find the files by the modification time, the -mtime option is used with the n which indicates time as nx24. Similarly, the + and – signs are prefixed to find files more than or less than the mentioned time of modification. For instance, to search files that were altered two days ago, the integer 2 will be used with the -mtime option.

find . -type f -mtime 2

To find files that were altered more than 2 days ago, +2 will be used.

find . -type f -mtime +2

In the same manner, to list files that were last modified less than 2 days ago, the -2 will be used after -mtime option.

To find the files by modification time in minutes, the –mmin option is used. The usage of prefixes + and – will remain the same as mentioned above. For example, to find the files that were modified exactly 5 minutes ago, the following command will be used.

find . -type f -mmin 5

To find the files by the last access use the -atime option with the time number of days. For instance, to search files that were last accessed more than 2 days ago use -atime with +2.

find . -type f -atime +2

To set the search criteria by number of minutes use -amin.

Similarly, to find the files that were last changed use the -ctime option. For instance, to find the files that were changed less than five days ago use -5 with the -ctime option.

find . -type f -atime -5

To set the search criteria by number of minutes use -cmin.

How to Find Files by Permissions

To find the files by permissions, the -perm option will be used. For example, to find all the files in the current working directory that have writable permissions for the current user, use the -perm option with u=w, here u signifies the user while w, is writable.

find MyDirectory -type f -perm /u=w

The /u=w can be replaced with the octal notation.

find MyDirectory -type f -perm 600

Other approaches to using the -perm option are given below.

-perm mode To find files/directories exactly matching the permission bits
-perm -mode To find files/directories that do not match the mentioned permission bits
-perm /mode To find files/directories matching one or more mentioned permission bits

Let’s understand the above mode with an example.

find . -type f -perm 222

find . -type f -perm -222

find . -type f -perm /222

In the above example, the 222 is the octal notation of giving writable permission to the user, group, and others.

  1. 2 Writable permissions to user
  2. 2 Writable permissions to group
  3. 2 Writable permissions to others

The first command will find the files that exactly match the permissions. The second command is finding files that do not have the mentioned permission, and the third command is finding files that have one or more of the mentioned permission bits.

How to Find Directories

To find a directory on Linux, the d flag is used with the type option. Let’s find the directories matching the pattern dir in the current directory using the find command.

find . -type d -name "dir*"

To find a directory in a specific directory, simply mention the directory path.

find MyDirectory -type d -name "dir*"

How to Find the Files and Delete

To find the files in a directory and to delete them, the -delete option is used. For example, to delete all the png files from the directory dir use the find command mentioned the directory and file type and then use -delete.

find dir -type f -name "*.png" -delete 

How to Find Files and Execute a Command

In many situations, you may need to apply the find filter and then execute a command. The find utility provides a -exec option to run a command over the find results.

For example, to perform the file command on the search results of the find command use the command given below:

find . -type f -exec file '{}' \;

In the above command the curly braces are a placeholder for the match result and are kept in the single quotes to avoid interpretation as shell script punctuation. In the same way, the semi-colon is protected, however, the single quotes can also be used in this case.

Similarly, to find files that contain the linuxhint word the grep command will be used.

find . -type f -exec grep -iRl "linuxhint" '{}' '\'

Here, the -i is used to make the search case-insensitive, -R for recursive, and -l to list only file names.

To find files and then perform a further action on them, the -exec option is used. For example, to find all the text files in a directory dir and change their permission to read-only for users, and groups use the find command with the -exec option to execute the chmod command.

find MyDirectory -type f -name "*.txt" -exec chmod 440 '{}' \;

How to Manage Search Depth

To manage the depth of the search into the directories using the find command, the -prune option is used. The -prune option essentially skips the mentioned directory while performing the search for the find command.

For example, to skip a dir2 in the dir1 for the search of all the files with the .pdf extension, the find command will be used with the -prune option.

find dir1 -type d -name "dir2" -prune -o -type f -name "*.pdf"

In the above command, the -o represents the logical OR, which separates the two alternative conditions.

The -prune option is useful for excluding specific directories, such as backups, to speed up the search process.

The find command processes the directories first and then the content by default. The -depth option is used to reverse the process. For example, if you want to process the content first and then the directories, mention the -depth option.

find dir1 -depth -type f -name "*.pdf"

The above command will find the text files of the deepest directory first. It can be useful to delete files from the deepest directory first. Other options for depth are listed below:

-maxdepth To define the maximum depth in the directory hierarchy for search; if 1 means search in the current directory only and no subdirectories
-mindepth To define the minimum depth in the directory hierarchy for search; if 2 means search starts from the second level directory

Conclusion

Looking through files and directories on Linux with the GUI is a simple task, but using the command line interface; it becomes challenging.

On Linux, the find command line utility is a powerful tool to search files and directories. The find is a versatile and powerful utility for searching files. It provides options to search files by name, type, modification time, and permissions. Moreover, it also provides options to operate on the output results through the other commands.

In this guide, I covered how to use the find command using various options and also mentioned how to find files and perform an operation on them.

Print Friendly, PDF & Email
Categories