Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
use this ls -ltr ./parent | grep '^d' | tail -1| awk '{print $NF}' –  Babyy Jun 13 at 15:36
    
Using awk works, but I want to see if there is a cleaner solution. :) –  Daniel Jun 13 at 15:44
    
find . -maxdepth 1 -type d | TZ=utc xargs ls -ld --full-time | sort -k 6 –  richard Jun 13 at 16:18
add comment

4 Answers 4

ls -td -- ./parent/*/ | head -n1 | cut -d'/' -f2

Difference to Herson's solution is the slash after *, which makes the shell ignore all non-dir files. Difference to Gnouc, it'll work if you are in another folder.

Cut needs to know the number of parent directories (2) in order to delete trailing '/'. If you don't have that, use

VAR=$(ls -dt -- parent/*/ | head -n1); echo "${VAR::-1}"
share|improve this answer
add comment

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
share|improve this answer
    
How to get rid of slash /? –  Daniel Jun 13 at 15:39
    
This does not work if you are in another folder. I mean ls -td -- ./parent | head -n 1 does not give me the correct results, do I HAVE TO use subshell such as cd ./parent every time and execute this command? Any solution? –  Daniel Jun 13 at 16:02
    
@Daniel: You must run my answer in current working directory to make it works. Otherwise, you must use a relative path like Herson's answer. –  Gnouc Jun 13 at 17:22
add comment

Obligatory zsh answer:

latest_directory=(parent/*(/om[1]))

The characters in parentheses are glob qualifiers: / to match only directories, om to sort matches by increasing age, and [1] to retain only the first (i.e. newest) match. Add N if you want to get an empty array (normally you get a 1-elementy array) if there is no subdirectory of parent.

Alternatively, assuming that parent doesn't contain any shell globbing character:

latest_directory='parent/*(/om[1])'; latest_directory=$~latest_directory

If you don't have zsh but you have recent GNU tools (i.e. non-embedded Linux or Cygwin), you can use find, but it's cumbersome. Here's one way:

latest_directory_inode=$(find parent -mindepth 1 -maxdepth 1 -type d -fprintf '%Ts %i' | sort -n | sed -n '1 s/.* //p')
latest_directory=$(find parent -maxdepth 1 -inum "$latest_directory_inode")

There's a simple solution with ls, which works as long as no directory name contains newlines or (on some systems) non-printable characters:

latest_directory=$(ls -td parent/*/ | head -n1)
latest_directory=${latest_directory%/}
share|improve this answer
add comment

You can do:

ls -td -- ../parent/* | head -n 1
share|improve this answer
    
This gives the latest file. Daniel only wants directories. –  Gilles Jun 14 at 22:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.