#!/usr/bin/env bash# inspired by http://stackoverflow.com/questions/4210042/exclude-directory-from-find-command Danile C. Sobral# using special syntax to avoid traversing.# However, logic is refactored because the Sobral version still traverses# everything on my system
echo ============================echo find - from cwd, omitting external volumesdateecho Enter sudo password if requestedsudo find . -not \( \-path ./Volumes/Archive -prune -o \-path ./Volumes/Boot\ OS\ X -prune -o \-path ./Volumes/C \-path ./Volumes/Data -prune -o \-path ./Volumes/jas -prune -o \-path ./Volumes/Recovery\ HD -prune -o \-path ./Volumes/Time\ Machine\ Backups -prune -o \-path ./Volumes/SuperDuper\ Image -prune -o \-path ./Volumes/userland -prune \\) -name "$1" -printdateecho ============================iMac2:~ jas$
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find /mnt/Backups/rsnapshot_backups | wc -l60314138 ## 60.3M files, folders34:07.30 ## 34 min
time du /mnt/Backups/rsnapshot_backups -d 03112240160 /mnt/Backups/rsnapshot_backups ## 3.1 TB33:51.88 ## 34 min
time rsnapshot du ## << more accurate re: rsnapshot footprint2.9T /mnt/Backups/rsnapshot_backups/hourly.0/4.1G /mnt/Backups/rsnapshot_backups/hourly.1/...4.7G /mnt/Backups/rsnapshot_backups/weekly.3/2.9T total ## 2.9 TB, per sudo rsnapshot du (more accurate)2:34:54 ## 2 hr 35 min
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find / -path /mnt -prune -o -name "*libname-server-2.a*" -print/usr/lib/libname-server-2.areal 0m8.644s ## 8.6 sec <<< NOTE!user 0m1.669ssys 0m2.466s
## As regular user (victoria); I also use an alternate timing mechanism, as## here I am using 2>/dev/null to suppress "Permission denied" warnings:
$ START="$(date +"%s")" && find 2>/dev/null / -path /mnt -prune -o \-name "*libname-server-2.a*" -print; END="$(date +"%s")"; \TIME="$((END - START))"; printf 'find command took %s sec\n' "$TIME"/usr/lib/libname-server-2.afind command took 3 sec ## ~3 sec <<< NOTE!
…在几秒钟内找到该文件,而这需要很多更长的时间(似乎递归遍历所有“排除”目录):
## As sudo (#), to avoid numerous "Permission denied" warnings:
time find / -path /mnt/ -prune -o -name "*libname-server-2.a*" -printfind: warning: -path /mnt/ will not match anything because it ends with /./usr/lib/libname-server-2.areal 33m10.658s ## 33 min 11 sec (~231-663x slower!)user 1m43.142ssys 2m22.666s
## As regular user (victoria); I also use an alternate timing mechanism, as## here I am using 2>/dev/null to suppress "Permission denied" warnings:
$ START="$(date +"%s")" && find 2>/dev/null / -path /mnt/ -prune -o \-name "*libname-server-2.a*" -print; END="$(date +"%s")"; \TIME="$((END - START))"; printf 'find command took %s sec\n' "$TIME"/usr/lib/libname-server-2.afind command took 1775 sec ## 29.6 min
#find command in linuxdef : find command used to locate /search files in unix /linux system ,find search for files in a directory hierarchy
1)exec Show diagnostic information relating to -exec, -execdir, -ok and -okdir2)-options-H =do not follow symoblic links while except while procesing .-L = follow symbolic links-P =never follow symbolic links
-type cFile is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option or the -follow option is in effect, unless thesymbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
s socket
D door (Solaris)
-DeleteDelete files; true if removal succeeded. If the removal failed, an error message is issued.If -delete#fails, find's exit status will be nonzero (when it eventually exits).
find /home/mohan/a -mindepth 3 -maxdepth 3 -type f -name "*.txt" |xargs rm -rffind -type d -namefind -type f -Namefind /path/ -type f -iname (i is case insenstive)
#find directores a/b/c and only delete c directory inside have "*.txt "find /home/mohan/a -mindepth 3 -maxdepth 3 -type f -name "*.txt" |xargs rm -rffind /home/mohan/a -mindepth 3 -maxdepath 3 -type f -name "*.txt" -delete
#delete particular directory have empty file and only we can delete empty filesfind /home/mohan -type f -name "*.txt" -empty -DELETE
#find multiple files and also find empty filesfind /home/mohan -type f \( -name "*.sh" -o -name "*.txt" \) -empty
#delete empty files two or more Filesfind /home/mohan -type f \( -nmae "*.sh" -o -name "*.txt" \) -empty -delete
#How to append contents of multiple files into one filefind . -type f -name '*.txt' -exec cat {} + >> output.file
#last modified files finding using less than 1 min (-n)ls -lrth|find . -type f -mmin -1
#last modified files more than 1 min (+n)ls -lrth|find . -type f -mmin +1
#last modified files exactly one minsfind . -type f -mmin 1
last modifiedfiles exactly in one day by using command (-mtime)find . -type f -mtime 10
#last modified more than 10 daysfind . -type f -mtime +10
#last modified less than 10 daysfind . -type f -mtime -10
#How to Find Modified Files and Folders Starting from a Given Date to the Latest Datefind . -type f -newermt "17-11-2020"
#How to Find a List of “sh” Extension Files Accessed in the Last 30 Days--- -matdimtypels -lrt|find . -type f -iname ".sh" -atime -30
#How to Find a List of Files Created Today, -1 means less than min,ls -lrt | find . -type f -ctime -1 -ls
# Exclude one path, and its contents, saving time by *not* recursing down the# excluded path at all.find . -name '*.js' -not \( -path "./dir_to_exclude" -prune \)
# Add the wildcard asterisk (`*`) to the end of the match pattern, as# in "./dir_to_exclude*", to exclude all files & folders beginning with the# name `./dir_to_exclude`. Prune to save time by *not* recursing down the# excluded paths at all.# - You can add the asterisk to the end of the pattern to apply this pattern to# all examples below as well, if desired.# - This example pattern would exclude "./dir_to_exclude", "./dir_to_exclude1",# "./dir_to_exclude2", "./dir_to_exclude99", "./dir_to_exclude_some_long_name",# "./dir_to_exclude_another_long_name", etc., as well as exclude all **files**# beginning with this match pattern but not otherwise in an excluded dir.find . -name '*.js' -not \( -path "./dir_to_exclude*" -prune \)
# Exclude multiple paths and their contents, saving time by *not* recursing down# the excluded paths at all.find . -name '*.js' \-not \( -path "./dir_to_exclude1" -prune \) \-not \( -path "./dir_to_exclude2" -prune \) \-not \( -path "./dir_to_exclude3" -prune \)
# If you change your "starting point" path from `.` to something else, be sure# to update the beginning of your `-path` with that as well, like this:
find "some_dir" -name '*.js' -not \( -path "some_dir/dir_to_exclude" -prune \)
find "some_dir" -name '*.js' \-not \( -path "some_dir/dir_to_exclude1" -prune \) \-not \( -path "some_dir/dir_to_exclude2" -prune \) \-not \( -path "some_dir/dir_to_exclude3" -prune \)
# 1. with the "starting point" being the current directory, `.`find . -not -path "./dir_to_exclude/*"# or (same thing)find -not -path "./dir_to_exclude/*"
# 2. with the "starting point" being the root dir, `/`find / -not -path "/dir_to_exclude/*"
# 3. with the "starting point" being "some_dir"find "some_dir" -not -path "some_dir/dir_to_exclude/*"
# match "./dir_to_exclude/file1" as well as# "./another_dir/dir_to_exclude/file1"find . -not -path "*/dir_to_exclude/*"
# match "/dir_to_exclude/file1" as well as# "/another_dir/dir_to_exclude/file1"find / -not -path "*/dir_to_exclude/*"
# match "some_dir/dir_to_exclude/file1" as well as# "some_dir/another_dir/dir_to_exclude/file1"find "some_dir" -not -path "*/dir_to_exclude/*"
# exclude the files and folders within the excluded dir, but# leaving "./dir_to_exclude" itselffind . -not -path "./dir_to_exclude/*"
# exclude the dir name only, but leaving (NOT excluding) all files and# folders within that dir!find . -not -path "./dir_to_exclude"
# exclude both the folder itself, as well as its contentsfind . \-not -path "./dir_to_exclude/*" \-not -path "./dir_to_exclude"
# [my favorite #1] exclude contents of `dir_to_exclude` at the search rootfind -not -path "./dir_to_exclude/*"
# exclude all files & folders beginning with the name `dir_to_exclude` at the# search rootfind -not -path "./dir_to_exclude*"
# [my favorite #2] exclude contents of `dir_to_exclude` at any level within your# search pathfind -not -path "*/dir_to_exclude/*"
# exclude all files & folders beginning with the name `dir_to_exclude` at any# level within your search pathfind -not -path "*/dir_to_exclude*"
# To exclude multiple matching patterns, use `-not -path "*/matching pattern/*"`# multiple times, like thisfind -not -path "*/dir_to_exclude1/*" -not -path "*/dir_to_exclude2/*"
find -not \( -path "./dir_to_exclude" -prune \) # works to exclude *both* the# directory *and* its contents# here, here but does *not*# exclude the contents as well# when the directory name is# written like this in the# examples abovefind -not \( -path "./dir_to_exclude*" -prune \)find -not \( -path "./dir_to_exclude/*" -prune \)find -not \( -path "*/dir_to_exclude" -prune \) # same note as just abovefind -not \( -path "*/dir_to_exclude*" -prune \)find -not \( -path "*/dir_to_exclude/*" -prune \)
# To exclude multiple matching patterns at once, use the `-not \( ... \)`# pattern multiple times, like thisfind -not \( -path "*/dir_to_exclude1/*" -prune \) \-not \( -path "*/dir_to_exclude2/*" -prune \)
但这些都不起作用:
# These do NOT work!find -not -path "dir_to_exclude"find -not -path "dir_to_exclude/*"find -not -path "./dir_to_exclude"find -not -path "./dir_to_exclude/"
关键是,通常,要使其工作,您必须以#0或#1开始每个匹配模式,并以#2或#3结束每个匹配模式,这取决于您要实现的目标。我说“一般”,因为在上面的-not \( ... \)样式部分中有两个注意到的例外。您可以通过它们右侧的注释来识别这两个例外:# works here but not above。
# Case-sensitive; notice I use `\.` instead of `.` when grepping, in order to# search for the literal period (`.`) instead of the regular expression# wildcard char, which is also a period (`.`).find -not \( -path "./dir_to_exclude/*" -prune \) \| grep "desired_file_name\.txt"
# Case-INsensitive (use `-i` with your `grep` search)find -not \( -path "./dir_to_exclude/*" -prune \) \| grep -i "desired_file_name\.txt"
# To make `dir_to_exclude` also case INsensitive, use the `find` `-ipath` option# instead of `-path`:find -not -ipath \( -path "./dir_to_exclude/*" -prune \) \| grep -i "desired_file_name\.txt"