Vojtěch Struhár

Posted on December 4, 2023 | #bash

Enter the newest subdirectory.

My various side-project folders contain tens of side projects that are yet to succeed. And I’m only ever really interested in the latest one (hehe). So instead of remembering its name, I want to change directories right into it.

That’s precisely what this bash function does:

cdl() {
    # Change Directory to Latest
    local latest_dir=$(ls -td -- */ | head -n 1)
    if [[ -d "$latest_dir" ]]; then
        cd "$latest_dir"
    else
        echo "No directories found!"
    fi
}

Breakdown

I just put this function in my .aliases file, sourced in .zshrc - usual stuff.

Read next → Convert python objects into dictionaries