changing to parent directory in unix

45,978

Solution 1

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

Solution 2

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 ../../../../../..'

Solution 3

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 /.

Solution 4

You can check out the recent project bd

If you are in this path

/home/user/project/src/org/main/site/utils/file/reader/whatever 

and you want to go to site directory quickly (instead of typing cd ../../../..),
then just type:

bd site
Share:
45,978

Related videos on Youtube

Vijay
Author by

Vijay

http://theunixshell.blogspot.com/

Updated on July 09, 2022

Comments

  • Vijay
    Vijay almost 2 years

    in general we use

    cd .. for going to the parent directory

    cd ../../ to go to the parents parent directory. and

    cd ../../../../../ for 5th parent directory.

    is there any simplified way of doing this?

    shell i am using is ksh.

    • SourceSeeker
      SourceSeeker over 14 years
      You can leave off the slash at the end. Also, the answer to your question may depend on which shell.
    • VonC
      VonC over 10 years
      You can checkout the new command 'bd': see my answer below
  • Vijay
    Vijay over 14 years
    this cannot be considered as answer rather it should be a comment.
  • Vijay
    Vijay over 14 years
    i am accepting this since it seems we dont have a direct command line short cut for this.still i hope there is some way to do it in short way on command line.
  • istruble
    istruble over 14 years
    How would you suggest a cautionary 'comment' such as this be written? It seemed like it merited vertical space for clarity. Sometimes the real answer to a question is to figure out that you might not want to do things the way that you first thought you wanted to do things. I hope your knowing that your 'cdn 5' might only be a 'cdn 1' might save you more than just a few dots.
  • Henk Langeveld
    Henk Langeveld about 5 years
    I really like the cdu function. It is simple and only depends on cd and variable substitution.

Related