Linux find command: how to find files and directories
The find command in Linux looks through the specified directory and its sub-items to find files or directories that match conditions such as name, type, or size. Specify the search location and conditions together, and enclose file name patterns in quotes so that the shell does not expand them in advance.
Basic format and search scope
In the following format, the starting path is where the search begins, and the conditions limit what to find. The path itself is also included in the search target. The examples below are based on GNU findutils find and Bash.
find PATH EXPRESSION
find . explores the current directory and subdirectories, and find /etc starts from /etc. If no conditions are specified, it prints the paths and also explores hidden files and hidden directories. Directories without permissions may show access errors, and their contents will not be searched.
The next exercise creates files in a new temporary directory. You need to continue in the same terminal to keep the workdir variable. After printing the path to check the location, run the example.
workdir=$(mktemp -d)
mkdir -p "$workdir/docs" "$workdir/empty-dir"
touch "$workdir/docs/app.conf" "$workdir/docs/EXTRA.CONF" "$workdir/docs/notes.txt" "$workdir/.hidden.conf" "$workdir/empty.txt"
printf '%s\n' "$workdir"
Find by name and file type
-name matches with case sensitivity against the final path component, not the full path. -iname does not distinguish case. Single quotes are necessary to prevent the shell from expanding * first and pass it to find.
find "$workdir" -name '*.conf'
find "$workdir" -iname '*.conf'
The first command searches for app.conf and .hidden.conf, and the second command also finds EXTRA.CONF. The output order may vary depending on the environment. Typing find /etc *.conf has no name condition and may act unintentionally depending on the shell expansion, so avoid it. Instead, use find /etc -name '*.conf'.
-type f searches for regular files, -type d searches for directories. If conditions are combined, all must be satisfied.
find "$workdir" -type f -name '*.conf'
find "$workdir" -type d -name 'docs'
The first command outputs only regular files with matching extensions, the second outputs only directories named docs. By default, GNU find does not follow symbolic links into subdirectories. To search for the links themselves, use -type l.
Narrowing the range by depth and empty items
-maxdepth 1 searches only up to the starting path and the items directly below it. To exclude the starting path from the results, specify -mindepth 1 as well. It is recommended to place both options before conditions in GNU find.
find "$workdir" -mindepth 1 -maxdepth 1 -type d
In practice, docs and empty-dir immediately below will be displayed. -empty finds empty regular files and empty directories, so you can distinguish by specifying the type you want.
find "$workdir" -type f -empty
find "$workdir" -type d -empty
The files created with touch in the practice are all empty. In the second command, empty-dir appears. Do not directly consider the search results as targets for deletion; first, check the path and conditions.
Executing commands on found files
-exec executes commands on matching items. The following example outputs the byte count of regular files without deleting them. {} changes to the found path, and + groups multiple paths as much as possible to pass to the command.
find "$workdir" -type f -name '*.conf' -exec wc -c -- {} +
Since the practice files are empty, the byte count for each file is displayed as 0. wc can also display the total if multiple files are received. Even if there are spaces in the file names, each path is passed as a separate argument. To execute the command for each item separately, you can use \; instead of +.
Before combining delete or modify commands, first output only the paths under the same conditions and check the targets and backups. In directories where other users may change files during searching, the targets can change even after checking, so extra caution is needed. For more details, refer to GNU find security considerations.
When the results are different from expected
If there are no results, check the starting path, the case of letters in the name, the extension, and the file type in order. Also, check whether the pattern was enclosed in quotes, and whether the full path is needed instead of -name. When searching based on the full path, you can use -path.
If there is a permission error, it means there are areas that could not be searched. Check the necessary access permissions, and when using administrator privileges, carefully limit the search range and the actions to be executed. The detailed meanings of name patterns and options can be found in GNU find name pattern explanation.




