cd command tutorial in linux/unix with examples and use cases

linux cd command – switch directory

Linux cd command is used to switch the current working directory to dirName.

Syntax

 cd OPTION

dirName: can be absolute or relative. If the directory name is omitted, it means to switch to the root directory of the currently logged in user.

Options

  • dirName
    Relative or absolute address of the directory to switch to
  • “-“
    Last used directory
  • “.”
    Current directory
  • “..”
    Parent directory
  • “~”
    Home directory of the currently logged in user
  • “/”
    System root directory

Examples & Use Cases

Switch to “/var/log”

In the following example we will switch from the current directory to the /val/log directory

 ➜ cd /var/log/

Switch to the last used directory

In the example below we will switch from the currently used directory to the last used directory.

 ➜  cd -  

Switch your home directory

In the following example we will switch your root directory, which is the home directory.

 ➜  cd ~ 

Switch to the upper two directories

In the following example we will switch to the upper two levels of the current directory.

➜  cd ../..

Switch to the “log” directory of the current directory

In the following example, you can use the following two commands to switch to the “log” directory under the current directory

➜  cd ./log

# OR

➜  cd log

Add a Comment

Your email address will not be published. Required fields are marked *