How to exclude a directory in find . command

2018-12-31 09:21发布

I'm trying to run a find command for all JavaScript files, but how do I exclude a specific directory?

Here is the find code we're using.

for file in $(find . -name '*.js')
do 
  java -jar config/yuicompressor-2.4.2.jar --type js $file -o $file
done

标签: linux shell find
30条回答
倾城一夜雪
2楼-- · 2018-12-31 09:57

This is the only one that worked for me.

find / -name NameOfFile ! -path '*/Directory/*'

Searching for "NameOfFile" excluding "Directory". Give emphasis to the stars * .

查看更多
无色无味的生活
3楼-- · 2018-12-31 09:57
 find . -name '*.js' -\! -name 'glob-for-excluded-dir' -prune
查看更多
冷夜・残月
4楼-- · 2018-12-31 09:58

For those of you on older versions of UNIX who cannot use -path or -not

Tested on SunOS 5.10 bash 3.2 and SunOS 5.11 bash 4.4

find . -type f -name "*" -o -type d -name "*excluded_directory*" -prune -type f
查看更多
明月照影归
5楼-- · 2018-12-31 09:59

If -prune doesn't work for you, this will:

find -name "*.js" -not -path "./directory/*"
查看更多
与风俱净
6楼-- · 2018-12-31 09:59

This is the format I used to exclude some paths:

$ find ./ -type f -name "pattern" ! -path "excluded path" ! -path "excluded path"

I used this to find all files not in ".*" paths:

$ find ./ -type f -name "*" ! -path "./.*" ! -path "./*/.*"
查看更多
无色无味的生活
7楼-- · 2018-12-31 10:00
find -name '*.js' -not -path './node_modules/*' -not -path './vendor/*'

seems to work the same as

find -name '*.js' -not \( -path './node_modules/*' -o -path './vendor/*' \)

and is easier to remember IMO.

查看更多
登录 后发表回答