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
cd
And all of these do the same behavior. What is difference?
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
cd
And all of these do the same behavior. What is difference?
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.
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
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
.
$foo
outside quotes mangles strings that contain whitespace or wildcards: \[*?
(and more under some shell configurations).
– Gilles 'SO- stop being evil'
Jun 14 '14 at 13:56
"$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 'SO- stop being evil'
Jun 14 '14 at 13:58