Search for Files by Filename in Linux

To look for a file by its name in Linux, use the find command. It will recursively scan the directories on your system and return a list of matches found.


The find Command Syntax

The Linux find command syntax looks like this:


find [options] [path...] [expression]


  • options - this attribute controls optimisations, debugging and symbolic link handling options.
  • path - the directory to start searching from. If none is supplied the search will be from your current working directory.
  • expression - the name of the file to search for, this can contain RegEx for partial matches.


Find Files & Directories by Name

To use the Linux find command, pass the -name flag followed by the name of the file to look for on your system. find will begin a recursive search from your current working directory.


find -name "laravel.log"


Search in a Directory

To search in a specific directory on your system, pass the path to the directory like this:


find /var/www -name "laravel.log"


Search for Directories Only

The default behaviour of the find command is to return matching directories and files. To search for directories only, add the -type flag and set its value to d.


find /var/www -type d -name "storage"


Search for Files Only

To search for files only set the value of the -type flag to f.


find /var/www -type f -name "laravel.log"


To follow symbolic links, add the -L option at the start of the command.


find -L -name "laravel.log"


Using Regular Expressions

You will often need to match all files with a particular extension, or files starting with a specific phrase. Situations like this can be handled using find with regular expressions like this:


find /var/www -type f -name "*.js"


Here is another example matching part of a filename with a .js extension:


find /var/www -type f -name "web*.js"


The default behaviour of find is to be case-sensitive. To perform case-insensitive searches pass the expression to match after the -iname flag instead of -name.


find /var/www -type f -iname "WEB*.js"


Find Empty Files by Name in Linux

To find only empty files add the -empty flag to the find command.


find /var/www -type f -iname "*.js" -empty


Find Files Modified in the Last X Number of Days in Linux

To return files with a name that were modified in the last X number of days, use the -mtime flag on the find command and pass the number of days.


find /var/www -type f -iname "*.js" -mtime 7


Find and Delete Files by name in Linux

Caution - do not run this command unless you are absolutely sure it is going to do what you want it to.


To delete files by their filename, pass the -delete flag to the find command like this:


find /var/www -type f -iname "*.js" -delete