Javatpoint Logo
Javatpoint Logo

Find Command in Linux/Unix with Examples

The find command helps us to find a particular file within a directory. It is used to find the list of files for the various conditions like permission, user ownership, modification, date/time, size, and more.

In Unix-like and other operating systems, the find command is a command-line utility that finds files on the basis of a few user-specified formats and either prints all matched object's pathname or, if other actions are requested, implements that action on all matched objects.

It starts a search from a wanted to start location and, after that, recursively traverses the directories (nodes) of a hierarchical structure (generally a tree). The find command can search and traverse from different file partition systems belonging to a single or more storage device under the starting directory.

The search format contains a pattern for matching against the filename or the time range for matching against the modification time or file access time. The find command provides a list of every file under the current working directory by default. However, users can restrict the search to a desired maximum level upon the starting directory.

The associated locate programs apply an indexed file database obtained from the fine command to give a faster technique for searching the whole file system by name.

The find utility comes by default with most of the Linux distros, so we don't need to install any additional package. It is one of the most essential and used commands of the Linux system.

The two options determine how find should consider symbolic links. The default nature is never to pursue symbolic links. The flag, i.e., -L, will lead find to pursue symbolic links. The flag, i.e., -H, will just pursue symbolic links while continuing with the command line arguments. The flags are mentioned in the POSIX standard for the find command. A basic extension is a flag, i.e., -P, to explicitly disable the symlink following.

At least a path must anticipate the expression. The find command can internally interpret wildcards, and commands must carefully be quoted to manage shell globbing.

Expression components are isolated by the command line argument boundary, generally indicated as whitespace within the shell syntax. They are considered from the left side to the right side. They can include logical components like OR and AND as well as predicates (actions and filters). GNU find contains several other features not mentioned by POSIX.

The following symbols are used to specify the directory:

(.) : For current directory name

(/) : For the root directory

Brief History of the Find Command

The find command occurred in Version 5 Unix as an element of the Programmer's Workbench project and was specified by Dick Haight with cpio, which were developed to be used together.

Originally, the GNU implementation of the find command was specified by Eric Decker. Later, it was developed by David MacKenzie, Tim Wood, and Jay Plett. Also, the find command has been ported to the IBM i OS.

Predicates

Widely used primaries are:

  • -name pattern: It checks that the file name is the same as the given shell-glob pattern or not.
  • -type type: It checks that the file is a provided type.
  • -print: It always gives the true value. It prints the current file name and a newline to stdout.
  • -print0: It always gives the true value. It prints the current file name and a null character to stdout. Not needed by POSIX.
  • -exec program [argument ...];: It always gives the true value. It executes a program with the fixed given arguments and the current file path.
  • -exec program [argument ...] { } +: It always gives the true value. It executes a program with the fixed given arguments and as several paths as possible. For almost every implementation, other {} occurrences mean extra copies of the given name (aspect not needed by POSIX).
  • -ok program [argument ...];: It is the same as -exec, but will return false or true if the program gives 0.

Find defaults to implementing -print in case the conditions are true if the expression utilizes none of -ok, -exec, -print, or -print0.

Operators

Operators enhance the find command expressions. They are mentioned in order of descending precedence:

  • ( expr ): This operator can force precedence.
  • ! expr: If expr returns false, it returns true.
  • expr1 expr2 (or expr1 -a expr2 : AND. expr2 isn't evaluated if expr1 is false.
  • expr1 -o expr2 : OR. expr2 isn't evaluated if expr1 is true.

Examples of the find Command

Let's see the following examples of the find command:

Find files by name

We can search all the files ending with the extension '.txt.' To do so, execute the below command:

The above command will list all the text files from the current working directory. Consider the below output:

Linux Find

From the above output, all the files having '.txt' extension are listed with the find command.

Finding files by type

The '-type' parameter is used to specify the file type.

Some of the file types are as follows:

  • f: regular file
  • d: directory
  • l: symbolic links
  • c: character devices
  • b: block devices

Consider the below command:

The above command will list all the directories having '.bak' extension. Consider the below output:

Linux Find

From the above output, the command "find . -type d -name "*.bak" is displaying all the directories ending with '.bak'.

Find newer files

The '-newer' parameter helps in searching the files which are newer than the mentioned file. Consider the below command:

The above command will display all the files which are newer than ' msg.txt' from the current working directory. Consider the below output:

Linux Find

From the above output, all the files displayed are newer than the 'msg.txt' file.

Find and delete a file

The '-delete' option is used to delete a specific file. We need to be very careful while using this command because there is no undo option if it is once executed. Consider the below command:

The above command will delete the file 'Demo.txt' from the current working directory. Consider the below output:

Linux Find

Find a directory

The ' type -d' option is used to find a directory. Consider the below command:

The above command will find the 'Newdirectory' location. Consider the below output:

Linux Find

Find files by modification time

The '-mtime' option, followed by the number of days, is used to find the files by modification. The number of days can be positive or negative. The negative value will be used for less than like -1 is used for the last day, and similarly, +1 will find the file for the more than one day ago. Consider the below command:

The above command will find the files which are modified within the last day. Consider the below output:

Linux Find

Find files by permission

The '-perm' option is used to find files by permission. Execute the find command with the '-perm' option and pass the required value. Consider the below command:

The above command will list the files from the specified directory that everyone could read, write, and execute.

Find and replace files

To find and replace files, we have to combine find command with the sed command. To operate on files, use the '-exec' option with the find command. Consider the below command:

from the above command, the specified occurrence will be replaced. Consider the below output:

Linux Find

Find text within multiple files

We can make another combination of the find command with the grep command to find the text from the various files. Consider the below command:

The above command will find the lines containing the text 'demo' from all the text files within the directory 'Newdirectory.' Consider the below output:

Linux Find

From the above output, we can see the lines having text 'demo' has been displayed.

Search every directory

The above command finds all directories for a regular file whose title is file1 and prints it on the screen. Generally, it's not a good idea to search for files this way. It can take a good amount of time. So, it's best to mention the directory. A few operating systems may mount file systems (dynamic) that are not favorable to the find command. More complicated file names containing characters unique to the shell may require to be closed in single quotes.


Next TopicLinux Locate





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA