How to Find Files in Linux Using the Command Line
If you want to find a file on Linux, the find command is the one to go with. The find command searches for files and directories based on their permissions, type, ownership, date, size, and more. In this article, I will explain how to use the find command on Linux. I will add different use cases so you can better understand how to use the command in different cases for finding files.
find [options] [path] [expression]
options: It controls the behavior and optimization method.
path: It defines the starting directory where the commend begins searching for files and directories.
expression: It controls options, patterns and
At the minimum, the command takes the path to find things. For example, use the following command. It will find all the files on the system.
$ find /
Here is another example of the find command.
$ find -L /var/www -name "*.jpg"
-L tells the command to follow symbolic links. The command will search the entire directory tree beneath /var/www/ for files ending with the .jpg extension.
Find Files by Name
If you want to and files by name, you should use the -name option followed by the name of the file you want to search. Here is an example.
$ find /home/usethistip -type f -name sample.pdf
If you want to perform the case-insensitive search, you should replace the -name option with -iname. See the example below.
$ find /home/usethistip -type f -iname sample.pd
If you want to find all files in the home directory not named logo.jpg, use the following command.
$ find /home -not -name 'logo.jpg'
Find all directories called log:
$ find / -type d -name "log"
Find Files by Extension
If you just want to list all the files of a specific type, you can use the find command to search files by extensions. Here is an example.
$ find /home/usethistip -type f -name '*.pdf'
It will look for all PDF files in the mentioned directory.
Find by type
Now If you want to find files by type, you can do this using file command. Here are some of the common file descriptors.
- f – regular file
- d – directory
- l – symbolic link
- c – character devices
- b – block devices
Find files by size
You can also find files based on their size. You just need to add -size parameter along with the size criteria.
$ find /var/log -type f -size +100M
Here, M is used for MB. Here are the size descriptions.
- c – bytes
- k – Kilobytes
- M – Megabytes
- G – Gigabytes
- b – 512-byte blocks
It will search for files with a size greater than 100MB in the given directory. If you use – sign, it will look for files with a size less than 100MB. See the command below.
$ find /var/log -type f -size +100M
You can also look for files within a size range. Here is a command to find all files between size 10 and 20MB:
$ find . -type f -size +10M -size 20M
Find Files by Owner
If you want to find files owned by a user or a group. You can use the -user and -group options. Here is an example of finding files owned by the user usethistip.
$ find / -user usethistip
Find Files by Permissions
There is a -param option to search for files based on file permissions. If you want to look for all files with permissions of exactly 775 in var/www/ folder, use the command.
$ find /var/www -perm 775
Find Files by Modification Date
There is also an option to find files by modification date. Here is an example of looking for files in the /home/usethistip directory that were modified 30 or more days ago:
$ find /home/usethistip -mtime +30 -daystart
We have shown you different use cases of the find command. I hope this tutorial was easy to understand and gave you a clear idea of how to effectively look for a file or folder in Linux using the File command. For further reading, visit the find man page and read all the options.