Find Command in Linux/Unix with ExamplesThe 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 CommandThe 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. PredicatesWidely used primaries are:
Find defaults to implementing -print in case the conditions are true if the expression utilizes none of -ok, -exec, -print, or -print0. OperatorsOperators enhance the find command expressions. They are mentioned in order of descending precedence:
Examples of the find CommandLet's see the following examples of the find command:
Find files by nameWe 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: From the above output, all the files having '.txt' extension are listed with the find command. Finding files by typeThe '-type' parameter is used to specify the file type. Some of the file types are as follows:
Consider the below command: The above command will list all the directories having '.bak' extension. Consider the below output: From the above output, the command "find . -type d -name "*.bak" is displaying all the directories ending with '.bak'. Find newer filesThe '-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: From the above output, all the files displayed are newer than the 'msg.txt' file. Find and delete a fileThe '-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: Find a directoryThe ' 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: Find files by modification timeThe '-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: Find files by permissionThe '-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 filesTo 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: Find text within multiple filesWe 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: From the above output, we can see the lines having text 'demo' has been displayed. Search every directoryThe 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 |