I am using ls -l -t
to get a list of files in a directory ordered by time.
我使用ls -l -t获取按时间顺序排列的目录中的文件列表。
I would like to limit the search result to the top 2 files in the list.
Is this possible?
I've tried with grep and I struggled.
我想把搜索结果限制在列表的前两个文件中。这是可能的吗?我试过了grep,我很挣扎。
5 个解决方案
#1
52
You can pipe it into head
:
你可以把它导入大脑:
ls -l -t | head -3
Will give you top 3 lines (2 files and the total).
将给你3行(2个文件和总数)。
This will just give you the first 2 lines of files, skipping the size line:
这只会给你前两行文件,跳过大小行:
ls -l -t | tail -n +2 | head -2
tail
strips the first line, then head
outputs the next 2 lines.
尾部剥离第一行,然后头部输出下两行。
#2
6
To avoid dealing with the top output line you can reverse the sort and get the last two lines
为了避免处理顶部输出行,您可以反转排序并得到最后两行。
ls -ltr | tail -2
This is pretty safe, but depending what you'll do with those two file entries after you find them, you should read Parsing ls on the problems with using ls
to get files and file information.
这是非常安全的,但是在找到这两个文件条目之后,您应该根据使用ls获取文件和文件信息的问题阅读解析ls。
#3
1
You can use the head
command to grab only the first two lines of output:
您可以使用head命令只抓取前两行输出:
ls -l -t | head -2
#4
0
You have to pipe through head.
你得穿过头部。
ls -l -t | head -n 3
ls -l -t |水头- n3
will output the two first results.
将输出两个第一个结果。
#5
0
Or you could try just this
或者你可以试试这个
ls -1 -t | head -2
The -1 switch skips the title line.
-1开关跳过标题行。
#1
52
You can pipe it into head
:
你可以把它导入大脑:
ls -l -t | head -3
Will give you top 3 lines (2 files and the total).
将给你3行(2个文件和总数)。
This will just give you the first 2 lines of files, skipping the size line:
这只会给你前两行文件,跳过大小行:
ls -l -t | tail -n +2 | head -2
tail
strips the first line, then head
outputs the next 2 lines.
尾部剥离第一行,然后头部输出下两行。
#2
6
To avoid dealing with the top output line you can reverse the sort and get the last two lines
为了避免处理顶部输出行,您可以反转排序并得到最后两行。
ls -ltr | tail -2
This is pretty safe, but depending what you'll do with those two file entries after you find them, you should read Parsing ls on the problems with using ls
to get files and file information.
这是非常安全的,但是在找到这两个文件条目之后,您应该根据使用ls获取文件和文件信息的问题阅读解析ls。
#3
1
You can use the head
command to grab only the first two lines of output:
您可以使用head命令只抓取前两行输出:
ls -l -t | head -2
#4
0
You have to pipe through head.
你得穿过头部。
ls -l -t | head -n 3
ls -l -t |水头- n3
will output the two first results.
将输出两个第一个结果。
#5
0
Or you could try just this
或者你可以试试这个
ls -1 -t | head -2
The -1 switch skips the title line.
-1开关跳过标题行。