cdnewest: Jump Into the Newest Directory

Roy HaydenRoy Hayden
2 min read

Here’s a tiny shell trick I use all the time when working with auto-generated or newly created folders:

alias cdnewest='cd $(ls -1dt ./*/ | head -n1)'

What It Does

This one-liner changes into the newest directory in the current folder. It’s especially handy when:

  • You just ran a script that creates a timestamped folder

  • You unpacked a zip or tarball and want to jump into it

  • You’re working with tools that dump output into new dirs (e.g. logs, build artifacts)

Why I Made It

I was sick of doing this manually:

ls -lrt
cd folder-name

Sometimes I didn’t even know what the folder was called, just that it was created 2 seconds ago. I wanted a one-command way to say:

“Take me to the latest directory created here.”

How It Works

Let’s break it down:

  • ls -1dt ./*/
    Lists all directories (./*/) in the current path, sorted by modification time (-t), most recent first (-d), one per line (-1).

  • head -n1
    Grabs just the top (newest) directory.

  • cd $(...)
    Changes into it.

Put together, cdnewest means:
“cd into the most recently modified directory here.”

Example

$ mkdir test_$(date +%s)
$ cdnewest

Now you’re in the new folder.

How to Make It Stick

Add this to your ~/.bashrc or ~/.bash_aliases:

alias cdnewest='cd $(ls -1dt ./*/ | head -n1)'

Then reload your shell:

source ~/.bashrc
0
Subscribe to my newsletter

Read articles from Roy Hayden directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Roy Hayden
Roy Hayden