bash Linux 按日期排序“ls -al”输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23412774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:21:23  来源:igfitidea点击:

Linux sorting "ls -al" output by date

linuxbashsorting

提问by Mindaugas Bernatavi?ius

I want to sort the output of the "ls -al" command according to date. I am able to easily do that for one column with command:

我想根据日期对“ls -al”命令的输出进行排序。我可以使用命令轻松地为一列执行此操作:

$ ls -al | sort -k6 -M -r

But how to do it for both collumn 6 and 7 simultaneously? The command:

但是如何同时为第 6 列和第 7 列执行此操作?命令:

$ ls -al | sort -k6 -M -r | sort -k7 -r

prints out results I do not understand.

打印出我不明白的结果。

The final goal would be to see all the files from the most recently modified (or v.v.).

最终目标是查看最近修改(或 vv)中的所有文件。

Here is the attached example for the data to be sorted and the command used: enter image description here

以下是要排序的数据和使用的命令的附加示例: 在此处输入图片说明

回答by William Pursell

With sort, if you specify -k6, the key starts at field 6 and extends to the end of the line. To truncate it and only use field 6, you should specify -k6,6. To sort on multiple keys, just specify -kmultiple times. Also, you need to apply the M modifier only to the month, and the n modifier to the day. So:

对于 sort,如果您指定-k6,则键从字段 6 开始并延伸到行尾。要截断它并仅使用字段 6,您应该指定-k6,6. 要对多个键进行排序,只需指定-k多次。此外,您只需将 M 修饰符应用于月份,将 n 修饰符应用于日期。所以:

 ls -al | sort -k 6,6M -k 7,7n -r 

Do note Charles' comment about abusing ls though. Its output cannot be reliably parsed.

请注意查尔斯关于滥用 ls 的评论。它的输出不能被可靠地解析。

回答by Amitesh

The final goal would be to see all the files from the most recently modified

最终目标是查看最近修改的所有文件

ls -t

or (for reverse, most recent at bottom):

或(反向,最近在底部):

ls -tr

The ls man page describes this in more details, and lists other options.

ls 手册页更详细地描述了这一点,并列出了其他选项。