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 10:08

I found the functions name in C sources files exclude *.o and exclude *.swp and exclude (not regular file) and exclude dir output with this command:

find .  \( ! -path "./output/*" \) -a \( -type f \) -a \( ! -name '*.o' \) -a \( ! -name '*.swp' \) | xargs grep -n soc_attach
查看更多
爱死公子算了
3楼-- · 2018-12-31 10:10

There are plenty of good answers, it just took me some time to understand what each element of the command was for and the logic behind it.

find . -path ./misc -prune -o -name '*.txt' -print

find will start finding files and directories in the current directory, hence the find ..

The -o option stands for a logical OR and separates the two parts of the command :

[ -path ./misc -prune ] OR [ -name '*.txt' -print ]

Any directory or file that is not the ./misc directory will not pass the first test -path ./misc. But they will be tested against the second expression. If their name corresponds to the pattern *.txt they get printed, because of the -print option.

When find reaches the ./misc directory, this directory only satisfies the first expression. So the -prune option will be applied to it. It tells the find command to not explore that directory. So any file or directory in ./misc will not even be explored by find, will not be tested against the second part of the expression and will not be printed.

查看更多
美炸的是我
4楼-- · 2018-12-31 10:10

Better use the exec action than the for loop:

find . -path "./dirtoexclude" -prune \
    -o -exec java -jar config/yuicompressor-2.4.2.jar --type js '{}' -o '{}' \;

The exec ... '{}' ... '{}' \; will be executed once for every matching file, replacing the braces '{}' with the current file name.

Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation*.


Notes

* From the EXAMPLES section of the find (GNU findutils) 4.4.2 man page

查看更多
旧时光的记忆
5楼-- · 2018-12-31 10:11

One option would be to exclude all results that contain the directory name with grep. For example:

find . -name '*.js' | grep -v excludeddir
查看更多
路过你的时光
6楼-- · 2018-12-31 10:11

To exclude multiple directories:

find . -name '*.js' -not \( -path "./dir1" -o -path "./dir2/*" \)

To add directories, add -o -path "./dirname/*":

find . -name '*.js' -not \( -path "./dir1" -o -path "./dir2/*" -o -path "./dir3/*"\)

But maybe you should use a regular expression, if there are many directories to exclude.

查看更多
初与友歌
7楼-- · 2018-12-31 10:12

Use the prune switch, for example if you want to exclude the misc directory just add a -path ./misc -prune -o to your find command:

find . -path ./misc -prune -o -name '*.txt' -print

Here is an example with multiple directories:

find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -print

Here we exclude dir1, dir2 and dir3, since in find expressions it is an action, that acts on the criteria -path dir1 -o -path dir2 -o -path dir3 (if dir1 or dir2 or dir3), ANDed with type -d. Further action is -o print, just print.

查看更多
登录 后发表回答