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.
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
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 /.
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
If you don t like typing or remembering file names, you can navigate directories with something like NerdTree?
You could define aliases to simplify this kind of cd
operation.
Note that it would be easy to make errors with an alias in terms of ../../../..
etc, because you would have to be very sure of the relationship between your current directory and where you wanted to be.
Better to use absolute paths with alias
use cd /
to go to the root of you filesystem, and cd ~
to go to you home directory.
Example: to go to you log director just do cd /var/log
.
For Bourne-type shells (including ksh
), you could write a shell function:
cdup() {
set -- "${1-1}"
while test "$1" -gt 0; do
cd ..
set -- "$(($1-1))"
done
}
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:
cd /webdata/cgi-bin
If the script may be run to act on files in different directories, then you could consider something like this:
TOPDIR="/webdata"
cd $TOPDIR/cgi-bin
But if you really can t do either of those then stick to .. chains like so:
chmod +x *.py
cd ../../../cgi-bin
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:
# change to TOPDIR/cartsys/production/code/python
cd python
chmod +x *.py
cd ../../../cgi-bin
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.
cd ..
<up arrow><ENTER>
<up arrow><ENTER>
<up arrow><ENTER>
<up arrow><ENTER>
I wrote a shell utility for this use case: https://github.com/kkew3/cdup. As some examples:
up -4
.site*
, use up -g site*
.[pP]roj(ect)?
, use up -E [pP]roj(ect)?
.I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...
how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...
I need to put the contents of all *.as files in some specified folder into one big file. How can I do it in Linux shell?
How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks
The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?
I like to write bash shell scripts on my iPhone, put them in an app folder with an icon, plist, etc., so they execute like apps by tapping their icon on SpringBoard. This is not interactive like ...
Whenever I start a shell in vim using :sh, it doesn t source my ~/.bashrc file. How can I get it to do this automatically?
在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......