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
How to exclude a directory in find . command
提问by helion3
I'm trying to run a find
command for all JavaScript files, but how do I exclude a specific directory?
我正在尝试find
为所有 JavaScript 文件运行一个命令,但如何排除特定目录?
Here is the find
code 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 -prune
switch. For example, if you want to exclude the misc
directory just add a -path ./misc -prune -o
to 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 find
expressions 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
.
这里我们排除了dir1、dir2和dir3,因为在find
表达式中,它是一个作用于条件的动作-path dir1 -o -path dir2 -o -path dir3
(如果dir1或dir2或dir3),与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 -not
notation ... it's more readable:
我更喜欢这种-not
符号......它更具可读性:
find . -name '*.js' -and -not -path directory
回答by GetFree
If -prune
doesn'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 -path
must exactly match what find
would 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 -not
which will make find
skip anything that was matched by that expression.
里面\(
and\)
是一个将完全匹配的表达式build/external
(请参阅上面的重要说明),并且在成功时将避免遍历下面的任何内容。然后将其分组为带有转义括号的单个表达式,并以-not
which为前缀,将find
跳过与该表达式匹配的任何内容。
One might ask if adding -not
will not make all other files hidden by -prune
reappear, and the answer is no. The way -prune
works 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/bar
and 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 find
to provide a list of files for xgettext
, and wanted to omit a specific directory and its contents. I tried many permutations of -path
combined with -prune
but 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, find
then returned the directory itself as one of the results, which caused xgettext
to crash as a result (doesn't accept directories; only files).
虽然我能够忽略我想要忽略的目录的内容,find
然后将目录本身作为结果之一返回,结果导致xgettext
崩溃(不接受目录;仅文件)。
My solution was to simply use grep -v
to 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 find
that will work 100%, I cannot say for certain. Using grep
was a quick and easy solution after some headache.
是否有一个论点可以find
100% 奏效,我不能肯定地说。grep
经过一些头痛,使用是一种快速简便的解决方案。
回答by sixro
回答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