是否有可能以某种方式使用find
命令,使其不会递归到子目录中?例如:
DirsRoot
|-->SubDir1
| |-OtherFile1
|-->SubDir2
| |-OtherFile2
|-File1
|-File2
那么像 find DirsRoot --do-not-recurse -type f
这样的命令会得到只有 File1, File2
两个文件的结果吗?
根据你当前的命令结构,我认为你可以使用 -maxdepth 1
选项来获得你想要的结果。如果不行,你可以尝试查看 find
的man page。
相关条目(方便起见):
-maxdepth levels
Descend at most levels (a non-negative integer) levels of direc-
tories below the command line arguments. `-maxdepth 0' means
only apply the tests and actions to the command line arguments.
你的选择基本可以分为:
# Do NOT show hidden files (beginning with ".", i.e., .*):
find DirsRoot/* -maxdepth 0 -type f
或者:
# DO show hidden files:
find DirsRoot/ -maxdepth 1 -type f
我相信您正在寻找-maxdepth 1
。
如果您需要符合POSIX标准的解决方案:
cd DirsRoot && find . -type f -print -o -name . -o -prune
-maxdepth 不是符合POSIX标准的选项。
find DirsRoot/* -type f -prune
吗? - dokaspar-prune
前插入“-o”了)答案是否定的。要完全理解为什么它不能被简化,只需在发出 find DirsRoot/* -type f -o -prune
命令之前发出 set -x
命令,你就会立即看到它。根本原因是 shell 扩展 DirsRoot/*
表达式的限制。 - sqr163find . -name . -o -prune
。 - Reishin使用find
命令中的-maxdepth
选项可以实现。
find /DirsRoot/* -maxdepth 1 -type f
来自手册
man find
-maxdepth levels
最多在起始点以下的目录层级(一个非负整数)中下降levels层。
-maxdepth 0
仅对起始点本身应用测试和操作。
-maxdepth 0
没有显示 任何 文件,但-maxdepth 1
的表现如预期一样,包括显示隐藏文件。 - Srinifind DirsRoot/* -maxdepth 0 -type f
中的*
。如果您省略它,将不会显示任何文件。 - mapeters