How to Find Files on Linux

Sometimes we forget where we have put some files, or we are just simply looking for a configuration file. This guide will focus on how you can easily find files on Linux with the find command.

The Find Command

The find command is a really powerful command that allows us to easily find files and directories on Linux. This command is complex and allows you to find files and directories based on different factors such as:

  • Permissions
  • Date
  • Type
  • Size

Syntax

The syntax of the find command is:

find <options > <path> <expression>

Options

Options in general control the overall behaviour of the find command.

  • -H – Do not follow symbolic links.
  • -L – Follow symbolic links.
  • -P – Never follow symbolic links (default).
  • -D – Debug. Allows you specify debugging options, such as: tree, stat, opt, etc.
  • -O – Enables query optimisation.

Important! If more than one of -H-L and -P is specified, each overrides the others; the last one appearing on the command line takes effect. Since the -P option is the default, it should be considered to be in effect unless either -H or -L is specified.

How to Find Files on Linux

Find Files by Name on Linux

This is the most common way to find files, whether you are on Windows, macOS, or Linux. We are always trying to find files by their name.

To find files by their name on Linux we must use the basic syntax we provided above. In the following example we’ll be looking for a .pdf file on our user /home/ directory, we’ll specify the name of the file with the -name flag.

find /home/linuxify -type f -name contract.pdf
output

[linuxify@linuxify ~]$ find /home/linuxify/ -type f -name contract.pdf
/home/j/contract.pdf
[linuxify@linuxify ~]$

If your file name is case sensitive you must use the -iname flag instead of -name flag.

Find Files by Extension on Linux

To find all files containing an specific file extension we must include an * (asterisk) along with the file extension as the name:

find /home/linuxify/website/ -type f -name '*.php'

In our example above, we are listing all the files that have a .php extension on the /home/linuxify/website/ directory.

Exclude File Extension

In our example above, we were looking to find and list all the .php files. In this case, let’s say that we are looking to do the contrary. To exclude all .php files. To exclude files by their extension we must use the -not expression:

find /home/linuxify/website/ -type f -not -name '*.php'

Find Files by Size on Linux

To find files by their size use the -size flag. To find by byte-blocks, bytes, megabytes, gigabytes:

  • b – 512-byte blocks (default)
  • c – bytes
  • w – two-byte words
  • k – Kilobytes
  • M – Megabytes
  • G – Gigabytes

More than

To find files greater than the specified size we must include a + (plus) sign before the size. This indicated more than. To find files more than 10 Megabytes:

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

Less than

To find files less than a specified size we must include a - (minus) before the size. This indicates less than. To find files less than 10 Megabytes:

find /home/linuxify/ -type f -size -10M

Range

To find files between a specified range, we must include both sizes. In the following example we’ll find files that are within the range of 10 Megabytes and 20 Megabytes:

find /home/linuxify/ -type f -size +10M -size 20M

Find Files by Permissions

To find files by permissions, we must use the -perm option. On our example, we’ll all the files that have 644 permissions:

find /home/linuxify/ -perm /644

Find Files by Owner

To find all the files owned by a different user we have to use the -user option:

find / -user linuxify

Find Files by Modification Date

The find command also allows us to find files by their access, modification and change time. Similarly to find files by size, we can use the plus or minus sign to indicate greater or less than.

In our example we’ll find all files modified within the last (minus sign) 10 days and have a .php file extension:

find /var/www/linuxify.net/ -name "*.php" -mtime 10

Alternatively, you can use the plus sign to find files that were modified more than 10 days ago with the -daystart option:

find /var/www/linuxify.net/ -name "*.php" +mtime 10 -daystart

Find Files by Type

The find command allows you to find files by their type, whether its a regular file, directory, symlink, etc. To find files by their type, we have to use the -type option along with what we are looking for:

  • f – regular file
  • d – directory
  • l – symbolic link
  • c – character devices
  • b – block devices
  • p – named pipe (FIFO)
  • s – socket

For example, to find all directories in the current working directory:

find . -type d

Summary

In conclusion, this guide showed you how to find files on Linux based on their type, size, modification time or access time, owner, permissions, etc.

Leave a Comment