在linux中写入目录和子目录中除1之外的所有文件的列表

发布于 2025-01-12 19:57:52 字数 366 浏览 3 评论 0原文

假设我有 2 个目录 src/Asrc/Bsrc/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

—━☆沉默づ 2025-01-19 19:57:52

尝试:

shopt -s extglob dotglob
find src/!(C) -type f >> changed-files-list.txt
  • extglob 选项与 shopt -s 一起打开 Bash 扩展通配符。请参阅 extglob 中的 glob - Greg 的维基。它会导致 src/!(C) 扩展到与 src/* 相同的列表,但不包括 src/C
  • 带有 shopt -sdotglob 选项会导致 glob 模式匹配名称以点 (.) 开头的文件或目录。例如,这意味着 src/!(C) 将匹配 src/.D

Try:

shopt -s extglob dotglob
find src/!(C) -type f >> changed-files-list.txt
  • The extglob option with shopt -s turns on Bash extended globbing. See extglob in glob - Greg's Wiki. It causes src/!(C) to expand to the same list as src/*, but excluding src/C.
  • The dotglob option with shopt -s causes glob patterns to match files or directories whose names start with a dot (.). It means that src/!(C) will match src/.D, for instance.
动次打次papapa 2025-01-19 19:57:52

建议使用ls命令

ls -1 src/{A,B}/*

选项-1每行列出一个文件(如find)。

优点是只列出指定的目录。且不穿越其下。

Suggesting to use ls command

ls -1 src/{A,B}/*

Option -1 list one file per line (like find).

Advantage list only the specified directories. And not traversing under.

你怎么敢 2025-01-19 19:57:52

使用 -prune 省略目录:

find src -path src/C -prune -o \( -type f  -print \)

如果您愿意接受包含 C 本身的输出行(但不包含 C 中的任何文件),您可以将其简化为:(

find src -name C -prune -o -type f

这也将省略任何文件名 C)

Use -prune to omit the directory:

find src -path src/C -prune -o \( -type f  -print \)

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:

find src -name C -prune -o -type f

(this will also omit any files names C)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文