2020-03-15 16:16:14 +00:00
|
|
|
# find
|
|
|
|
# Search for files in a directory hierarchy
|
|
|
|
|
|
|
|
# Find files by case-insensitive extension, such as `.jpg`, `.JPG`, & `.jpG`).
|
|
|
|
# By default, find(1) uses glob pathname pattern matching. To avoid shell
|
|
|
|
# interpretation, the glob either must be expanded or the string quoted.
|
2021-03-15 15:33:56 +00:00
|
|
|
#
|
|
|
|
# Period is optional; it's implied unless a path is provided. find(1) works
|
|
|
|
# recursively unless otherwise directed (IE: `-maxdepth [N]`).
|
2019-08-01 12:19:46 +00:00
|
|
|
find . -iname '*.jpg'
|
2018-01-12 10:20:53 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find directories.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type d
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files. Specifically files; not directories, links, FIFOs, etc.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type f
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files set to the provided octal mode (permissions).
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type f -perm 777
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files with setuid bit set, keeping to the same filesystem.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l
|
2020-03-15 16:16:14 +00:00
|
|
|
# The above is a useful demonstration of some pitfalls into which a user can
|
|
|
|
# fall, where the below is the above but corrected. Here is why:
|
|
|
|
#
|
|
|
|
# * The `.` (current working directory) is assumed when no path is provided.
|
|
|
|
# * Group syntax (parentheses) was used, but nothing was actually grouped.
|
|
|
|
# * A lot of people have their ls(1) command aliased in many ways, -
|
|
|
|
# potentially causing problems with the output and how xargs(1) handles it.
|
|
|
|
# By escaping the command, we temporarily override any aliases and even
|
|
|
|
# functions by the same name.
|
|
|
|
# * At least in my experience, the prior xargs(1) is not as reliable.
|
|
|
|
# * The `-print0` and `xargs -0` is great, but unnecessary (except when?).
|
|
|
|
#
|
|
|
|
# However, it might be more preferred to simply use find(1)'s own `-printf`
|
|
|
|
# flag, in order to avoid the need for xargs(1) and ls(1), which should be many
|
|
|
|
# times faster, and allows for more specificity.
|
|
|
|
find -perm -4000 -type f -print0 | xargs -I '{}' -0 \ls -l '{}'
|
|
|
|
|
2021-06-08 15:03:13 +00:00
|
|
|
# Find and remove files with case-sensitive extension of `.txt`.
|
2020-03-15 16:16:14 +00:00
|
|
|
find [PATH] -name '*.txt' -exec rm '{}' \;
|
|
|
|
# The above is much more efficiently written as shown below, as find(1) has its
|
|
|
|
# own built-in delete function, not to mention a single rm(1) process was
|
|
|
|
# previously executed for each file processed, which is comparatively slow.
|
|
|
|
find [PATH] -name '*.txt' -delete
|
|
|
|
|
|
|
|
# Find files with extension '.txt' and look for a string into them.
|
2018-01-12 10:20:53 +00:00
|
|
|
find ./path/ -name '*.txt' | xargs grep 'string'
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files with size bigger than 5 Mb and sort them by size.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files bigger thank 2 MB and list them.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
|
2020-03-15 16:16:14 +00:00
|
|
|
# Alternative, faster approach* to the above.
|
|
|
|
#
|
|
|
|
# Why it's faster:
|
|
|
|
#
|
|
|
|
# * No need for an external process, like ls(1).
|
|
|
|
# * The use of `;` with the `-exec` flag executes an ls(1) process for each
|
|
|
|
# file found, which is comparatively very slow.
|
|
|
|
# * The `printf` feature is built in and special to awk(1).
|
|
|
|
#
|
|
|
|
# That said, awk(1) or gawk(1) is doing a little more here, in order to get
|
|
|
|
# somewhat of a human-readable file size, but its impact is likely negligible.
|
|
|
|
find -type f -size +20000k -printf '%s %P\n' |
|
|
|
|
awk "{printf(\"%'dM %s\n\", \$1 / (1024 * 1024), \$2)}"
|
|
|
|
|
|
|
|
# Find files modified more than 7 days ago and list file information.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type f -mtime +7d -ls
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find symlinks owned by the given user, then list file information.
|
|
|
|
find -type l -user [NAME] -ls
|
|
|
|
# The following may be the syntax used on a Mac, however this is not valid on
|
|
|
|
# Linux, or at least version 4.7.0. All flags in GNU find(1) are one `-` only.
|
|
|
|
find . -type l --user=[NAME] -ls
|
2018-01-12 10:20:53 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Search for and delete empty directories.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type d -empty -exec rmdir {} \;
|
2020-03-15 16:16:14 +00:00
|
|
|
# A far more efficient approach to the above. If no path is provided, then the
|
|
|
|
# current working directory (CWD) is assumed, making the `.` superfluous.
|
|
|
|
find -type d -empty -delete
|
2018-01-12 10:20:53 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Search for directories named `build` at a maximum depth of 2 directories.
|
|
|
|
# This means that find will not recursively search beyond two levels.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -maxdepth 2 -name build -type d
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Search all files which are not in a `.git` directory. Depending on the shell
|
|
|
|
# used, the bang (`!`) may need to be escaped, to avoid shell interpretation.
|
|
|
|
# Alternatively, although non-POSIX, the `-not` flag can be used.
|
|
|
|
find . \! -iwholename '*.git*' -type f
|
2018-01-12 10:20:53 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find all files that have the same inode (indicating hard link) as FILE. All
|
|
|
|
# output going to STDERR (typically error messages) will also be redirected to
|
|
|
|
# `/dev/null`, a special pseudo-file where data is sent to die.
|
|
|
|
find . -type f -samefile [FILE] 2>/dev/null
|
2018-01-12 10:20:53 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find all files in the current directory and modify their permissions.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -type f -exec chmod 644 {} \;
|
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files with extension `.txt` and edit all of them with vim(1).
|
|
|
|
#
|
|
|
|
# The use of `+` (escaped to avoid shell interpretation) with `-exec` means
|
|
|
|
# that only one process (in this case, `vim`) per `exec`ution is used. If `;`
|
|
|
|
# is instead used (would also need escaping), then one `vim` process would be
|
|
|
|
# used per file.
|
2018-01-12 10:20:53 +00:00
|
|
|
find . -iname '*.txt' -exec vim {} \+
|
2019-05-01 09:43:34 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# Find files with extension `.png`, then rename their extension to `.jpg`. It's
|
|
|
|
# highly important that `\;` is used here, instead of `\+`, otherwise it'd make
|
|
|
|
# a right mess of the files, due to the way in which mv(1) works.
|
2019-08-01 12:19:46 +00:00
|
|
|
find . -type f -iname '*.png' -exec bash -c 'mv "$0" "${0%.*}.jpg"' {} \;
|
2019-11-06 00:50:46 +00:00
|
|
|
|
|
|
|
# Use logic and grouping to delete extension-specific files.
|
|
|
|
find \( -iname "*.jpg" -or -iname "*.sfv" -or -iname "*.xspf" \) -type f -delete
|
2019-11-06 17:36:10 +00:00
|
|
|
|
2020-03-15 16:16:14 +00:00
|
|
|
# List executable files, by basename, found within PATH.
|
2019-11-06 17:36:10 +00:00
|
|
|
find ${PATH//:/ } -type f -executable -printf "%P\n"
|