Linux 如何在 find 中排除目录。命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:52:36  来源:igfitidea点击:

How to exclude a directory in find . command

linuxshellfind

提问by helion3

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

我正在尝试find为所有 JavaScript 文件运行一个命令,但如何排除特定目录?

Here is the findcode we're using.

这是find我们正在使用的代码。

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

采纳答案by f10bit

Use the -pruneswitch. For example, if you want to exclude the miscdirectory just add a -path ./misc -prune -oto your find command:

使用-prune开关。例如,如果要排除misc目录,只需-path ./misc -prune -o在 find 命令中添加一个:

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, dir2and dir3, since in findexpressions it is an action that acts on the criteria -path dir1 -o -path dir2 -o -path dir3(if dir1or dir2or dir3), ANDed with type -d.

这里我们排除了dir1dir2dir3,因为在find表达式中,它是一个作用于条件的动作-path dir1 -o -path dir2 -o -path dir3(如果dir1dir2dir3),与type -d.

Further action is -o print, just print.

进一步的操作是-o print,只需打印。

回答by The Archetypal Paul

find . -name '*.js' -\! -name 'glob-for-excluded-dir' -prune

回答by Joshua

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

一种选择是使用 grep 排除所有包含目录名称的结果。例如:

find . -name '*.js' | grep -v excludeddir

回答by Drew Frezell

Use the -prune option. So, something like:

使用 -prune 选项。所以,像这样:

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

The '-type d -name proc -prune' only look for directories named proc to exclude.
The '-o' is an 'OR' operator.

'-type d -name proc -prune' 只查找要排除的名为 proc 的目录。
'-o' 是一个 'OR' 运算符。

回答by mpapis

I prefer the -notnotation ... it's more readable:

我更喜欢这种-not符号......它更具可读性:

find . -name '*.js' -and -not -path directory

回答by GetFree

If -prunedoesn't work for you, this will:

如果-prune不适合你,这将:

find -name "*.js" -not -path "./directory/*"

Caveat:requires traversing all of the unwanted directories.

警告:需要遍历所有不需要的目录。

回答by Daniel C. Sobral

I find the following easier to reason about than other proposed solutions:

我发现以下比其他建议的解决方案更容易推理:

find build -not \( -path build/external -prune \) -name \*.js
# you can also exclude multiple paths
find build -not \( -path build/external -prune \) -not \( -path build/blog -prune \) -name \*.js

Important Note:the paths you type after -pathmust exactly match what findwould print without the exclusion. If this sentence confuses you just make sure to use full paths through out the wholecommand like this: find /full/path/-not \( -path /full/path/exclude/this-prune \) .... See note [1] if you'd like a better understanding.

重要说明:您键入的路径-path必须与find没有排除项的打印内容完全匹配。如果这句话迷惑你只要确保通过了使用完整路径整个这样的命令:。如果您想更好地理解,请参阅注释 [1]。find /full/path/-not \( -path /full/path/exclude/this-prune \) ...

Inside \(and \)is an expression that will match exactlybuild/external(see important note above), and will, on success, avoid traversing anything below. This is then grouped as a single expression with the escaped parenthesis, and prefixed with -notwhich will make findskip anything that was matched by that expression.

里面\(and\)是一个将完全匹配的表达式build/external(请参阅上面的重要说明),并且在成功时将避免遍历下面的任何内容。然后将其分组为带有转义括号的单个表达式,并以-notwhich为前缀,将find跳过与该表达式匹配的任何内容。

One might ask if adding -notwill not make all other files hidden by -prunereappear, and the answer is no. The way -pruneworks is that anything that, once it is reached, the files below that directory are permanently ignored.

有人可能会问,添加-not会不会使所有其他隐藏的文件-prune重新出现,答案是否定的。-prune工作方式是,一旦到达,该目录下的文件将被永久忽略。

This comes from an actual use case, where I needed to call yui-compressor on some files generated by wintersmith, but leave out other files that need to be sent as-is.

这来自一个实际用例,我需要在 Wintersmith 生成的某些文件上调用 yui-compressor,但忽略了需要按原样发送的其他文件。



Note [1]: If you want to exclude /tmp/foo/barand you run find like this "find /tmp \(..." then you must specify -path /tmp/foo/bar. If on the other hand you run find like this cd /tmp; find . \(...then you must specify -path ./foo/bar.

注意 [1]:如果要排除/tmp/foo/bar并像这样运行 find ,find /tmp \(...则必须指定-path /tmp/foo/bar. 另一方面,如果您像这样运行 find ,cd /tmp; find . \(...那么您必须指定-path ./foo/bar.

回答by Lemmings19

I was using findto provide a list of files for xgettext, and wanted to omit a specific directory and its contents. I tried many permutations of -pathcombined with -prunebut was unable to fully exclude the directory which I wanted gone.

我曾经find为 提供文件列表xgettext,并希望省略特定目录及其内容。我尝试了许多-path组合的排列,-prune但无法完全排除我想要的目录。

Although I was able to ignore the contentsof the directory which I wanted ignored, findthen returned the directory itself as one of the results, which caused xgettextto crash as a result (doesn't accept directories; only files).

虽然我能够忽略我想要忽略的目录的内容find然后将目录本身作为结果之一返回,结果导致xgettext崩溃(不接受目录;仅文件)。

My solution was to simply use grep -vto skip the directory that I didn't want in the results:

我的解决方案是简单地使用grep -v跳过结果中我不想要的目录:

find /project/directory -iname '*.php' -or -iname '*.phtml' | grep -iv '/some/directory' | xargs xgettext

Whether or not there is an argument for findthat will work 100%, I cannot say for certain. Using grepwas a quick and easy solution after some headache.

是否有一个论点可以find100% 奏效,我不能肯定地说。grep经过一些头痛,使用是一种快速简便的解决方案。

回答by sixro

None of previous answers is good on Ubuntu. Try this:

以前的答案都不适用于 Ubuntu。尝试这个:

find . ! -path "*/test/*" -type f -name "*.js" ! -name "*-min-*" ! -name "*console*"

I have found this here

在这里找到了这个

回答by james dupin

For a working solution (tested on Ubuntu 12.04 (Precise Pangolin))...

对于工作解决方案(在 Ubuntu 12.04(精确穿山甲)上测试)...

find ! -path "dir1" -iname "*.mp3"

will search for MP3 files in the current folder and subfolders except in dir1 subfolder.

将在当前文件夹和子文件夹中搜索 MP3 文件,除了 dir1 子文件夹。

Use:

用:

find ! -path "dir1" ! -path "dir2" -iname "*.mp3"

...to exclude dir1 AND dir2

...排除 dir1 AND dir2