从排序文件中获取特定文件

时间:2011-02-18 08:51:00

标签: scripting

meybe这太简单了,但在unix上是新的。

我有一个像/ home这样的目录,在这个目录中我有5个文件,如;

/home/file1.csv

/home/file2.csv

/home/file3.csv

/home/file4.csv

/home/file5.csv

我想显示一个文件,它们必须按日期排序,我可以从该订单中显示例如3.file ..

2 个答案:

答案 0 :(得分:1)

我不确定你是想让它先订购最新订购还是最先订购。

ls -t | head -3

将打印最新的3个文件,

ls -rt | head -3

将打印最旧的3个文件。

如果你想在访问时间内对结果进行排序(上面的命令在修改时间内对它们进行排序),请分别使用以下命令。

ls -u | head -3
ls -ru | head -3

答案 1 :(得分:0)

上面提到的aifreedom方法比我之前提到的方法更好(转移到底部)

@denso,要忽略这些目录,你可以这样做。

ls -p | grep -v \/ | head -3

ls -p lists everything in the current directory with the directories followed by a "/" grep -v \*symbol* takes a stream and filters out the inputs containing symbol (here, the words containing /) head -3 will provide the first 3 from the list.

_old_

I don't know if there is an easier way to do this, but this works

ls -p | grep -v \/ | head -3