Difference between commands
The difference is that cd
is a single command, no arguments. If there is no arguments, cd
will go to your home folder by default. Now, by contrast ..
always means "one directory up" or parent directory. So when you do cd ..
it will go up one level. And this is always true, with exception of /
folder, because there is nowhere else to go. Here's a demo:
$ pwd
/etc
$ cd ..
$ pwd
/
$ cd ..
$ pwd
/
Using this knowledge in practice
So where are the two commands useful ? cd
will jump back to your home directory, which is the same as doing cd ~
or doing cd $HOME
. This means that if you are somewhere very very deep in directory tree, you can quickly and easily go back to home directory. Here's an example:
$ pwd
/sys/class/block/sda1/holders
$ cd
$ pwd
/home/xieerqi
With ..
you can also do cool things. Let's say I am in /sys/class/block/sda1/holders
folder and I want to quickly go to up 3 levels. So we can do this:
$ pwd
/sys/class/block/sda1/holders
$ cd ../../../
$ pwd
/sys/class
What also can be done is to make a function out of this. We can navigate easier by specifying a number of levels to go up. Here it is:
goup() {
num=$1
while [ $num -ne 0 ]
do
cd ..
num=$(expr $num - 1 )
done
}
Knowing that cd ..
goes up one level, why not specify how many times we want to repeat cd ..
? That's exactly what this while loop does. And here it is in action:
$ pwd
/sys/class/block/sda1/holders
$ goup 4
$ pwd
/sys
cd
andcd ..
. I edited the question to explicitly ask for an explanation of the former. – David Foerster Jul 11 '17 at 15:38