I am completely new to unix/linux OS. I have installed Ubuntu 16.04.2 LTS in my system.
What is the difference between cd and cd .. ?
I have opened a terminal and executed the following command: pwd.
It gave me /home/avinash
Now I executed cd .. and it gave me /home$
Then I executed the command cd and then gave pwd, which returned /home/avinash/
Can anyone please explain the difference between cd and cd .. ?
4 Answers
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/xieerqiWith .. 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/classWhat 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 - After execution
cdyou'll get into your home directory - After execution
cd ..you'll get one directory up in the directory-tree
cdcommand will take you back to your home directory directly, its doesnt matter where ever you are.cd ..will take you back just one step back, i.e to parent directory of current directory.
This is one command, but .. is a parameter of cd. Parameter .. always means that you want go one catalog up in linux.
Command cd get you into your home directory:
root@test:/etc/init# cd
root@test:~# Command cd with parameter .. get you one directory up in the directory-tree where you are actual
root@test:/etc/init# cd ..
root@test:/etc# cd /etc/initThere is another parameter like . whose means that you always want go to current catalog. You can see this when you list tree:
root@test:/etc/init# ls -la
razem 248
drwxr-xr-x 2 root root 4096 lip 10 15:06 .
drwxr-xr-x 140 root root 12288 lip 10 15:07 ..
-rw-r--r-- 1 root root 338 kwi 9 2016 acpid.conf
-rw-r--r-- 1 root root 278 gru 28 2014 anacron.conf
-rw-r--r-- 1 root root 3709 mar 16 04:05 apparmor.conf
-rw-r--r-- 1 root root 1626 kwi 10 14:16 apport.confLike you see, always you have indexed on system file current catalog like ., then forward to catalog up using .., and then next catalog/files forwarded down in unix tree.