How Does find -name Work?

In Linux, the find is the built-in command utilized for searching directories and files in the operating system. It comes with various features such as searching files by the parameter of name, extensions, file permissions, etc. Primarily, this command searches the file and the result can be used for processing, i.e, removing, renaming, or moving.

This article will present the detailed usage of the “find -name” command in Linux with the following outline:

How Does the find -name Work in Linux? 

The “find -name” command is used for searching the files/directories and performing a specific operation. The syntax for using the “find -name” command is given below:

Syntax:

$ find [Path] -name [File Name/Extension]

Type the find keyword, path, and file name/extension after the “name” option to search the files.

Let’s implement the working of the “find -name” using a few examples.

Example 1: Finding File By Name

To find the file by its name, type the path along with the file name as given in the below command. In our case, we are searching the “date.txt” file in the “/home/itslinuxfoss” path:

$ find /home/itslinuxfoss -name date.txt

All the occurrences of the file named “date.txt” are printed. 

Example 2: Finding File By Extension Name

To find the file by extensions, you can give an extension expression in the command. Here, the wildcard character “*” prints all file names with a particular extension, i,e., html:

$ find /home/itslinuxfoss -name '*.html'

One file which has the extension “.html” is printed alongside its complete path. 

Example 3: Finding File By Modification Time

To find the files by their modification time, the user can utilize the “mtime” flag in the command along with the times in days. The below command will search the given file modified in the last 5 days:

$ find /home/itslinuxfoss -name file.c -mtime -5

The “file.c” file has been modified in the last 5 days.

Example 4: Finding and Deleting the File

The user can also search and delete the particular files with the addition of the “delete” option at the end as given in the below-mentioned command:

$ find /home/itslinuxfoss -name test.txt -delete

The file will be searched and deleted.

Example 5: Finding Directory By Name

To find the directories by their name specify the “d” flag in the command and with the “name” parameter give the directory name. The below command will search the “Henry” directory in the home directory of the user:

$ find /home/itslinuxfoss -type d -name Henry

The “Henry” directory has been searched.

Conclusion 

The “find -name” is utilized for locating the files/directories by different parameter names, and extensions, or searching and deleting the particular file. The methods to find files and directories by their various patterns such as “name”, extensions, “modification time”, etc are given in the above guide. This post has uncovered the working of the specific command “find -name”. To gain in-depth knowledge of the “find” command, you can check our dedicated article on searching the files and directories through the find Command.