Find Files In Linux From Command Line

Linux is a very flexible operating system that provides different commands and ways to find files. As a command-line-based operating system, Linux provides a very powerful command named find in order to find files according to the different aspects. In this tutorial, we will examine how to find files according to their names, sizes, locations, attributes, etc. easily with the find command.

find Command Syntax

find command has the following syntax where the SEARCH_LOCATION and SEARCH_TERM is a must.

find OPTION SEARCH_LOCATION SEARCH_TERM
  • OPTION is used to specify different search types like size, owner, permission, etc.
  • SEARCH_LOCATION is the start path of the search and continues to the sub-directories.
  • SEARCH_TERM is the file name or extension or term which will search for in the file names.

Find Files According To Name

The most popular case to find a file is by searching according to its name. Files consist of different names that can contain letters, special characters, and numbers. We can search for files with complete names or partial names. In order to search according to name, the -name option should be used. In the following example, we will search the file with the name example.txt in the path “/home/ismail/”. Even if it’s not mandatory using double quotes is very important and prevents errors.

$ find /home/ismail -name "example.txt"

The result will be listed below with its full path.

/home/ismail/example.txt

Find According To Start of The Pattern

We can also provide only the start of the file name and list files starting with the specified characters. In the following example, we will search for files starting with “python”. We will use the search term “*python” where the * means any characters.

$ find /home/ismail -name "python*"

The output will be like the below. Every search result file name contains the term “python”.

/home/ismail/snap/notepad-plus-plus/238/notepad-plus-plus/autoCompletion/python.xml
/home/ismail/.local/lib/python3.8
/home/ismail/.local/lib/python3.8/site-packages/numpy/lib/tests/data/python3.npy

Find File Name Pattern

We can also search for files according to the end of their names. In the following example, we will search for files that end with “3.8”. We will use “*3.8”.

$ find /home/ismail -name "*3.8"

The output will be like the below as we see different files ending with “3.8” will be listed. Even the start of the file name is different every listed file ends with “3.8”.

/home/ismail/.local/lib/python3.8
/home/ismail/.local/bin/f2py3.8

Find Case Insensitive

find command searches case sensitive by default. This means the uppercase letter is different from the lowercase letter. You can disable case sensitivity with the -iname parameter. In the following example, we will search files case-insensitive manner. This means “example.txt”, “Example.txt”, and “EXAMPLE.txt” will match for the following search.

$ find /etc -iname "example.txt"

Find Files According To Extension

Extensions are used to express the file types or data inside the file. For example “*.txt” is used for the text files and added after the file name. “*.pdf” is used for PDF files, “*.doc” for Word files, etc. find command can be used to search files according to their extensions. We will use the -name option and only provide the file extension. In the following example, we will list text files with the “*.txt” extension.

$ find /home/ismail -name "*.txt"

The output will be like the below.

Find Files According To Extension

We can also use the following extensions to search different types of files according to their extensions. In the following example, we will search file types and extensions like Word File (*.doc), PDF File (*.pdf), PowerPoint File (*.ppt), Windows Executable File (*.exe), and Temporary File (*.tmp).

#Word File
$ find /home/ismail -name "*.doc"

#PDF File
$ find /home/ismail -name "*.pdf"

#Power Point File
$ find /home/ismail -name "*.ppt"

#Windows Executable File
$ find /home/ismail -name "*.exe"

#Temporary File
$ find /home/ismail -name "*.tmp"

Find Files According To Location

Up to now, we have searched files located in the /home/ismail path. But we can search for files in different locations by providing the full path or absolute path like below. All subdirectories of the given path will be also searched.

$ find /etc -name "example.txt"

$ find /var -name "example.txt"

$ find /home -name "example.txt"

Find Files According To Type

Everything in a Linux operating system is a file. Files can be in different types like symbolic links, character devices, etc. While searching a specific file can be specified with the -type parameter according to the following file type table. By default, the find command searches and lists all file types.

File TypeDescription
fA regular file like text, data, binary, etc.
dDirectory
lSymbolic link to another file or directory
cCharacter devices
bBlock devices like hard drives, storage devices, etc.
sSocket
pNamed pipe

Now we will search only regular files and not include directories, symbolic links, characters, or block devices by using -type f .

$ find /etc -type f -name "example.txt"

Find Files According To Size

Files can be in different sizes and sizes are generally related to the file type. We can search for files according to their size which will very helpful to filter unrelated results. The size can be specified with the -size option and size value in a megabyte, or kilobyte. Use the following size descriptors to define size.

Size Description
cbytes
kkilobytes
Mmegabytes
Ggigabytes
b512-byte blocks

In the following example, we will search for files whose sizes are higher than 1000MB.

$ find /etc -size +1000M -name "example.txt"

We can also search files those sizes are lower than 1000MB with the -size -1000MB .

$ find /etc -size -1000M -name "example.txt"

Find Files According To Modification, Access and Change Date

Files can be searched according to their modification, access, and change time. But in order to complete this operation reliably, the file system should set these modifications, access, and change times accordingly. The -mtime option is used to specify the time information. The + plus sign is used to express before that time and - minus sign is used to express after that time.

In the following example, we will search *.txt file which is modified 10 days before.

$ find / -mtime +10 -daystart -name "*.txt"

In the following example, we will search *.txt extension which is modified 10 days and before.

$ find / -mtime 10 -name "*.txt"

Find Files According To the Owner

Another useful feature of the find command is finding files according to their owners. These can be user owners or group owners. The user owner can be specified with the -user option and the group owner can be specified with the -group option. Let’s search files owned by the user ismail in the following example.

$ find / -user ismail

$ find / -user ismail -name "*.cfg"

We can also search for group owner with the following command where files those group is ismail will be listed.

$ find / -group ismail

$ find / -group ismail -name "*.txt"

Find Files According To Permissions

In Linux, every file has permission in order to regulate access to the file. The find command can be used to search files according to their current permission. The -perm option is used to specify the permission like 775.

$ find /var/ww -perm 644 -name "*.php"

Find Empty Files

The find command provides the -empty option to find empty files. You can provide the file name or not in order to match empty files.

$ find /home/ismail -empty

Or we can find specific file types like *.txt files which are empty and do not contain any data.

$ find /home/ismail -empty -name "*.txt"

Run Command On The Founded Files

find is a very advanced command where you can run or execute different commands on the files you found. The -exec option is used to run specified commands in the search results. {} is used to express the search result or found file. Also \; is added end of the command to prevent errors. In the following example, we will search for text files and print their contents to the terminal. While using the {} it will be replaced with the file names in the background so to prevent errors especially file names with spaces using a single or double quote will be very useful.

$ find . -name "*.txt" -exec cat '{}' \;

Find and Delete Files

The find command also provides the ability to delete found files easily. This can be useful to delete temporary or backup files with little effort. The -delete option will be used to delete matched files. While using this option be careful.

$ find . -name "*.txt" -delete

$ find . -name "*.bak" -delete

$ find . -name "*.tmp" -delete

Put Search Results into A File

You may want to store the search result and use it later or send others. This can be done by putting or redirecting the search results into a file. We will just redirect the output into a file named find.txt .

$ find . -name "*.txt" > find.txt

Find Files Remotely

Leave a Comment