Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing to parent directory in unix

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.

like image 699
Vijay Avatar asked Dec 09 '09 06:12

Vijay


People also ask

How do I go to parent directory?

To go up one level of the directory tree, type the following: cd .. The special file name, dot dot ( .. ), refers to the directory immediately above the current directory, its parent directory.

How do I change directory to parent folder?

To change your current working directory to its parent folder (move one branch down the directory tree): > cd .. To move the file data.

How do I go to parent directory in Shell?

You can go back to the parent directory of any current directory by using the command cd .. , as the full path of the current working directory is understood by Bash . You can also go back to your home directory (e.g. /users/jpalomino ) at any time using the command cd ~ (the character known as the tilde).


2 Answers

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 ../../../../../..'
like image 23
wallyk Avatar answered Sep 23 '22 03:09

wallyk


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
like image 129
Dennis Williamson Avatar answered Sep 23 '22 03:09

Dennis Williamson