Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

ls, gives me all directories

ls -trh, gives me all directories sorted by date (newest last)

ls -dtrh */ | tail -1, gives me name of latest directory (by date)

Is it possible to somehow incorporate the ls and cd commands, so I could navigate to the latest directory. Something logically equal to ls -trh | tail -1 | cd, but working.

share|improve this question

migrated from Jan 22 '11 at 10:51

This question came from our site for professional and enthusiast programmers.

It also gives you files. You can't cd to a file. – gbarry Dec 10 '10 at 3:08
Thanks, I've just updated command to show folders only – Petro Semeniuk Dec 10 '10 at 3:12
Migrate this question to Super User? – Peter Mortensen Jan 22 '11 at 9:47
up vote 10 down vote accepted
cd "$(ls -trh | tail -1)"

This uses the output of the the ls|tail pipeline as the command-line arguments to cd.

EDIT: camh is correct that this should give better performance, because head won't go through the lines you're ignoring.

cd "$(ls -th | head -1)"
share|improve this answer
I'd suggest instead of reverse sorting and using tail, you use a normal sort and use head. That way it processes less data: cd $(ls -th | head -n 1) (I know the OP used -r|tail, but we can improve on that) – camh Dec 10 '10 at 3:28
@camh, good suggestion. – Matthew Flaschen Dec 10 '10 at 3:34
Protect the command substitution with double quotes, or else it won't work when the directory contains spaces. – marco Jan 22 '11 at 12:12
@marco, thanks, fixed. – Matthew Flaschen Jan 22 '11 at 13:00

solution using backticks:

cd `ls -th | head -1`
share|improve this answer

I have done an alias for my own use:

alias cdu='cd $(ls -rtd */ | tail -1)'

this will put you in last modified/created directory in your position.

share|improve this answer

Use this simple command:

cd `ls -t`

The character <`> is a backtick character. Not an apostrophe.

This will go to the latest directory. Try it.

share|improve this answer
It will probably not, and is more likely to return a "too many arguments" error. I say probably, because it will work if a single directory is in the current-dir. – Slomojo Jan 22 '11 at 13:05

Your Answer


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.