在linux中写入目录和子目录中除1之外的所有文件的列表
假设我有 2 个目录 src/A
、src/B
和 src/C
。
我想列出 A 和 B 中的所有文件,但不是 C。
这工作正常,但它也列出了
find src/ -type f >> changed-files-list.txt
我尝试过的
find src/ -type f -not -name 'src/C' >> changed-files-list.txt
src/C 的文件,但我猜它不起作用,因为我使用了 -type f。如何从上述命令中排除目录?
Suppose I have 2 directories src/A
, src/B
and src/C
.
I want to list all the files inside A and B but not C.
This working fine but it's also listing file of src/C
find src/ -type f >> changed-files-list.txt
I tried
find src/ -type f -not -name 'src/C' >> changed-files-list.txt
but it's not working I guess because I used -type f. How can exclude a directory from above command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
extglob
选项与shopt -s
一起打开 Bash 扩展通配符。请参阅 extglob 中的 glob - Greg 的维基。它会导致src/!(C)
扩展到与src/*
相同的列表,但不包括src/C
。shopt -s
的dotglob
选项会导致 glob 模式匹配名称以点 (.
) 开头的文件或目录。例如,这意味着src/!(C)
将匹配src/.D
。Try:
extglob
option withshopt -s
turns on Bash extended globbing. See extglob in glob - Greg's Wiki. It causessrc/!(C)
to expand to the same list assrc/*
, but excludingsrc/C
.dotglob
option withshopt -s
causes glob patterns to match files or directories whose names start with a dot (.
). It means thatsrc/!(C)
will matchsrc/.D
, for instance.建议使用
ls
命令选项
-1
每行列出一个文件(如find)。优点是只列出指定的目录。且不穿越其下。
Suggesting to use
ls
commandOption
-1
list one file per line (like find).Advantage list only the specified directories. And not traversing under.
使用
-prune
省略目录:如果您愿意接受包含 C 本身的输出行(但不包含 C 中的任何文件),您可以将其简化为:(
这也将省略任何文件名
C
)Use
-prune
to omit the directory:If you are willing to accept the line of output that includes C itself (but none of the files in C), you can simplify that to:
(this will also omit any files names
C
)