Last active
October 29, 2025 03:03
-
-
Save GNOMES/6bf65926648e260d8023aebb9ede9573 to your computer and use it in GitHub Desktop.
cd backwards to named directoy in current path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Add to ~/.bashrc | |
| # jump backwards from cwd path | |
| dc() { | |
| cd "${PWD%/$1/*}/$1/" 2>/dev/null || echo "Directory not found: $1" | |
| } | |
| # Function to add tab completion to dc | |
| _dc_complete() { | |
| IFS='/' read -ra dirs <<<"$(pwd)" | |
| local dir_names=("${dirs[@]:1}") | |
| COMPREPLY=() | |
| for dir in "${dir_names[@]}"; do | |
| [[ $dir == ${COMP_WORDS[COMP_CWORD]}* ]] && COMPREPLY+=("$dir") | |
| done | |
| } | |
| complete -F _dc_complete dc |
Author
Author
Revised parent gist to a pure bash version. Older version using sed:
dc() {
cd "$(pwd | sed "s|$1\/.*|$1\/|")" || echo "Directory not found: $1"
}
@GNOMES Thanks for this. It is pretty slick.
I try to keep track of the source and license of all the snippets I put in my bashrc. Is there a license I can put down for this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using this instead of repetitive
cd ../../..for going backwards from current directory.Example: