Ubuntu – difference between these command

command line

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?

Best Answer

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.