按日期顺序列出文件,文件名中包含空格

发布于 2024-10-10 06:17:04 字数 757 浏览 0 评论 0原文

我从一个包含数百个文件(完整路径)的列表(按随机顺序)的文件开始。我想列出该列表中十个最新文件的详细信息。这是我天真的尝试:

$ ls -las -t `cat list-of-files.txt` | head -10

只要所有文件中都没有空格,就可以,但如果它们有空格,则会失败,因为这些文件在空格处分开并被视为单独的文件。文件“hello world”给了我:

ls: hello: No such file or directory
ls: world: No such file or directory

我尝试引用原始文件列表文件中的文件,但此处文档仍然在文件名中的空格处分割文件,将引号视为文件名的一部分:

$ ls -las -t `awk '{print "\"" $0 "\""}' list-of-files.txt` | head -10

ls: "hello: No such file or directory
ls: world": No such file or directory

我能想到的唯一方法是单独 ls 每个文件(也许使用 xargs)并创建一个中间文件,其中文件列表和日期以可排序的顺序作为每行中的第一个字段,然后对该中间文件进行排序。然而,这感觉有点麻烦和低效(数百个 ls 命令而不是一两个)。但这可能是唯一的方法吗?

有没有什么方法可以传递“ls”要处理的文件列表,其中这些文件可以包含空格 - 看起来应该很简单,但我很困惑。

I am starting with a file containing a list of hundreds of files (full paths) in a random order. I would like to list the details of the ten latest files in that list. This is my naive attempt:

$ ls -las -t `cat list-of-files.txt` | head -10

That works, so long as none of the files have spaces in, but fails if they do as those files are split up at the spaces and treated as separate files. File "hello world" gives me:

ls: hello: No such file or directory
ls: world: No such file or directory

I have tried quoting the files in the original list-of-files file, but the here-document still splits the files up at the spaces in the filenames, treating the quotes as part of the filenames:

$ ls -las -t `awk '{print "\"" $0 "\""}' list-of-files.txt` | head -10

ls: "hello: No such file or directory
ls: world": No such file or directory

The only way I can think of doing this, is to ls each file individually (using xargs perhaps) and create an intermediate file with the file listings and the date in a sortable order as the first field in each line, then sort that intermediate file. However, that feels a bit cumbersome and inefficient (hundreds of ls commands rather than one or two). But that may be the only way to do it?

Is there any way to pass "ls" a list of files to process, where those files could contain spaces - it seems like it should be simple, but I'm stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

何处潇湘 2024-10-17 06:17:06

您可以强制 bash 使用另一个字段分隔符,而不是“一个或多个空白字符”:

OIFS=$IFS
IFS=

但是,我认为这段代码不会比执行循环更有效;此外,如果 list-of-files.txt 中的文件数量超过参数的最大数量,则该方法将不起作用。

\n' ls -las -t $(cat list-of-files.txt) | head -10 IFS=$OIFS

但是,我认为这段代码不会比执行循环更有效;此外,如果 list-of-files.txt 中的文件数量超过参数的最大数量,则该方法将不起作用。

Instead of "one or more blank characters", you can force bash to use another field separator:

OIFS=$IFS
IFS=

However, I don't think this code would be more efficient than doing a loop; in addition, that won't work if the number of files in list-of-files.txt exceeds the max number of arguments.

\n' ls -las -t $(cat list-of-files.txt) | head -10 IFS=$OIFS

However, I don't think this code would be more efficient than doing a loop; in addition, that won't work if the number of files in list-of-files.txt exceeds the max number of arguments.

深海不蓝 2024-10-17 06:17:06

试试这个:

xargs -a list-of-files.txt ls -last | head -n 10

Try this:

xargs -a list-of-files.txt ls -last | head -n 10
灼痛 2024-10-17 06:17:06

我不确定这是否有效,但是您是否尝试使用 \ 转义空格?用sed什么的。例如,sed "s/ /\\\\ /g" list-of-files.txt

I'm not sure whether this will work, but did you try escaping spaces with \? Using sed or something. sed "s/ /\\\\ /g" list-of-files.txt, for example.

酒废 2024-10-17 06:17:06

这对我有用:

xargs -d\\n ls -last < list-of-files.txt | head -10

This worked for me:

xargs -d\\n ls -last < list-of-files.txt | head -10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文