How to Find Files Using the Command Line in Linux
Being able to find files using the command-line will give you a whole new level of control while managing your Linux system.
Today we are going to be learning about the find
command, which provides many options for searching for files and directories in Linux based on their intrinsic properties. You can also use the find
command in other Unix-like operating systems such as macOS.
Find Command Syntax
Firstly let's have a look at the syntax of the find
command.
find [options] [path ...] [expression]
Options
- a bunch of interchangeable flags that can be optionally added to change the behaviour of the command including whether to search symbolic links.Path
- the top-level directory from which the find command will begin searching through.Expression
- an expression used to match files. This can contain a regular expression.
Basic Usage
The most basic way to use the find
command to search for everything in the current working directory.
find .
The .
(dot) character tells find
we want to search from the current working directory.
The output will be a list of all files and directories including their paths relative to your current directory:
./database/languages.sql
./database/seeds
./database/seeds/NavigationTableSeeder.php
./database/seeds/DatabaseSeeder.php
./database/seeds/UserTableSeeder.php
./database/timezonedb.sql
./.env
./package_old.json
Common Find Expression Flags
Let's have a look at some of the optional expression flags we can apply to the end of the find command to match files and directories.
-not
- returns results that do not match the expression.-type f
- search for files.-type d
- search for directories.-type l
- search for symbolic links.-type c
- search for character devices.-type b
- search for block devices.-type p
- search for named pipes.-type s
- search for sockets.-name
- search by name.-iname
- case insensitive search.-mtime N
- files modified in the lastN
days.-maxdepth N
- The maximum number of levels including the current directory to search down defined byN
.
Using one of the above examples we could search only for directories in the current directory by passing in the -type d
flag.
find . -type d
Find Files by Name
Most people will want to use the find
command to search for files by their name. Let's search a directory for files that are called controller.php
with the -iname
flag because we want to match files with both upper and lower case letters.
find /var/www/skillsugar.com/ -type f -iname controller.php
Matches of controller.php
Controller.php
CONTROLLER.PHP
and other case variations will be returned.
If you only want to match files that have the exact casing use -name
instead.
find /var/www/skillsugar.com/ -type f -name controller.php
Find Files by Date Modified
Another useful function is finding files that have been modified recently. This can be done using the -mtime
flag which takes a numerical value of days. In the following command we will search a directory for files that have been modified in the last 7 days.
find /var/www/skillsugar.com/html/ -type f -mtime 7
Find Files by Extension
To search for files only containing a certain extension we will use the -name
flag, though we will add the *
(asterisk) regular expression to match everything that has a specific extension.
find /var/www/skillsugar.com/html/ -type f -name '*.php'
You must put regular expressions in single or double quotes to stop them from getting interpreted by the shell rather than find
.
Find Files by Size
You can use find to search for files that are an extract size or greater than. This can be very helpful if you need to look for large files that are taking up unnecessary space on your system.
To search for files by size we will pass the -size
flag into our command followed by a numerical size value and finally the size format we intend to look for.
find /var/www/skillsugar.com/html/ -type f -size 1024c
By default -size
uses byte-blocks to match the file size though we can use any one of the following when looking for files:
b
- byte blocksc
- bytesw
- two-byte wordsk
- kilobytesM
- MegabytesG
- Gigabytes
If we wanted to find all files that are exactly 5 Megabytes in size in the current directory we could use the following command:
find . -type f -size 5M
Files with Size Greater Than
Files that are larger than a specified size can be found by putting a +
(plus) directly before the size value.
find . -type f -size 5M
Files with Size Less Than
Files that are larger than a specified size can be found by putting a -
(dash) directly before the size value.
find . -type f -size -5M
Files with Size Within Range
To find files in a size range, pass two size flags, the first with a greater than minimum value and secondly a less than maximum value.
find . -type f -size +2M -size -5M
Find Files by Owner
You can search for files that are owned by a specific user by passing in -user
followed by the name of the user.
find . -type f -user skillsugar
Find and Delete Files
To delete files using find
you can pass in the -delete
flag.
Warning - never put the -delete
flag on a command unless you have tested the command without the -delete
flag and observed it is doing what you intend it to. If you delete the wrong files they are gone forever.
find /var/www/skillsugar/trash.bin -delete
Modify Files with the Find Command
If you would like to modify files using find you can do so using the -exec
flag. In -exec
we can run any utility we wish.
In the following command we will search for all files in a folder and change the owner to www-data
:
find /var/www/skillsugar/storage -name '*' -exec chown nginx {} \;
Combine Find Command with Grep to Search Content of File
find
will only allow us to search for a file based on its properties. If we want to check the contents of files for matches we can use grep in the -exec
flag. Let's say we wanted to find all files in a directory with the extension .log
that contain the phrase “skillsugar.com”. We could use the following command to print out their contents:
find /var/www/skillsugar/storage -name '*.log' -exec grep "skillsugar.com" {} \;
Conclusion
You now have a comprehensive overview of how to use the find
command to search your file system in Linux. Over time remembering the different flags and how to combine them to achieve what you want will become second nature.