ITEXPERT Linux

How to search files on Linux, using command options

Command ‘find’ is used to search for files and directories, Unix/Linux

find [OPTION…] [PATH] [EXPRESSION…]
OPTION
-P: Do not follow the symbolic link, use the symbolic link’s own information
-L: Use file information linked to symbolic link.
-H: Does not follow symbolic links, except when handling Command Line Argument.
-D: Output debugs messages.
EXPRESSION
-name: Search for files that match the specified string pattern.
-empty: Search for empty directories or zero-sized files.
-delete: Delete the searched file or directory.
-exec: Execute the specified command on the retrieved file.
-path: Search in the path corresponding to the specified string pattern.
-print: Print the search results. Search items are separated by a newline. (Default)
-print0: Print search results. Search items are separated by null.
-size: Search for files using file size.
-type: Search for files corresponding to the specified file type.
-mindepth: Specifies the minimum depth of subdirectories to start searching.
-maxdepth: Specifies the maximum depth of subdirectories to be searched.
-atime: Search files based on file access time.
-ctime: Search files based on the time of file content and property change.
-mtime: Search for files based on the data modification time of the file.

Combining “two or more expressions” via operator

Expression description
(expression) Specifies the priority of expression.
!expression
-not expression NOT operation on the result of expression.
expression -a expression
expression -and expression
expression expression AND operation between expressions.
expression -o expression
expression -or expression OR operation between expressions.

When two or more expressions are used and the operator is not specified, -a (AND) is applied by default (“-empty -print” = “-empty -a -print”)

An example of using the find command.

A simple search for a given name in the current directory. Specify filename after find command

$ ls
FILE_1 FILE_2 FILE_3
$ find FILE_1
FILE_1

$ find. -name “FILE”

find usage example command options
List of files and directories in the current directory find
List of files and directories in the target directory find [PATH] Search for files in all files and subdirectories under the current directory find. -name [FILE] Search for a file on the entire system (root directory) find / -name [FILE] Search for files whose filenames start with a specific string find. -name “STR*”
Search for files that contain a specific string in the file name find. -name “*STR*”
Search for files whose filename ends with a specific string find. -name “*STR”
Search for empty directories or zero-sized files find. -empty
Delete after searching all files with a specific extension find. -name “*.EXT” -delete
Print a list of searched files consecutively without line breaks find. -name [FILE] -print0
Search only for files or directories find. -name [FILE] -type f
Search for files using file size find. -size +[N]c -and -size -[M]c
Print detailed information about the searched file. (find + ls) find. -name [FILE] -exec ls -l {}
Output number of lines in the retrieved file. (find + wc) find. -name [FILE] -exec wc -l {}
Finding a string in the searched file. (find + grep) find. -name [FILE] -exec grep “STR” {}
Save search results to a file. (find, redirection) find. -name [FILE] > [SAVE_FILE] Do not display error messages during search (find, redirection) find. -name [FILE] 2> /dev/null
Don’t search subdirectories find. -maxdepth 1 -name [FILE] Copy retrieved files. (find + cp) find. -name [FILE] -exec cp {} [PATH]