"find | xargs | ls" 不是 运行 ls 查找文件名
"find | xargs | ls" not running ls on filenames from find
所以我有一个包含文件和子目录的目录。我想递归地获取所有文件,然后以长格式列出它们,按修改日期排序。这是我的想法。
find . -type f | xargs -d "\n" | ls -lt
然而,这只列出了当前目录中的文件,而不是子目录。我不明白为什么,因为下面打印出了所有文件。
find . -type f | xargs -d "\n" | cat
感谢任何帮助。
xargs
只有在 ls
作为参数传递 时才能启动 ls
。当您从 xargs
传输到 ls
时,只有 ls
的一个副本被启动——由父级 shell——并且它没有给出来自find | xargs
作为参数——它们在它的标准输入上,但是 ls
从不读取它的标准输入,所以它甚至不知道它们在那里.
因此,您需要删除 |
字符:
# Does what you specified in the common case, but buggy; don't use this
# (filenames can contain newlines!)
# ...also, xargs -d is GNU-only
find . -type f | xargs -d '\n' ls -lt
...或者,更好:
# uses NUL separators, which cannot exist inside filenames
# also, while a non-POSIX extension, this is supported in both GNU and BSD xargs
find . -type f -print0 | xargs -0 ls -lt
...或者,甚至更好:
# no need for xargs at all here; find -exec can do the same thing
# -exec ... {} + is POSIX-mandated functionality since 2008
find . -type f -exec ls -lt {} +
此答案中的大部分内容也涵盖在 Actions、Complex Actions 和 Actions 中Using Find 的 Bulk 节,非常值得一读。
所以我有一个包含文件和子目录的目录。我想递归地获取所有文件,然后以长格式列出它们,按修改日期排序。这是我的想法。
find . -type f | xargs -d "\n" | ls -lt
然而,这只列出了当前目录中的文件,而不是子目录。我不明白为什么,因为下面打印出了所有文件。
find . -type f | xargs -d "\n" | cat
感谢任何帮助。
xargs
只有在 ls
作为参数传递 时才能启动 ls
。当您从 xargs
传输到 ls
时,只有 ls
的一个副本被启动——由父级 shell——并且它没有给出来自find | xargs
作为参数——它们在它的标准输入上,但是 ls
从不读取它的标准输入,所以它甚至不知道它们在那里.
因此,您需要删除 |
字符:
# Does what you specified in the common case, but buggy; don't use this
# (filenames can contain newlines!)
# ...also, xargs -d is GNU-only
find . -type f | xargs -d '\n' ls -lt
...或者,更好:
# uses NUL separators, which cannot exist inside filenames
# also, while a non-POSIX extension, this is supported in both GNU and BSD xargs
find . -type f -print0 | xargs -0 ls -lt
...或者,甚至更好:
# no need for xargs at all here; find -exec can do the same thing
# -exec ... {} + is POSIX-mandated functionality since 2008
find . -type f -exec ls -lt {} +
此答案中的大部分内容也涵盖在 Actions、Complex Actions 和 Actions 中Using Find 的 Bulk 节,非常值得一读。