1

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 .. ?

muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    And https://askubuntu.com/questions/483129/what-is-difference-between-these-command – muru Jul 11 '17 at 07:25
  • 1
    @muru. U have marked my question as duplicate and the link you provide does not explain the the 2 commands i have asked. And also the 2 questions linked to my question does not answer what i have asked – Avinash Reddy Jul 11 '17 at 11:04
  • @AvinashReddy: Pandya's answer explains both cd and cd ... I edited the question to explicitly ask for an explanation of the former. – David Foerster Jul 11 '17 at 15:38

4 Answers4

2

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
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
1
  • After execution cd you'll get into your home directory
  • After execution cd .. you'll get one directory up in the directory-tree
Yaron
  • 13,173
1
  1. cd command will take you back to your home directory directly, its doesnt matter where ever you are.

  2. cd .. will take you back just one step back, i.e to parent directory of current directory.

Raja G
  • 102,391
  • 106
  • 255
  • 328
0

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/init

There 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.conf

Like 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.