Looking for a command that will return the single most recent file in a directory.
Not seeing a limit parameter to ls
...
I personally prefer to use as few not built-in bash
commands as I can (to reduce the number of expensive fork and exec syscalls). To sort by date the ls
needed to be called. But using of head
is not really necessary. I use the following one-liner (works only on systems supporting name pipes):
read newest < <(ls -t *.log)
or to get the name of the oldest file
read oldest < <(ls -rt *.log)
(Mind the space between the two '<' marks!)
If the hidden files are also needed -A arg could be added.
I hope this could help.
Finding the most current file in every directory according to a pattern, e.g. the sub directories of the working directory that have name ending with "tmp" (case insensitive):
find . -iname \*tmp -type d -exec sh -c "ls -lArt {} | tail -n 1" \;
ls -Art | tail -n 1
Not very elegant, but it works.
I use:
ls -ABrt1 --group-directories-first | tail -n1
It gives me just the file name, excluding folders.
try this simple command
ls -ltq <path> | head -n 1
If you want file name - last modified, path = /ab/cd/*.log
If you want directory name - last modified, path = /ab/cd/*/
ls -Frt | grep "[^/]$" | tail -n 1