ls command

Print the Newest File in a Given Directory and Other Directory Listing Examples

Linux directory listing command ls is another must.

This command lists files oldest to newest piped to grep which excludes directories piped to awk which returns the last line, which is the newest file. It only returns the file name.

ls -lrt | grep ^- | awk 'END{print $NF}'

If you wanted see all of the files and directories sorted oldest to newest this would do it.

ls -lrt

If you wanted see all of the files and directories sorted newest to oldest.

ls -lt

Leave a Reply

Your email address will not be published. Required fields are marked *