Skip to content

Instantly share code, notes, and snippets.

@koteq
Created August 4, 2013 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koteq/6149870 to your computer and use it in GitHub Desktop.
Save koteq/6149870 to your computer and use it in GitHub Desktop.
Change dir recursively
#!/bin/bash
#
# this script should not be run directly,
# instead you need to source it from your .bashrc,
# by adding this line:
# . ~/bin/cdr.sh
#
function cdr() {
replacement=$1
array=(${PWD//\// })
for ((i = ${#array[@]} - 1; i >= 0; i--)); do
directory=${PWD/${array[i]}/$replacement}
if [ -d $directory ]; then
# show whats changed, and cd to new location
echo $PWD
echo $directory
cd $directory
return
fi
done
echo "Valid path not found"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment