I'm in the root folder and need to find the last 10 modified files of just this folder. Every time I put -mtime like my lecturer said I get 10 days. I need the last 10 modified files, not the last 10 days worth. I have tried find -my time, my time piped with tail. I get a long list of every modified file of the last 10 days. I need just the last 10 modified files of the root directory.
-
Do you need this output recursively (the latest files in all subdirectories), or only for the current directory? Please edit your question to include this information, and also include exactly what you tried. – Panki Oct 25 '21 at 08:26
-
2Does this answer your question? [Merging files from most recent](https://unix.stackexchange.com/questions/672253/merging-files-from-most-recent) – cas Oct 25 '21 at 09:03
-
aside from the fact that the link above wants to merge (concatenate) recent files and you just want to list them, this is the same problem with the same basic answer. – cas Oct 25 '21 at 09:05
-
2It would be educational if the lecturer expected you to use the `head` or `tail` utility; you could then create some specially-named files to show how that implicit assumption of "one file per line" breaks. – Jeff Schaller Oct 25 '21 at 10:24
-
1Similar: [Displaying the last N modified files?](https://unix.stackexchange.com/q/57358) See also: [How to find last n newest files for each of multiple directories](https://unix.stackexchange.com/q/389277) – Stéphane Chazelas Oct 25 '21 at 16:38
2 Answers
In zsh, for the 10 regular files in the current working directory or below that were last modified the most recently:
ls -ldt -- **/*(D.om[1,10])
In other shells, but assuming you're on a recent GNU system:
find . -type f -printf '%T@:%p\0' |
LC_ALL=C sort -zrn |
LC_ALL=C sed -z 's/^[^:]*://;10q' |
xargs -r0 ls -ltd
If you don't want to consider files in subdirectories, remove the **/ in zsh or add -maxdepth 1 to find after ..
To exclude hidden files, remove the D glob qualifier in zsh, or change the find line to:
LC_ALL=C find . -name '.?*' -prune -o -type f -printf '%T@:%p\0' |
Or if also excluding files in subdirectories:
LC_ALL=C find . -maxdepth 1 ! -name '.*' -type f -printf '%T@:%p\0' |
Those make no assumption on what characters or non-characters the file paths may contain.
- 522,931
- 91
- 1,010
- 1,501
ls -t | head should work, as long as the filenames don't include newlines.
ls -t sorts by time, with newest files first. head only keeps the top 10 lines.
If you want more details, you can use ls -lt, but that prepends an extra line with the total size, so you need ls -lt | head -n 11.
If you want to include hidden files, you can use ls -At | head. (ls -A, or --almost-all for GNU ls, includes hidden files except for . and ...)
Note that this gives you the most recent 10 files of any type, including directories, not just regular files.
- 1,465
- 11
- 13
-
You can use ls -latR | head to search also in subdirectories, to find the oldest files, use ls -latr | head – Wolfgang Blessen Aug 18 '23 at 09:31