Linux 如何列出 (ls) 目录中最后修改的 5 个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15691359/
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
How can I list (ls) the 5 last modified files in a directory?
提问by Ryan
I know ls -t
will list all files by modified time. But how can I limit these results to only the last nfiles?
我知道ls -t
会按修改时间列出所有文件。但是如何将这些结果限制为仅最后n 个文件?
采纳答案by Paul Rubel
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(那是一个)表示每行一个文件,头部表示取前 5 个条目。
If you want the last 5 try
如果你想要最后 5 次尝试
ls -1t | tail -5
回答by Alex
Use tail
command:
使用tail
命令:
ls -t | tail -n 5
回答by victrr
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 | head ${n}
回答by samisnotinsane
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
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
回答by Headbank
By default ls -t
sorts output from newest to oldest, so the combination of commands to use depends in which direction you want your output to be ordered.
默认情况下,ls -t
输出从最新到最旧排序,因此要使用的命令组合取决于您希望输出的排序方向。
For the newest 5 files ordered from newest to oldest, use head
to take the first 5 lines of output:
对于从新到旧排序的最新 5 个文件,使用head
输出的前 5 行:
ls -t | head -n 5
For the newest 5 files ordered from oldest to newest, use the -r
switch to reverse ls
's sort order, and use tail
to take the last 5 lines of output:
对于从最旧到最新排序的最新 5 个文件,使用-r
switch 来反转ls
的排序顺序,并使用tail
输出的最后 5 行:
ls -tr | tail -n 5