Linux Find Command Examples
The find command is a powerful and versatile utility in
Linux and Unix-like operating systems used for searching files and
directories within a directory hierarchy. It allows users to specify
various criteria such as name, type, size, modification time, and
permissions to locate specific files.
Find Files by Case-Insensitive Extension
This command searches for files with a case-insensitive extension,
such as .jpg, .JPG, and .jpG.
The -iname option performs a case-insensitive name
match.
find . -iname '*.jpg'
Find Directories
To find only directories, use the -type d option. This
will list all subdirectories within the specified path.
find . -type d
Find Regular Files
To find only regular files (excluding directories, symbolic links,
etc.), use the -type f option.
find . -type f
Find Files by Octal Permissions
You can find files that match a specific octal permission mode using
the -perm option. For example, to find files with 777
permissions:
find . -type f -perm 777
Find Files with Setuid Bit Set
This command finds files with the setuid bit set, which is often
used for security-related executables. The -xdev option
prevents crossing filesystem boundaries.
find . -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l
A more efficient and recommended approach using
-printf to avoid external commands like
ls:
find -perm -4000 -type f -print0 | xargs -I '{}' -0 \ls -l '{}'
Find and Remove Files by Extension
To find and remove files with a specific extension, like
.txt, you can use the
-exec rm {} \; option. However, a more efficient method
is to use the built-in -delete action.
find [PATH] -name '*.txt' -delete
Find Files and Search for Strings
Combine find with grep to search for a
specific string within files that match certain criteria.
find ./path/ -name '*.txt' | xargs grep 'string'
Find Files by Size
Locate files larger than a specified size. For example, to find files larger than 5 MB:
find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z
To find files larger than 2 MB and list them with human-readable sizes:
find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
A faster alternative using -printf:
find -type f -size +20000k -printf '%s %P\n' |
awk "{printf(\"%'dM %s\n\", \$1 / (1024 * 1024), \$2)}"
Find Files by Modification Time
Find files modified more than a certain number of days ago. For instance, to find files modified more than 7 days ago:
find . -type f -mtime +7d -ls
Find Symbolic Links
Search for symbolic links owned by a specific user.
find -type l -user [NAME] -ls
Delete Empty Directories
Efficiently search for and delete empty directories using the
-empty and -delete options.
find -type d -empty -delete
Find Directories by Name and Depth
Search for directories named build up to a maximum
depth of 2 levels.
find . -maxdepth 2 -name build -type d
Find Files Excluding Specific Directories
Find all files that are not within a .git directory.
The -not or escaped ! operator is used for
negation.
find . \! -iwholename '*.git*' -type f
Find Files with the Same Inode
Locate all files that share the same inode number as a given file, indicating they are hard links.
find . -type f -samefile [FILE] 2>/dev/null
Modify File Permissions
Find all files in the current directory and modify their permissions
using -exec chmod.
find . -type f -exec chmod 644 {} \;
Edit Files with Vim
Find files with a specific extension (e.g., .txt) and
open them all for editing with vim using
-exec with + for efficiency.
find . -iname '*.txt' -exec vim {} \+
Rename File Extensions
Find files with one extension (e.g., .png) and rename
them to another (e.g., .jpg) using a bash script within
-exec. It's crucial to use \; here.
find . -type f -iname '*.png' -exec bash -c 'mv "$0" "${0%.*}.jpg"' {} \;
Delete Multiple File Types
Use logical operators like -or with grouping to delete
files of multiple specified extensions.
find \( -iname "*.jpg" -or -iname "*.sfv" -or -iname "*.xspf" \) -type f -delete
List Executable Files by Basename
List the basenames of all executable files found within the
directories specified in the PATH environment variable.
find ${PATH//:/ } -type f -executable -printf "%P\n"