Find Files In Linux From Command Line

Linux is a very flexible operating system where 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 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 continue 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 are consist of different names that can contain letters, special characters, and numbers. We can search for files with the 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 it’s not mandatory using double quotes is very important and prevents errors.

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

The result will be listed like below with its full path.

/home/ismail/example.txt

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”.

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

The output will be like below.

/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

We can also search files according to their names end. In the following example we will search for files which end with “3.8”.

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

The output will be like below as we see different files ending with “3.8” will be listed.

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

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”, “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 below.

/home/ismail/.cache/tracker/db-version.txt
/home/ismail/.cache/tracker/db-locale.txt
/home/ismail/.cache/tracker/parser-version.txt
/home/ismail/.cache/tracker/first-index.txt
/home/ismail/.cache/tracker/locale-for-miner-apps.txt
/home/ismail/.cache/tracker/last-crawl.txt
/home/ismail/.mozilla/firefox/0g1wxsfn.default-release/AlternateServices.txt
/home/ismail/.mozilla/firefox/0g1wxsfn.default-release/SiteSecurityServiceState.txt
/home/ismail/.mozilla/firefox/0g1wxsfn.default-release/SecurityPreloadState.txt
/home/ismail/.mozilla/firefox/0g1wxsfn.default-release/pkcs11.txt
/home/ismail/.mozilla/firefox/0g1wxsfn.default-release/TRRBlacklist.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy-1.19.1.dist-info/top_level.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy-1.19.1.dist-info/entry_points.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy-1.19.1.dist-info/LICENSE.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy-1.19.1.dist-info/LICENSES_bundled.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy/core/include/numpy/multiarray_api.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy/core/include/numpy/ufunc_api.txt
/home/ismail/.local/lib/python3.8/site-packages/numpy/LICENSE.txt
/home/ismail/test.txt
/home/ismail/example.txt

You can also use following extensions to search different type of files according to their extensions.

#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 files in different location by provide the full path or absolute path like below. All sub-directories 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 link, character devices etc. While search a specific file can can be specified with the -type parameter according to the following file type table.

READ  How To Run "sudo" Command Without Password with NOPASSWD?
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 do not include directories, symbolic links, character 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 where 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, 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 those 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 reliable, 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 and 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 Owner

Another useful feature for the find command is finding files according to their owners. These can be user owner or group owner. The user owner can be specified with the -user option and group owner can be specified with the -group option. Lets 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 in order to find empty files. You can provide 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 contains 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 founded 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 founded files easily. This can be useful to delete temporary or backup files with a 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 via 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

Leave a Comment