Finding Files On Linux Using The Find Command

The find command is an incredibly powerful and flexible tool that is crucial in administrating your Linux system.

You can use the find tool to search for files and directories within your system using a specified expression.

You can even search for files based on their specified properties. For example, you can search for a file based on the permissions that have been set to it, or the owner, or file size.

Even more interesting, you can use find on Linux to execute actions on every file that your expression matches against.

This guide will walk you through some of the various ways to use the find tool on your Linux system.



Finding Files in Linux Using the Find Command

The find command is an essential tool in any Linux user’s arsenal, allowing for efficient file searching based on various criteria. This article will guide you through its syntax and usage, providing examples to help you master file searching in Linux.

Syntax of the Find Command

The basic syntax of the find command is:

find [path] [options] [expression]
  • [path] specifies the directory to search.
  • [options] are global options that affect the overall operation.
  • [expression] defines what to search for and what to do with the results.

The syntax for the find tool is quite simple, the most important things to focus on are the path and the expression. The expression is how you control what the tool funds in the directory.

It is important to note, the order of your expression does matter. If you wanted to execute a command, it should be at the end of the expression to ensure a match event occurs.

using the linux find command

Basic Usage of Find on Linux

The simplest and most basic way to use the find tool is to specify only the directory that you want it to search through.

find "directory"

When using this command, replace "directory" with the directory you want to search.

When using this command, the tool will list every file and object in the directory. There are not many times you will want to do this, and there are better commands to use that we will get in to.

Example of Using Find on a Directory

To search a directory located at "/home/raspians/example/", we would use the following command:

find /home/raspians/example/

This will give you a complete list of all the files and folders in this directory.


You Might Like: Finding Files On Ubuntu Using Terminal


Filtering the File Type with Find

  find "directory" -type "filetype"

Using this command, you can filter the type of file that will be returned. To search only for files or directories, use the "-type" option, and a letter that is used to designate the file type.

This list gives you the file types you can specify, and the letter that represents that file type. The types you are most likely to use are “r” Regular Files and “d” Directories.

  • d – Directory
  • f – Regular File
  • p – Named Pipe
  • l – Symbolic Link
  • c – Character
  • b – Block
  • s – Socket
  • D – Door

Only Searching for Files

As an example, we will use the -type option to list the files (f) in our directory.

  find /path/to/directory -type f

This will return all the files inside your chosen directory.



Searching for Directories

 This time we will use the same command as our last example, but we will search for directories instead of searching for files.

This means that we will be using the letter “d“, instead of using the “f”  letter alongside the “-type” option.

  find /path/to/directory -type d

Now you will see all the directories instead of the individual files.

finding files on linux using the find command

Finding Files on Linux by Name

This is the most common way that you will use the find tool. To search for files by name using the the find tool, you need to use the the “-name” option alongside the "find" command.

Using the “-name” option will perform a case-sensitive search, to perform a non case-sensitive seach you would use the “-iname” option.

  • Performing a Case Sensitive Search
  find /path/to/directory -iname filename
  • Performing a Case Insensitive Search
  find /path/to/directory -iname filename

Using a Wildcard to Perform a Search

The find tool on Linux lets you to use wildcards (*) within the filename. This comes in handy when you want to find files by their file extension.

In this example, we will find all the files within our example that are a “.txt“. To do this search, we will use the asterisk (*) followed by “.txt“.

  find /path/to/directory -type f -name *.txt

You Might Like: What Is The Default Raspbian Username And Password?


Using Find to Search by Size

The find command in Linux also allows you to locate files based on their size using the -size option. This option is straightforward to use and can be combined with other options for more precise searches.

To search for files of a specific size, use the following syntax:

find DIRECTORY -size FILESIZE

The -size option supports various suffixes to specify the size unit, eliminating the need to provide the size in bytes only:

  • b – 512-byte blocks (default if no suffix is specified)
  • c – Bytes
  • w – Two-byte blocks
  • k – Kilobytes
  • M – Megabytes
  • G – Gigabytes

To search for files larger or smaller than a specified size, prepend the size value with a plus sign (+) for greater than or a minus sign (-) for less than:

find DIRECTORY -size +10M

– Finds files larger than 10 megabytes.

find DIRECTORY -size -10M

– Finds files smaller than 10 megabytes.

Finding Files On Linux Using The Find Command

Finding FIles in a Size Range

You can combine multiple size parameters to search for files within a specific size range.

For instance, to locate all files between 12 Megabytes and 22 Megabytes, you would use two size options:

  • The first option represents the minimum size. In our case, this would be +12M.
  • The second option indicates the maximum size. For our example, this would be -22M.

The command would look like this:

find DIRECTORY -size +12M -size -22M

You Might Like: Top 5 Programming Languages For Your Raspberry Pi


Finding Files by their Modification, Access, or Change Time

The find command in Linux offers the capability to search for files based on their last modified, accessed, or changed times.

