What is difference between these command: I used cd $HOME/directory/test/ before running each lines:

cd ~
cd /
cd ~/
cd -
cd --
cd /.
cd $HOME
cd $USR

And all of these do the same behavior. What is difference?

  • please ask a single question. – Avinash Raj Jun 14 '14 at 9:36
  • 10
    @AvinashRaj Eight separate questions, one per command? Having this one question seems better to me. Those commands are all very similar and do similar things. This question is about why they all seem to do the same thing. And there's a good, thorough answer that seems to answer the whole question, which is usually a clue that it really is in effect one question. On the other hand, we might find some earlier question to dupe this to... – Eliah Kagan Jun 14 '14 at 11:05
  • 6
    @AvinashRaj: This is a single question. – Jack Jun 14 '14 at 18:36

The simple command cd <dir> which changes directory to <dir>.

  • ~ indicates $HOME directory
  • / indicates the root directory
  • ~/ indicates the $HOME directory as well. The only difference is that it explicitly shows it's a directory (the trailing slash). cd ~/ and cd and cd ~ and cd $HOME all do exactly the same thing.
  • cd - Changes the working directory to the previous working directory.

These special symbols "." (dot) and ".." (dot dot)[Relative Parameters]:

The "." symbol refers to the current directory and the ".." symbol refers to the current directory's parent directory.

$USER and $HOME are Environment-Variables

$USER = The name of the currently logged-in user. This variable is set by the system. You probably shouldn't change its value manually. (ex:myuser1)

$HOME = The location of the currently logged-in user's home directory.(ex: /home/myuser1)

Recommended to use cd "$HOME" or cd "$USER" so-that cd gets proper input in case of space, etc.

  • thanks, And one more what is difference cd / and cd //. – αғsнιη Jun 14 '14 at 9:38
  • 1
    @KasiyA // is treated as /. So cd//home is same as cd/home – Pandya Jun 14 '14 at 9:58
  • 4
    @Pandya You may want to add information about what happens when you cd to an undefined environment variable. (The variable expands to an empty string and cd with no arguments is run, which changes to the home directory.) This may be what's going on with cd $USR, since while the USER variable is almost always defined, USR rarely is. – Eliah Kagan Jun 14 '14 at 11:07
  • I didn't knew about -. Is this same as .. then? – Jack Jun 14 '14 at 18:35
  • 4
    @Jack: No, they do different things. cd - takes you to the last directory where you were working in (given by $OLDPWD). It also prints out the old directory path. .. refers to the parent directory; cd .. will move you one level up. If you were working in the parent directory before, cd - and cd .. will both have the same effect. – Amal Murali Jun 14 '14 at 21:08
cd ~

Changes to your home directory. ~ at the beginning of a path is an abbreviation meaning “the user's home directory”.

cd /

Changes to the root directory /. Nothing special going on here.

cd ~/

The trailing / doesn't make any difference. It forces ~ to be interpreted as a directory, but cd does that anyway. (A trailing / makes a difference on a symbolic link to a directory — compare ls -ld /var/spool/mail and ls -ld /var/spool/mail/.)

cd -

Changes to the directory that you were in before the previous cd command. This is a special case of the cd command: when its argument is -, it does this.

cd --

With most commands, including cd, the argument -- means that anything that comes afterwards will be treated as an operand rather than an option. So for example cd -- -P means to change into a directory called -P, whereas cd -P passes the -P option (which makes a difference if the path that you change into goes via a symbolic link). When there is no argument after --, the -- doesn't do anything; this command is equivalent to plain cd. cd with no argument, in turn, is a shortcut for cd ~.

cd /.

Equivalent to cd /, since /. is the same directory as / (. is mostly useful on its own, to mean “the current directory”).

cd $HOME

Changes to your home directory. This fails if the path to your home directory contains a space or other characters. Always use double quotes around variable substitutions: cd "$HOME".

cd $USR

In all likelihood, this does nothing because no variable named USR is defined in your shell, hence the command that runs is just cd.

  • ... a space or other characters. like what characters? – αғsнιη Jun 14 '14 at 13:46
  • I remember that in some cases (bash scripts) $HOME/<Some Directory or File> didn't work and I had to use /home/$USER/<Some Directory or File> instead. This is also true for ~/<Some Directory or File> So there must be certain differences where it's advisable to use one and not the other (which I don't know) - am I wrong? – Sadi Jun 14 '14 at 13:51
  • @KasiyA $foo outside quotes mangles strings that contain whitespace or wildcards: \[*? (and more under some shell configurations). – Gilles Jun 14 '14 at 13:56
  • 2
    @Sadi In a bash script, "$HOME" always works to mean the home directory of the calling user. "/home/$USER" does the same thing only if the user's home directory is at this location (which isn't always the case on institutional networks). Unless you need to do something related to a specific installation's setup, /home/$USER is never right. – Gilles Jun 14 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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