In this article we are going to shows how to find Files and Directories in Linux Terminal using find command. If you want to learn about find command in Linux with Examples then this post is ideal for you.
The find
command is a powerful and one of the most used command-line utility in the Linux system that is used to search or locates the files and directories based on a user-specified expression and then applies user requested action on each matched file.
The find
command also allows you to search the files and directories based on their type, date, ownership, size, permissions and many more.
How to Find Files in Linux Terminal
In the Linux, the basic syntax of the find command command is show as below:
find [options] [path...] [expression]
Here:
options
– Treat the symbolic links, debugging options, and optimization method.path…
– Defines the directory or directories where find command will search the files.expression
– Defines the search patterns, options and actions separated by operators.
For an Example:
find -L /var/www -name "*.html"
- The
-L
(options) – It tells thefind command
to follow symbolic links. - The
/var/ww
w
(path…) – Specifies the directory wherefind command
will search the given files. - The
-name "*.js
(expression) – It tells thefind command
for searching files ending with.html
.
Find Files by Name
If you want to find the file by name, use the -name
option with the name of the file that you want to search:
find /home/studiesonline -type f -name articles.pdf
The command above will search for a file named articles.pdf
in the /home/studiesonline
directory.
To search the file by name and ignoring the case-sensitive, replace the -name
option with -iname
as show below:
find /home/studiesonline -type f -iname articles.pdf
Find Files by Name in Current Directory
To find the file in a current working directory, run the command below:
find . -name filename.txt
Find Files by Extension
If you want to find the file by extension, run the command below:
find /home/studiesonline -type f -name '*.pdf'
The command above will find all the files ending with .pdf
in the /home/studiesonline
directory.
You can reverse the above expression by adding -not
option. For an example, if you want to find all files that don’t ending with *.
pdf. To do that run the command below:
find /home/studiesonline -type f -not -name '*.pdf'
Find Files by Type
If you want to search files by their specific file types like directories, regular files or symlinks, use the -type
option with the below descriptors to specify the file type:
f
: regular filed
: directoryl
: symbolic linkc
: character devicesb
: block devicesp
: named pipe (FIFO)s
: socket
For an example, if you want to find all the regular file in the current working directory , run the command below:
find . -type d
Find Files by Size
If you want to search files by size, use -size
option with the size criteria. Below are the suffixes to specify the file size:
b
: 512-byte blocks (default)c
: bytesw
: two-byte wordsk
: KilobytesM
: MegabytesG
: Gigabytes
Run the command below to find all files of exactly 5M
from the
directory:/home/studiesonline/data
find /home/studiesonline/data -type f -size 5M
If you want to search for all files which are greater than 1M
and less than to 3M
(within a size range) from the current working directory. To do that run the command below:
find . -type f -size +2M -size -3M
To search for all files which are less than 2MB
inside the current working directory, run the command below:
find . -type f -size -2M
Same as above, if you want to search for all files which are greater than 2MB
inside the current working directory, run the command below:
find . -type f -size +2M
Find Files by Modification Date
The find command also allows you to search the files by their last modification, access, or change time. To search for all .html files inside the /var/www/html directory which has been modified in the last 20 days, run the command below:
find /var/www/html -name "*.html" -mtime 20
Another example using -daystart
option:
find /home/studiesonline -mtime +20 -daystart
The command above will display the all files from the /home/studiesonline
directory which were modified 20 or more days ago.
Find Files by Owner
If you want to search for the files which are owned by a particular user or group. To do that, use the -user
and -group
options with the find command as show below:
find / -user studiesonline
The command above will search for all files and directories that are owned by the user studiesonline
.
Find and Delete Files
You can also delete the all matched files using the -delete
option at the end of the match expression:
find /var/www/html/temp/ -name '*.tmp' -delete
The command above will delete all the files ending with .tmp
from the /var/www/html/temp
directory.
That’s all.
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.