In Linux, each file has three different timestamps that record the last time an operation was performed on the file. The options to check for these operations are as follows:

  • -atime: This option allows you to find files based on their last access time. A file is considered accessed whenever its contents are read.
  • -mtime: Use this option to search for files based on when they were last modified. Linux considers files modified when their contents have been changed.
  • -ctime: This option helps you find files based on when the file’s attributes were modified. The change time should not be confused with the modification time. A file is considered changed when its metadata, such as permissions, have been modified.

The basic syntax for these options is as follows, where the value represents the exact number of days since the file was modified, changed, or accessed:

find DIRECTORY -mtime DAYSSINCEMODIFIED

These options also support the less than (-) and greater than (+) operators.



Finding Files Modified on a Specific Day

To search for files based on their modification date, simply specify the exact number of days since the modification occurred.

For instance, to find a file modified exactly 7 days ago, use the -mtime option with the number 7:

find /home/raspians/example -mtime 7

This command will scan the specified directory for any file with a modification date of 7 days ago.

Identifying Files Modified After a Certain Date

To locate files modified after a specific number of days, prepend the number of days with a plus sign (+). For example, to find files modified more than five days ago, use +5:

find /home/raspians/example -mtime +5

This will search for all files in the specified directory that have been modified later than five days ago.

Locating Files Modified Before a Certain Date

Similarly, to find files modified before a certain number of days, use the minus sign (-). For instance, to find files modified within the last 4 days, use -4:

find /home/raspians/example -mtime -4

Searching for Files Modified Within a Date Range

The find command allows you to combine options to search for files modified within a specific date range. For example, to find files modified between 12 and 30 days ago, use both -mtime +12 and -mtime -30:

find /home/raspians/example -mtime +12 -mtime -30

This command will locate files that were modified later than 12 days ago but before 30 days ago.


You Might Like: 30 Commands For Your Raspberry Pi


Search for Files by Owner

The find command allows you to locate files on your Linux system based on their ownership.

To find files owned by a specific user, use the -user option followed by the username:

find DIRECTORYNAME -user USERNAME

Similarly, to find files owned by a specific group, use the -group option followed by the group name:

find DIRECTORYNAME -group GROUPNAME

Search for Files with Specific Permissions

You can also use the find command to search for files with certain permissions.

This can be particularly useful for security purposes, such as ensuring that no files or folders have been set to overly permissive settings like 777.

To search for files with specific permissions, use the -perm option followed by the numerical permission value:

find DIRECTORY -perm NUMERICALPERMISSION

For more detailed control, you can prefix the permission value with one of two operators:

  • The forward slash (/) indicates that any of the specified permission bits must be set for a file to match:
  find DIRECTORY -perm /NUMERICALPERMISSION
  • The minus symbol (-) indicates that at least the specified permission bits must be set:
  find DIRECTORY -perm -NUMERICALPERMISSION

For example, searching with -perm -111 will match files where all permission groups have the execute permission.

Executing Commands on Found Files

One of the most powerful capabilities of the find command is the ability to execute a command on each file it finds.

To do this, use the -exec option followed by the command you wish to execute, the placeholder for the results, and the delimiter:

find DIRECTORY -exec COMMAND {} DELIMITER


Results Placeholder

To include the full pathname of each found file in the command you’re executing, use curly brackets ({}). The find command will replace these brackets with the pathname of each file found. You can use this placeholder multiple times within a single command.

For instance, to change the permissions of all files in a directory to 644, you can use the following command:

find /home/raspians-type f -exec chmod 644 {} \;

In this example, the curly brackets are replaced with the pathname of each file found.

Delimiters

The -exec option in the find command requires a delimiter to indicate the end of the command and how to process the results.

There are two types of delimiters you can use, each with its own use case:

The Semicolon Delimiter

The semicolon (;) is used to execute the command for each file found separately. It must be escaped with a backslash (\) to prevent the shell from interpreting it as a command delimiter.

For example, to echo the pathname of every file found, use the following command:

find /home/raspians/example -type f -exec echo {} \;

This command executes the echo command for each file individually, displaying the pathname of each file.

The Plus Sign Delimiter

The plus sign (+) is an alternative delimiter that tells the find command to concatenate all the results before executing the command. This means the command is executed only once, with a list of all found files separated by spaces.

For example, to echo the pathnames of all found files in one go, use the plus sign delimiter:

find /home/raspians/example -type f -exec echo {} +

This command differs from the semicolon delimiter in that it combines all the results into a single execution of the echo command.


You Might Like: Introducing The Raspberry Pi 5


Conclusion

The find command is a powerful tool for searching files in Linux based on various criteria. By mastering its syntax and options, you can efficiently locate files and directories, filter by type, size, and modification time, and even execute commands on the found files.

With practice, you’ll find that find becomes an indispensable part of your Linux toolkit.

If you prefer a video explainer, Learn Linux TV has put together a great resourc that I’ve shared below.


Erik D

Leave a Comment

Your email address will not be published. Required fields are marked *