I know ls -t
will list all files by modified time. But how can I limit these results to only the last n files?
我知道ls -t会按修改后的时间列出所有文件。但是我怎么才能将这些结果限制到最后的n个文件呢?
4 个解决方案
#1
163
Try using head or tail. If you want the 5 most-recently modified files:
试着用头或尾巴。如果你想要5个最近修改的文件:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
-1(即1)表示每行一个文件,而head表示取前5个条目。
If you want the last 5 try
如果你想要最后五次尝试
ls -1t | tail -5
#2
11
Use tail
command:
使用尾命令:
ls -t | tail -n 5
#3
2
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
ls -t按创建时间列出文件,而不是最后一次修改时间。如果您想要按最后修改时间(从上到下)列出文件,请使用ls -ltc。因此,要列出最后一个n: ls -ltc |头${n}
#4
2
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
被接受的答案只列出文件名,但要获得前5个文件,你也可以使用:
ls -lht | head -6
ls -lht | head -6
where:
地点:
-l
outputs in a list format
-l以列表格式输出
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-h使输出可读(即文件大小出现在kb、mb等中)
-t
sorts output by placing most recently modified file first
-t通过先放置最近修改的文件对输出进行排序
head -6
will show 5 files because ls
prints the block size in the first line of output.
head -6将显示5个文件,因为ls在输出的第一行打印块大小。
I think this is a slightly more elegant and possibly more useful approach.
我认为这是一种更优雅、可能更有用的方法。
Example output:
示例输出:
total 26960312 -rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py -rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf -rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf -rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf -rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
总计26960312 -rw-r- r- @ 1名用户员工1.2K 11 1月11日11:22电话2.7py -rw-r- r- @ 1个用户员工2.7米10 1月15:26 03-cookies-1。pdf -rw-r- r- @ 1名用户员工92m 9 1月16:21 Wk1_sem。pdf -rw-r- r- @ 1用户工作人员502k8 Jan 10:20 lab-01。用户工作人员2.0M 5 1 / 2 0410-1.wmv。
#1
163
Try using head or tail. If you want the 5 most-recently modified files:
试着用头或尾巴。如果你想要5个最近修改的文件:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
-1(即1)表示每行一个文件,而head表示取前5个条目。
If you want the last 5 try
如果你想要最后五次尝试
ls -1t | tail -5
#2
11
Use tail
command:
使用尾命令:
ls -t | tail -n 5
#3
2
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
ls -t按创建时间列出文件,而不是最后一次修改时间。如果您想要按最后修改时间(从上到下)列出文件,请使用ls -ltc。因此,要列出最后一个n: ls -ltc |头${n}
#4
2
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
被接受的答案只列出文件名,但要获得前5个文件,你也可以使用:
ls -lht | head -6
ls -lht | head -6
where:
地点:
-l
outputs in a list format
-l以列表格式输出
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-h使输出可读(即文件大小出现在kb、mb等中)
-t
sorts output by placing most recently modified file first
-t通过先放置最近修改的文件对输出进行排序
head -6
will show 5 files because ls
prints the block size in the first line of output.
head -6将显示5个文件,因为ls在输出的第一行打印块大小。
I think this is a slightly more elegant and possibly more useful approach.
我认为这是一种更优雅、可能更有用的方法。
Example output:
示例输出:
total 26960312 -rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py -rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf -rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf -rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf -rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
总计26960312 -rw-r- r- @ 1名用户员工1.2K 11 1月11日11:22电话2.7py -rw-r- r- @ 1个用户员工2.7米10 1月15:26 03-cookies-1。pdf -rw-r- r- @ 1名用户员工92m 9 1月16:21 Wk1_sem。pdf -rw-r- r- @ 1用户工作人员502k8 Jan 10:20 lab-01。用户工作人员2.0M 5 1 / 2 0410-1.wmv。