Shell – Get the latest directory (not the latest file)

fileslsshelltimestamps

My folder parent has the following content:

A.Folder B.Folder C.File

It has both folders and files inside. B.Folder is newer. Now I just want to get B.Folder, how could I achieve this? I tried this,

ls -ltr ./parent | grep '^d' | tail -1

but it gives me drwxrwxr-x 2 user user 4096 Jun 13 10:53 B.Folder, but I just need the name B.Folder.

Best Answer

Try this:

$ ls -td -- */ | head -n 1

-t options make ls sort by modification time, newest first.

If you want remove /:

$ ls -td -- */ | head -n 1 | cut -d'/' -f1
Related Question