find . -type d
可用于查找某个起点以下的所有目录。但是它.也会返回当前目录(),这可能是不希望的。如何排除呢?
find . -type d
可用于查找某个起点以下的所有目录。但是它.也会返回当前目录(),这可能是不希望的。如何排除呢?
Answers:
find . ! -path . -type d
对于这种特殊情况(.),打高尔夫球比mindepth解决方案要好(24个字符对26个字符),尽管由于的原因,打高尔夫球可能有点困难!。
要排除其他目录,这会打的不好,并且需要一个DRYness变量:
D="long_name"
find "$D" ! -path "$D" -type d
我!与之间的决策树-mindepth:
!了便携性。.?扔硬币。long_name?使用-mindepth。find / ! -regex '/\(a\|b\)/.*'或更简单的方法来遍历grep。为了不递归,上面是非常低效的,你应该使用-prune:stackoverflow.com/questions/1489277/...
findwith grep来排除目录,但是父目录仍然存在,从而导致所有内容都被删除。
find,您需要检查前缀:stackoverflow.com/questions/17959317/…但是Bash for loop可以处理它:-)
\!安全起见,您可能希望避免使用感叹号()。我机器上的所有示例man find均已退出,因此好像是一个好主意。编辑-刚刚注意到它甚至明确说过:! expr True if expr is false. This character will also usually need protection from interpretation by the shell.
不仅find可以通过-maxdepth参数控制的递归深度,还可以使用相应的-mindepth参数从“顶部”限制深度。因此,实际需要的是:
find . -mindepth 1 -type d
find . -mindepth 1 -maxdepth 1 -type d ...
我find ./* <...>在不介意忽略一级点文件时使用(*默认情况下,blob中的glob与这些glob不匹配-请在内置的shopt中查看“ dotglob”选项:https://www.gnu.org/software/bash /manual/html_node/The-Shopt-Builtin.html)。
eclipse tmp#查找。 。 。/屏幕 ./screen/.testfile2 ./.X11-unix ./.ICE-unix ./tmux-0 ./tmux-0/默认
eclipse tmp#查找./* 。/屏幕 ./screen/.testfile2 ./tmux-0 ./tmux-0/默认
-exec选项与选项一起使用。例如,如果尝试find dir/* -type d -exec rmdir {} \;,则会看到错误。
rmdir并且很可能告诉您目录不为空,因为find它将对目录进行深度优先搜索,在其子目录之前显示父目录。
find /path/ ! -path "/path/first" ! -path "/path/second"是唯一的方法吗?