The combination of find and ls works well for
- file names without newlines
- not very large number of files
- not very long file names
Decision:
find . -name "my-pattern" ... -print | xargs -0 ls -1 -t | head -1
Let me break it:
With find we can map all interesting files as follows:
find . -name "my-pattern" ...
then using -print0 , we can safely pass all the file names to ls as follows:
find . -name "my-pattern" ... -print0 | xargs -0 ls -1 -t
ls -t will sort the files by modification time (first the newest) and print them one line at a time. You can use -c to sort by creation time. Note : this will break with file names containing newlines.
Finally, head -1 will deliver us the first file in the sorted list.
Note. xargs use system limits for the size of the argument list. If this size exceeds, xargs will call ls several times. This will disrupt the sorting and possibly also the final output. Run
xargs --show-limits
to check the limits of your system.
Boris Brodski Nov 23 '17 at 9:43 on 2017-11-23 09:43
source share