Created
March 16, 2022 15:56
-
-
Save Juan-Embid/bd28f7d0b01fde28464cb06e4a49df11 to your computer and use it in GitHub Desktop.
Got this script from geeksforgeeks. The script jumps to the desire directory with out typing "cd ../../../../.. ". Execute permissions are needed ($ chmod -x path/to/our/file/jump.sh) and also add it to your bashrd ($ echo “source ~/path/to/our/file/jump.sh”>> ~/.bashrc). Now you can try the script ($ jump dir_name)
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
| # !/bin/bash | |
| # A simple bash script to move up to desired directory level directly | |
| function jump() | |
| { | |
| # original value of Internal Field Separator | |
| OLDIFS=$IFS | |
| # setting field separator to "/" | |
| IFS=/ | |
| # converting working path into array of directories in path | |
| # eg. /my/path/is/like/this | |
| # into [, my, path, is, like, this] | |
| path_arr=($PWD) | |
| # setting IFS to original value | |
| IFS=$OLDIFS | |
| local pos=-1 | |
| # ${path_arr[@]} gives all the values in path_arr | |
| for dir in "${path_arr[@]}" | |
| do | |
| # find the number of directories to move up to | |
| # reach at target directory | |
| pos=$[$pos+1] | |
| if [ "$1" = "$dir" ];then | |
| # length of the path_arr | |
| dir_in_path=${#path_arr[@]} | |
| #current working directory | |
| cwd=$PWD | |
| limit=$[$dir_in_path-$pos-1] | |
| for ((i=0; i<limit; i++)) | |
| do | |
| cwd=$cwd/.. | |
| done | |
| cd $cwd | |
| break | |
| fi | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment