What is difference between these command?
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 ~/
andcd
andcd ~
andcd $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.
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
.