changing to parent directory in unix
And I thought I was lazy...
Long ago, I got tired of typing cd ..
so, since roughly 1988 one of my standard aliases (and batch files for MSDOS/Windows) is up
. Perhaps I should extend the concept:
alias up='cd ..'
alias up2='cd ../..'
alias up3='cd ../../..'
alias up4='cd ../../../..'
alias up5='cd ../../../../..'
alias up6='cd ../../../../../..'
You need to be careful if you setup any aliases like this. You will not always go up 5 directories when you cd ../../../../..
. If you are only 2 or 3 directories down from / you will wind up in /. Try this for yourself.
$ cd ~
$ pwd
/home/you
$ cd ../../../..
$ pwd
/
This happens because the parent directory of / is in fact /.
This function is for Bash, but something similar could be done for others (this may work as-is in ksh and zsh):
cdn () { pushd .; for ((i=1; i<=$1; i++)); do cd ..; done; pwd; }
Example usage:
/some/dirs/and/subdirs$ cdn 3
/some/dirs/and/subdirs /some/dirs/and/subdirs
/some
/some$ popd
/some/dirs/and/subdirs$
Here's a function that will cd
to a named subdirectory above the current working directory:
cdu () { cd "${PWD%/$1/*}/$1"; }
Example usage:
/usr/share/atom/resources/app/apm/src/generator$ cdu apm
/usr/share/atom/resources/app/apm$ cdu resources
/usr/share/atom/resources$ cd -
/usr/share/atom/resources/app/apm$ cdu share
/usr/share