How to use the command 'find' (with examples)
The ‘find’ command is a powerful tool for searching files or directories under a given directory tree, recursively. It supports various options and filters to help you find specific files or directories based on criteria such as name, size, modification time, etc.
Use case 1: Find files by extension
Code:
find root_path -name '*.ext'
Motivation: You may want to find all files with a specific file extension in a directory and its subdirectories. This can be useful when you need to work with a particular type of file or perform operations on them.
Explanation:
root_path
: The directory path where the search should start.-name '*.ext'
: A filter to match files with the given file extension. The*
is a wildcard that matches any characters, and.
is used to match the literal dot in the file extension.
Example output:
root_path/file1.ext
root_path/subdir1/file2.ext
root_path/subdir2/file3.ext
Use case 2: Find files matching multiple path/name patterns
Code:
find root_path -path '**/path/**/*.ext' -or -name '*pattern*'
Motivation: Sometimes, you may need to search for files that match multiple patterns in their path or name. This can be useful when you want to find files in specific directory structures or files containing specific keywords.
Explanation:
-path '**/path/**/*.ext'
: A pattern to match files that are located in any directory structure containing “path” and have a file extension of “ext”.-or
: An operator to specify an alternative search pattern.-name '*pattern*'
: A pattern to match files that have “pattern” anywhere in their name.
Example output:
root_path/path1/subdir/file1.ext
root_path/path2/subdir/file2_pattern.ext
Use case 3: Find directories matching a given name, in case-insensitive mode
Code:
find root_path -type d -iname '*lib*'
Motivation: You may need to find directories that match a specific name, regardless of case sensitivity. This can be useful when you want to locate directories that contain particular words or components in their names.
Explanation:
-type d
: Only search for directories.-iname '*lib*'
: A case-insensitive pattern to match directories containing the word “lib” in their names.
Example output:
root_path/libraries
root_path/Plugins/LIBRARY
Use case 4: Find files matching a given pattern, excluding specific paths
Code:
find root_path -name '*.py' -not -path '*/site-packages/*'
Motivation: Sometimes, you may want to search for files that match a particular pattern but exclude certain paths from the search. This can be useful when you need to work with files in a specific context and ignore files from certain directories.
Explanation:
-not
: Negates the following expression.-name '*.py'
: A pattern to match files ending with “.py”.-path '*/site-packages/*'
: A pattern to exclude files in the “site-packages” directory or its subdirectories.
Example output:
root_path/file1.py
root_path/subdir1/file2.py
Use case 5: Find files matching a given size range, limiting the recursive depth to “1”
Code:
find root_path -maxdepth 1 -size +500k -size -10M
Motivation: You may want to find files that fall within a specific size range while limiting the search depth to a certain level. This can be useful when you need to locate large or small files within a specific directory and its immediate subdirectories.
Explanation:
-maxdepth 1
: Limits the searching depth to only the current directory level.-size +500k
: Matches files with a size larger than 500 kilobytes.-size -10M
: Matches files with a size smaller than 10 megabytes.
Example output:
root_path/big_file.txt
root_path/small_file.jpg
Use case 6: Run a command for each file
Code:
find root_path -name '*.ext' -exec wc -l {} \;
Motivation: You may want to execute a specific command for each file found during the search operation. This can be useful when you need to perform some action or retrieve specific information from each file.
Explanation:
-exec
: Specifies a command to be executed for each file found.wc -l {}
: The command to execute, which in this case, counts the number of lines in each file. The{}
acts as a placeholder for the current file’s name.\;
: Indicates the end of the-exec
command.
Example output:
10 root_path/file1.ext
25 root_path/subdir1/file2.ext
15 root_path/subdir2/file3.ext
Use case 7: Find files modified in the last 7 days
Code:
find root_path -daystart -mtime -7
Motivation: You may need to find files that have been modified within a specific time range, such as the last week. This can be useful when you want to work with recently updated files or track changes within a directory.
Explanation:
-daystart
: Start measuring the time from the beginning of the current day instead of the last 24 hours.-mtime -7
: Matches files that have been modified within the last 7 days.
Example output:
root_path/file1.ext
root_path/file2.txt
Use case 8: Find empty (0 byte) files and delete them
Code:
find root_path -type f -empty -delete
Motivation: You may want to find and delete empty files within a specific directory and its subdirectories. This can be useful when you want to clean up your file system or remove files that are no longer needed.
Explanation:
-type f
: Only search for regular files.-empty
: Matches files that have a size of 0 bytes.-delete
: Deletes the matched files.
Example output: No output will be displayed if there are no empty files found. The empty files in the specified directory and its subdirectories will be deleted.