使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
find . \( -path '.**/.git' -o -path '.**/.hg' \) -prune -o -name '*.js' -print
上面的示例查找当前目录下的所有*.js文件,不包括文件夹.git和.hg,无论这些.git和.h文件夹有多深。
注意:这也适用于:
find . \( -path '.*/.git' -o -path '.*/.hg' \) -prune -o -name '*.js' -print
但我更喜欢**表示法,以便与其他一些工具保持一致,这在这里可能会偏离主题。
其他回答
我想知道目录的数量,文件的大小(仅为当前目录的MB),而这段代码正是我想要的:-)
来源
- ... 2791037 Jun 2 2011 foo.jpg
- ... 1284734651 Mär 10 16:16 foo.tar.gz
- ... 0 Mär 10 15:28 foo.txt
d ... 4096 Mär 3 17:12 HE
d ... 4096 Mär 3 17:21 KU
d ... 4096 Mär 3 17:17 LE
d ... 0 Mär 3 17:14 NO
d ... 0 Mär 3 17:15 SE
d ... 0 Mär 3 17:13 SP
d ... 0 Mär 3 17:14 TE
d ... 0 Mär 3 19:20 UN
代码
format="%s%'12d\n"
find . -type d -not -path "./*/*" | wc -l | awk -v fmt=$format '{printf fmt, " Anzahl Ordner = ", $1-1}'
find . -type f -not -path "./*/*" | wc -l | awk -v fmt=$format '{printf fmt, " Anzahl Dateien = ", $1}'
du . -hmS --max-depth=0 | awk -v fmt=$format '{printf fmt, " Groesse (MB) = ", $1}'
注意:awk需要额外的format=“%s%12d\n”来格式化数字。
结果
Anzahl Ordner = 8
Anzahl Dateien = 3
Groesse (MB) = 1.228
这适合我在Mac上使用:
find . -name *.php -or -path "./vendor" -prune -or -path "./app/cache" -prune
它将排除带有php后缀的搜索名称的供应商和app/cache-dir。
对于工作解决方案(在Ubuntu 12.04(精确穿山甲)上测试)。。。
find ! -path "dir1" -iname "*.mp3"
将在当前文件夹和子文件夹(dir1子文件夹除外)中搜索MP3文件。
Use:
find ! -path "dir1" ! -path "dir2" -iname "*.mp3"
…排除dir1和dir2
最好使用exec操作而不是for循环:
find . -path "./dirtoexclude" -prune \
-o -exec java -jar config/yuicompressor-2.4.2.jar --type js '{}' -o '{}' \;
执行者…'{}' ... '{}' \; 将对每个匹配的文件执行一次,将大括号“{}”替换为当前文件名。
请注意,大括号括在单引号中,以防止它们被解释为shell脚本标点符号*。
笔记
*从find(GNU findutils)4.4.2手册页的EXAMPLES部分
我认为自己是一个狂欢爱好者,但是。。。在过去的两年中,我们没有找到一个适合bash用户的解决方案。我所说的“用户友好”是指只需一次调用,这不需要我记住复杂的语法+我可以使用与以前相同的find语法,因此以下解决方案最适合那些^^^
复制粘贴到shell中,并将~/.bash_aliases作为源代码:
cat << "EOF" >> ~/.bash_aliases
# usage: source ~/.bash_aliases , instead of find type findd + rest of syntax
findd(){
dir=$1; shift ;
find $dir -not -path "*/node_modules/*" -not -path "*/build/*" \
-not -path "*/.cache/*" -not -path "*/.git/*" -not -path "*/venv/*" $@
}
EOF
当然,为了添加或删除要排除的目录,您必须使用您选择的目录编辑此别名func。。。