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.
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.
- 544,893
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,485
-
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
headortailutility; 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