I have this directory contents:

enter image description here

As you can see, the directory names are just timestamps - in general when I am the top level directory I usually want to open the most recent directory which is the one with the largest timestamp value which means it should be sorted last (right?).

Is there a good way to quickly cd into the last directory? Can I assume they will be sorted by timestamp by the OS? Otherwise the dirty way to do it would be to copy the value of the largest timestamp and then run "$ cd 1453775108178".


Both the ls command and wildcards such as * list file names in lexicographic orders. For file names that consist solely of digits, this corresponds to numerical order only if all the file names have the same number of digits. Your file names appear to be milliseconds since the epoch; as long as the times are between 2001-09-09 01:46:40 and 2286-11-20 17:46:39.999, lexicographic order is fine for you.

In zsh, you can use the [-1] glob qualifier to take the last match.

cd *([-1])

This can be refined in several ways, such as

  • cd *(/[-1]) to ensure that only directories are matched (in case there are also other types of files).
  • cd *(-/[-1]) to also include symlinks to directories.
  • cd <->([-1]) to ensure that only names consisting of digits only¹ are matched.
  • cd <->(/n[-1]) to sort those names in numerical instead of lexicographic order (e.g. 10 sorted after 9 instead of just after 1).

Another approach in zsh is to make use of completion. This is more or less straightforward depending on the current completion settings. If you set the glob_complete option (which I do) and bind reverse-menu-complete to a key, let's say Shift+Tab, which can be done with the following code in your .zshrc:

setopt glob_complete
bindkey '\e\t' reverse-menu-complete

then you can type cd *Shift+Tab and the * will be replaced by the last match.

Other shells lack this nice functionality. You can make a little function.

cdlast () {
  set ./*/
  shift "$(($#-1))"
  cd "$1"

With an optional leading -, but given that file names beginning with - are problematic, hopefully you don't use them.

| improve this answer | |

If you only have directories being created, you can find the last file /dir using --

ls -tr1 |tail -1
to cd to it --
cd $(ls -tr1 | tail -1)

If you have both files and dirs, more work is needed --

cd $(ls -ltr |grep ^d |tail -1 | awk '{print $9}')

other options -- find with mtime (you need to know the window when the dir was created to filter) Since all these commands may take very long to run if the dir is large (100k+ files) the best way to do this is to create one file which contains the last dir created and use that to cd to the latest dir.

| improve this answer | |

You could create an alias or function in the .bashrc file - e.g:

alias lcd="cd $( ls -1 | tail -1 )"


lcd() { cd $( ls -1 | tail -1 ) ; }
| improve this answer | |

If you have control over the process that creates the directories, it may be possible to adapt it so that it always writes a symlink called, say, latest that points to the newest directory.

This is what I do in the script that extracts photos from my digital camera card, and it works very well for me:

$ ls -loghd pictures/[12l]*
dr-xr-xr-x  4 4.0K May 24  2012 pictures/1989
dr-xr-xr-x  6 4.0K May 24  2012 pictures/1990
dr-xr-xr-x 38 4.0K Dec 15 19:59 pictures/2015
drwxr-xr-x  3 4.0K Jan 10 23:03 pictures/2016
lrwxrwxrwx  1   34 Jan 10 23:03 pictures/latest -> 2016/2016-01-10
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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