I know ls -t will list all files by modified time. But how can I limit these results to only the last n files?
- 14,817
- 30
- 97
- 171
-
12Reverse the order `ls -lrt` might help somebody. – PJ Brunet Jan 12 '15 at 14:51
-
Love it @PJBrunet. I've formed the habit of always ls'ing with `tharl` as it tends to have everything I need. – Adam Grant Feb 15 '18 at 16:57
6 Answers
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
- 25,810
- 7
- 57
- 77
-
2If I'm not mistaken, the shell represents the output of `ls` in multiple columns, but the output of `ls` is piped to following command with 1 file/dir at a time – Alex Mar 28 '13 at 20:22
-
3
-
2I don't see a total, but if there is one that would certainly work. – Paul Rubel Mar 29 '13 at 13:31
-
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
ls -lht | head -6
where:
-l outputs in a list format
-h makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-t sorts output by placing most recently modified file first
head -6 will show 5 files because ls prints the block size in the first line of output.
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
- 641
- 6
- 17
Use tail command:
ls -t | tail -n 5
- 9,662
- 8
- 39
- 60
-
1This gives the 5 oldest. You need to reverse the sort ( `ls -tr` ) or use `head` rather than `tail`, to get the 5 newest. – Headbank Jan 03 '20 at 13:52
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.
For the newest 5 files ordered from newest to oldest, use head to take the first 5 lines of output:
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:
ls -tr | tail -n 5
- 342
- 2
- 6
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}
- 37
- 2
-
1Which platform/version do you refer to? Can you provide a link? Looking at the [linux man-page](http://man7.org/linux/man-pages/man1/ls.1.html) yields the opposite of what you state: `-t sort by modification time, newest first` – Joma Sep 08 '16 at 21:35
None of other answers worked for me. The results were both folders and files, which is not what I would expect.
The solution that worked for me was:
find . -type f -mmin -10 -ls
This lists in the current directory all the files modified in the last 10 minutes. It will not list last 5 files, but I think it might help nevertheless
- 4,465
- 2
- 35
- 61