Skip to content

Instantly share code, notes, and snippets.

@archi
Created September 19, 2019 12:35
Show Gist options
  • Save archi/2a2331401842c0548fa8de0f69796f58 to your computer and use it in GitHub Desktop.
Save archi/2a2331401842c0548fa8de0f69796f58 to your computer and use it in GitHub Desktop.
bash function to go 'up' a bunch of folders
# usage example (after 'source'ing this file):
# up 4
# to go up 4 folders
#
# or:
# up
# then it goes up by 1, and you can press ENTER to go up further
# press any other key to stay where your are
#
# Bonus: Sets OLDPWD, so you can use "cd -" to go back to the origin path
#
# (Tested with bash 5, TODO: "up foobar" to go to the next folder that matches the pattern "foobar")
function up {
local OLD=`pwd`
if [ "$#" == "1" ]; then
local cnt=$1
while [ $cnt != 0 ]; do
cd ..
cnt=`expr $cnt - 1`
done
pwd
export OLDPWD=$OLD
return
fi
local char=''
local cnt=0
while [ "$char" == "" ]; do
cd ..
pwd
cnt=`expr $cnt + 1`
read -n1 -s -r char
done
export OLDPWD=$OLD
echo "up by $cnt"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment