Find command in Linux with examples. Use find command in Linux to search a file or search a string. Find is the Linux command to find a file in a directory and subdirectory. See how:
find command
find command is used to locate a specific file by name or extension. ‘find’ searches the directory tree rooted at each file name FILE by evaluating the EXPRESSION on each file it finds in the tree. The command line may begin with the ‘-H’, ‘-L’, ‘-P’, ‘-D’ and ‘-O’ options. These are followed by a list of files or directories that should be searched. If no files to search are specified, the current directory (‘.’) is used.
This list of files to search is followed by a list of expressions describing the files we wish to search for. The first part of the expression is recognised by the fact that it begins with ‘-‘ followed by some other letters (for example ‘-print’), or is either ‘(‘ or ‘!’. Any arguments after it are the rest of the expression.
Syntax of find Command
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]
Options of find Command
The -H, -L and -P options control the treatment of symbolic links. Arguments following these are taken to be names of files or directories to be examined, up to the first argument that begins with “-“, or the argument “(” or “!”. If no paths are given, the current directory is used. If no expression is given, the expression -print is used.
The five “real” options -H, -L, -P, -D and -O must appear before the first path name in the find command argument.
1. -P : This is the default and it will never follow symbolic links.
2. -L : Follow symbolic links. When find examines or prints information about files. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself.
3. -H : Do not follow symbolic links, except while processing the command line arguments.
Note: If more than one of -H, -L and -P is specified, each overrides the others; the last one appearing on the command line takes effect. Since it is the default, the -P option should be considered to be in effect unless either -H or -L is specified.
Debug Options for find Command
-D debugoptions : Print diagnostic information; this can be helpful to diagnose problems with why find is not doing what you want. Debug options are:
1. help : Explain the debugging options.
2. tree : Show the expression tree in its original and optimised form.
3. stat : Print messages as files are examined with the stat and lstat system calls. The find program tries to minimize such calls.
4. opt : Prints diagnostic information relating to the optimisation of the expression tree; see the -O option.
5. rates : Prints a summary indicating how often each predicate succeeded or failed.
Examples of find Command
find . -name testfile.txt
Find a file called testfile.txt in current and sub-directories.
find /home -name *.jpg
Find all .jpg files in the /home and sub-directories.
find . -type f -empty
Find an empty file within the current directory.
find /home -user exampleuser -mtime 7 -iname ".db"
Find all .db files (ignoring text case) modified in the last 7 days by a user named exampleuser.
Some More Examples
find . -name sourcedigit.txt
Find Files Using Name in Current Directory
find /home -name sourcedigit.txt
Find Files Under Home Directory
find / -type d -name SD
Find Directories Using Name – Find all directories whose name is SD in / directory.
find . -type f -name sourcedigit.html
Find Files of Specific Extension Using Name of the File. Find all html files whose name is sourcedigit.html in a current working directory.