Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)
In general, I used the

cd .. for moving to your parent directory

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

cd ../../../../../ for fifth parent directory.

is there any other way of doing this?

The shell I am using is ksh.

1 Answer

0 votes
by (36.8k points)
edited by

This is the function used for Bash, but something similar could be done for others, for example:

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 is 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

Want to be a Linux expert? Come and join this Linux course

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...