Searching for files is relatively easy when you are using a GUI. But in certain environments like GUI-less servers, you need to search for files using the command line.
There is a powerful command in Linux that helps you search for files and folders called find
. In this article, we will discuss the find
command with some examples.
What is the find command in Linux?
The find
command lets you efficiently search for files, folders, and character and block devices.
Below is the basic syntax of the find
command:
find /path/ -type f -name file-to-search
Where,
/path
is the path where file is expected to be found. This is the starting point to search files. The path can also be/
or.
which represent root and current directory, respectively.-type
represents the file descriptors. They can be any of the below:
f
– Regular file such as text files, images and hidden files.
d
– Directory. These are the folders under consideration.
l
– Symbolic link. Symbolic links point to files and are similar to shortcuts.
c
– Character devices. Files that are used to access character devices are called character device files. Drivers communicate with character devices by sending and receiving single characters (bytes, octets). Examples include keyboards, sound cards and mouse.
b
– Block devices. Files that are used to access block devices are called block device files. Drivers communicate with block devices by sending and receiving entire blocks of data. Examples include USB, CD-ROM
-name
is the name of the file type that you want to search.
Examples of the find command
Now we know the syntax of the find
command, let's look at some examples.
How to search files by name or extension
Suppose we need to find files that contain "style" in their name. We'll use this command:
find . -type f -name "style*"
Output

Now let's say we want to find files with a particular extension like .html
. We'll modify the command like this:
find . -type f -name "*.html"
Output

How to search hidden files
Hidden files are represented by a dot in the beginning of the filename. They are normally hidden, but can be viewed with ls -a
in the current directory.
We can modify the find
command as shown below to search for hidden files.
find . -type f -name ".*"
Output

How to search log files and configuration files
Log files usually have the extension .log
, and we can find them like this:
find . -type f -name "*.log"
Output

Similarly, we can search for configuration files like this:
find . -type f -name "*.conf"
How to search other files by type
We can search for character block files by providing c
to -type
:
find / -type c
Similarly, device block files can be found by using b
:
find / -type b
How to search directories
In the example below, we are finding the folders named lib
. Note that we are using -type d
.
find . -type d -name "lib*"
Output

💡 Tip: we can identify directories by looking at the d
flag in the output of ls -lrt
.

How to search files by size
An incredibly helpful use of the find
command is to list files based on a particular size.
find / -size +250MB
Other units include:
G
: GigaBytes.M
: MegaBytes.K
: KiloBytesc
: bytes.
Just replace <Unit Type> with the relevant unit.
find <directory> -type f -size +N<Unit Type>
How to search files by modification time
find /path -name "*.txt" -mtime -10
- -mtime +10 means you are looking for a file modified 10 days ago.
- -mtime -10 means less than 10 days.
- -mtime 10 If you skip + or – it means exactly 10 days.
Below are the contents of my home directory:

Let's apply an example in my home directory.
find . -type f -name ".*" -mtime +10

Practical examples of find
with bash scripts
We can combine find
with rm
or mv
to create meaningful bash scripts that can be automated.
Let's say we want to create a script that moves log files older than 7 days to a backup path. From there, it deletes log files older that older than 30 days. We can create a script and schedule it with cron
. You can learn more about cron
jobs here.
Let's view the script:
#!/bin/bash
# Script to move from logs older than 7 days to backup logs path: /app/backup_logs/ESB0*
# move ESB01 logs to backup
find /logs/esb01/audit -name "*.tar.gz" -mtime +7 -exec mv {} app/backup_logs/ESB01/ \;
# Remove logs from backup path after 30 days
find /app/backup_logs/ESB01 -name "*.tar.gz" -mtime +30 -exec rm {} \;
Note that we are using exec
with find
. Basically, exec
executes the command provided ( mv
and rm
in our case). {}
is the placeholder which holds the results of the command. Lastly, we provide the delimiter ;
. As we do not want the shell to interpret the semicolon, we escape it with \
.
The shared script is very useful in archiving and removing logs.
Wrapping up
In this article, we have studied the find
command in detail and learned how to search files by name, type, size and modification time.
I hope you found this tutorial helpful.
Share your thoughts on Twitter!
You can read my other posts here.
Resources: Banner images from Office illustrations by Storyset and Canva.