使用find搜索*.js文件时,如何排除特定目录?

find . -name '*.js'

当前回答

如果有人在研究如何同时忽略多条路径。您可以使用bash数组(在GNUbash版本4.4.20(1)-发行版上运行良好)

#!/usr/bin/env bash

# This script helps ignore unnecessary dir paths while using the find command

EXCLUDE_DIRS=(
    "! -path /*.git/*"
    "! -path /*go/*"
    "! -path /*.bundle/*"
    "! -path /*.cache/*"
    "! -path /*.local/*"
    "! -path /*.themes/*"
    "! -path /*.config/*"
    "! -path /*.codeintel/*"
    "! -path /*python2.7/*"
    "! -path /*python3.6/*"
    "! -path /*__pycache__/*"
)
find $HOME -type f ${EXCLUDE_DIRS[@]}

# if you like fzf

find $HOME -type f ${EXCLUDE_DIRS[@]} | fzf --height 40% --reverse

同样由于某些原因,您将无法忽略/bin/目录路径。

其他回答

对于跳过目录的首选语法应该是什么,这里显然有些混乱。

GNU意见

To ignore a directory and the files under it, use -prune

从GNU查找手册页

推理

-prune阻止find下降到目录中。仅指定-not-path仍将进入跳过的目录,但每当查找测试每个文件时,-not-paath将为false。

与-prune有关的问题

-梅干做了它想要做的事情,但在使用它时仍需要注意一些事情。

find打印修剪后的目录。TRUE这是预期的行为,它只是没有下降到目录中。为了避免完全打印目录,请使用逻辑上省略它的语法。-prune只适用于-print,不适用于其他操作。不正确-prune适用于除-delete之外的任何操作。为什么它不能与delete一起使用?要使-delete起作用,find需要按DFS顺序遍历目录,因为-delete将首先删除树叶,然后删除树叶的父级,等等。但是,要指定-sprune以使其合理,find必须命中一个目录并停止其降序,这显然在启用-dedepth或-delete时没有意义。

表演

我对这个问题的三个排名靠前的答案进行了简单的测试(用-exec bash-c'echo$0'{}\;替换-print以显示另一个动作示例)。结果如下

----------------------------------------------
# of files/dirs in level one directories
.performance_test/prune_me     702702    
.performance_test/other        2         
----------------------------------------------

