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.
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.
For Bourne-type shells (including
ksh
), you could write a shell function:I wrote a shell utility for this use case: https://github.com/kkew3/cdup. As some examples:
up -4
.site*
, useup '/site*/'
.[pP]roj(ect)?
, useup -E '[pP]roj(ect)?'
.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.This happens because the parent directory of / is in fact /.
You can check out the recent project
bd
and you want to go to
site
directory quickly (instead of typingcd ../../../..
),then just type:
This is generally how I do it. Of course in ksh you may have your navigation keys set to something else. When I used ksh I used have them set to vi style so that would be
k
instead of up arrow.In shell scripts it is better to be explicit. If you can use an absolute pathname then do so and run a command like:
If the script may be run to act on files in different directories, then you could consider something like this:
But if you really can't do either of those then stick to .. chains like so:
This is quite clear. Perform an action in the current working directory, then navigate up 3 levels and select the cgi-bin directory. Anyone who would be capable of understanding what you are doing in the shell script should have no difficulty in following this. If your script is really complex, then it would help to add some comments like this:
The implication is that you were in the code directory and changed down one level to python, therefore the reader who forgot where you were in the directory hierarchy sees a reminder to help them count up 3 levels.
This function is for Bash, but something similar could be done for others (this may work as-is in ksh and zsh):
Example usage:
Here's a function that will
cd
to a named subdirectory above the current working directory:Example usage: