Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What do these two commands mean?

cd ../
cd /..

I am ending up in two directories upwards the tree on the first command and in the root directory from the second command. Why does that happen?

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

A / at the end of the name of a directory/folder is optional. Most of the time, including or omitting the final slash in a directory name does not change the effect of a command.

So cd ../ is equivalent to cd ...

Paths that don't start with a / are relative paths; they are resolved relative to the current directory.

Every directory has to special entries:

  • a directory entry called . that resolves to the directory itself
  • a directory entry called .. that resolves to the directory's parent (i.e., the directory that contains that directory)

Therefore cd ../ and cd .. change directory to the parent of the current directory. Thus, if you start out in /home/fazlan and run cd .. or cd ../, you'll end up in /home.

In contrast, /.. is an absolute path (albeit an unusual one). / is the filesystem root--the directory you get to if you go up in the directory hierarchy all the way. (Usin the same parent and child metaphor, we say / is the ancestor of everything in the filesystem.)

Since .. means "the parent directory of this directory" and / means "the top of the filesystem," /.. means "the parent directory of the top of the filesystem."

But what does it mean to talk about the directory that contains /? Well, / is an exception. Since no directory contains it, we say that /'s parent is itself. Therefore, in /, .. is /. Consequently, /.. is the same as /.

This is why cd ../ brings you up one directory from where you started, while cd /.. brings you to the very top. More elegant and easily read ways to do these things are cd .. and cd /, respectively.

share|improve this answer
    
+1 for bringing up relative paths. I wish I would have thought to mention that. –  BryceAtNetwork23 Apr 2 '13 at 17:04
    
@BryceAtNetwork23 Thanks. BTW, if you wish, you should definitely feel free to edit your answer to add any information you consider to be missing. –  Eliah Kagan Apr 2 '13 at 17:04
    
Made my girlfriend read this... now shw gets it.. Thanks! –  Scott Goodgame Apr 2 '13 at 22:13
add comment

The first is a relative path. The second is an absolute path.

The .. operator in that argument means move up one.

The slash when not at the front means division of sub-directory name.

The slash when at the front means "This is an absolute path. Start at root directory." The .. is discarded since you can't back out any further. (There was a time when lots of these allowed you to break chroots)

share|improve this answer
add comment

A single dot (.) represents the current directory. In fact, if you executed a cd . it would put you in your current directory (which would appear to do nothing). If you were to copy something from /dir1 to your current dir, you could do it like this:

cp /dir1/somefile.txt .

Executing a cd .. brings you up one directory level. A cd ../ does the same. If you wanted to go up two levels, you would add two more dots on the end, like this:

cd ../..

To return to the root directory, you can execute a cd /. Now let's say I executed the following:

cd /dir1/dir2/dir3/..

That puts me in the /dir1/dir2 directory, because (as we established above) the .. tells it to go up one level. So it goes to dir3, and then goes up.

By invoking a cd /.. you are telling it to go to the root directory / and then up one level, which it cannot do. So it is effectively leaving you at the root dir, instead.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.