> find ".performance_test" -path ".performance_test/prune_me" -prune -o -exec bash -c 'echo "$0"' {} \;
.performance_test
.performance_test/other
.performance_test/other/foo
  [# of files] 3 [Runtime(ns)] 23513814

> find ".performance_test" -not \( -path ".performance_test/prune_me" -prune \) -exec bash -c 'echo "$0"' {} \;
.performance_test
.performance_test/other
.performance_test/other/foo
  [# of files] 3 [Runtime(ns)] 10670141

> find ".performance_test" -not -path ".performance_test/prune_me*" -exec bash -c 'echo "$0"' {} \;
.performance_test
.performance_test/other
.performance_test/other/foo
  [# of files] 3 [Runtime(ns)] 864843145

结论

f10bit的语法和Daniel C.Sobral的语法平均运行时间为10-25ms。GetFree的语法不使用-prune,耗时865ms。所以,是的,这是一个相当极端的例子,但如果您关心运行时间,并且正在做任何远程密集的事情,那么您应该使用-prune。

注意Daniel C.Sobral的语法在两种删减语法中表现得更好;但是,我强烈怀疑这是某些缓存的结果,因为切换两个运行的顺序会导致相反的结果,而非修剪版本总是最慢的。

测试脚本

#!/bin/bash

dir='.performance_test'

setup() {
  mkdir "$dir" || exit 1
  mkdir -p "$dir/prune_me/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/w/x/y/z" \
    "$dir/other"

  find "$dir/prune_me" -depth -type d -exec mkdir '{}'/{A..Z} \;
  find "$dir/prune_me" -type d -exec touch '{}'/{1..1000} \;
  touch "$dir/other/foo"
}

cleanup() {
  rm -rf "$dir"
}

stats() {
  for file in "$dir"/*; do
    if [[ -d "$file" ]]; then
      count=$(find "$file" | wc -l)
      printf "%-30s %-10s\n" "$file" "$count"
    fi
  done
}

name1() {
  find "$dir" -path "$dir/prune_me" -prune -o -exec bash -c 'echo "$0"'  {} \;
}

name2() {
  find "$dir" -not \( -path "$dir/prune_me" -prune \) -exec bash -c 'echo "$0"' {} \;
}

name3() {
  find "$dir" -not -path "$dir/prune_me*" -exec bash -c 'echo "$0"' {} \;
}

printf "Setting up test files...\n\n"
setup
echo "----------------------------------------------"
echo "# of files/dirs in level one directories"
stats | sort -k 2 -n -r
echo "----------------------------------------------"

printf "\nRunning performance test...\n\n"

echo \> find \""$dir"\" -path \""$dir/prune_me"\" -prune -o -exec bash -c \'echo \"\$0\"\'  {} \\\;
name1
s=$(date +%s%N)
name1_num=$(name1 | wc -l)
e=$(date +%s%N)
name1_perf=$((e-s))
printf "  [# of files] $name1_num [Runtime(ns)] $name1_perf\n\n"

echo \> find \""$dir"\" -not \\\( -path \""$dir/prune_me"\" -prune \\\) -exec bash -c \'echo \"\$0\"\' {} \\\;
name2
s=$(date +%s%N)
name2_num=$(name2 | wc -l)
e=$(date +%s%N)
name2_perf=$((e-s))
printf "  [# of files] $name2_num [Runtime(ns)] $name2_perf\n\n"

echo \> find \""$dir"\" -not -path \""$dir/prune_me*"\" -exec bash -c \'echo \"\$0\"\' {} \\\;
name3
s=$(date +%s%N)
name3_num=$(name3 | wc -l)
e=$(date +%s%N)
name3_perf=$((e-s))
printf "  [# of files] $name3_num [Runtime(ns)] $name3_perf\n\n"

echo "Cleaning up test files..."
cleanup

我在C源文件exclude*.o和exclude*.swp以及exclude(非常规文件)和exclude-dir输出中找到了函数名:

find .  \( ! -path "./output/*" \) -a \( -type f \) -a \( ! -name '*.o' \) -a \( ! -name '*.swp' \) | xargs grep -n soc_attach

使用-prune选项。因此,类似于:

find . -type d -name proc -prune -o -name '*.js'

“-typed-name-proc-prune”只查找要排除的名为proc的目录。“-o”是“OR”运算符。

#linux中的find命令def:find命令用于在unix/linux系统中查找/搜索文件,查找搜索目录层次结构中的文件

1) exec显示与-exec、-execdir、-ok和-okdir相关的诊断信息2) -选项-H=除非在处理过程中,否则不要遵循符号链接。-L=遵循符号链接-P=从不遵循符号链接-c型文件类型为c:b块(缓冲)特殊c字符(无缓冲)特殊d目录p命名管道(FIFO)f常规文件l符号链接;如果-L选项或follow选项有效,则永远不会出现这种情况,除非符号链接断开。如果要在-L有效时搜索符号链接,请使用-xtype。s插座D门(Solaris)-删除删除文件;如果删除成功,则为true。如果删除失败,将发出错误消息。如果-删除#如果失败,find的退出状态将为非零(当它最终退出时)。find/home/mohan/a-mindepth 3-maxdepth 3-type f-name“*.txt”|xargs rm-rffind-type d-namefind-type f-名称find/path/-type f-iname(i是大小写限制)#查找目录a/b/c,只有删除其中的c目录才有“*.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#删除特定目录有空文件,只有我们才能删除空文件find/home/mohan-type f-name“*.txt”-空-删除#查找多个文件,同时查找空文件find/home/mohan-type f \(-name“*.sh”-o-name“*.txt”\)-空#删除空文件两个或多个文件find/home/mohan-type f \(-name“*.sh”-o-name“*.txt”\)-empty-delete#如何将多个文件的内容追加到一个文件中查找-键入f-name“*.txt”-exec cat{}+>>output.file#上次修改的文件查找时间少于1分钟(-n)ls-lrth|查找-f-mmin-1型#上次修改的文件超过1分钟(+n)ls-lrth|查找-f型-mmin+1#上次修改的文件正好一分钟查找-f-mmin 1型使用命令(-mtime)在一天内完全修改最后一个文件查找-类型f-mtime 10#上次修改时间超过10天查找-类型f-mtime+10#上次修改时间少于10天查找-类型f-mtime-10#如何查找从给定日期到最新日期的修改文件和文件夹查找-类型f-newermt“17-11-2020”#如何查找过去30天内访问的“sh”扩展文件列表---matdimtypels-lrt |查找-类型f-iname“.sh”-atime-30#如何查找今天创建的文件列表,-1表示少于分钟,ls-lrt |查找-类型f-ctime-1-ls

这适合我在Mac上使用:

find . -name *.php -or -path "./vendor" -prune -or -path "./app/cache" -prune

它将排除带有php后缀的搜索名称的供应商和app/cache-dir。