cd

Description

The cd command, also known as chdir(change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

The syntax for the command is:

cd [directory]

Use cases

  1. Imagine you have a folder (videogames) on your computer where you keep all your video games. You use the cd command to go there so you can play your favorite games.

    cd videogames
    
  2. Suppose you have a folder (music) where you store your music. You use cd to enter that folder to listen to your favorite songs.

    cd music
    
  3. Imagine you are working in a project folder named my_project that is located within a projects directory, which, in turn, is inside your Documents directory. To move up to the parent directory (i.e. Documents), you can use the cd .. command.

    cd ..
    
  4. You're working in two directories: source and destination. You want to quickly switch back and forth between them. You can use cd - to toggle between these directories.

    cd source
    cd - # Switches to "destination"
    cd - # Switches back to "source"
    

Additional Section

  1. Navigating to the root Directory

    To navigate to the root directory, which is the top-level directory in the file system, we can use cd / command.

    cd /
    
  2. Navigating to home Directory

    To navigate to home folder, where personal files and settings are stored, we can use the cd command with either username or the tilde (~) symbol.

    cd susheel
    

    OR

    cd ~
    

    OR

    cd
    
  3. Navigating Directory with spaces in them

    Suppose you have a directory named `My Documents`` with a space in the name, and you want to navigate to it. To do this, you need to enclose the directory name in quotes.

    cd "My Documents"
    

    Note: You can also use escape character to achieve same result

    cd My\ Documents
    
  4. Navigating using absolute paths and relative paths

    1. Absolute Path

      • An absolute path specifies the complete directory structure from the root directory to the target directory.
      • It always begins with a forward slash (/) in the Unix-like systems.

      Example

      Suppose you have the following directory structure:

      /
      ├── home
      │   └── user
      │       ├── documents
      │       └── pictures
      └── var
         └── logs
      

      To navigate to the pictures directory using an absolute paths:

      cd /home/user/pictures
      
    2. Relative Paths

      • A relative path specifies the location of the target directory relative to your current working directory.
      • It doesn't start with a forward slash.

      Example

      If you are currently in the documents directory.

      /
      ├── home
      │   └── user
      │       ├── documents
      │       └── pictures
      └── var
         └── logs
      

      To navigate to the pictures directory using a relative path

      cd ../pictures
      

    Note: You can learn more about absolute and relative path from here.

Exercises

  1. You are in the videogames directory and want to navigate to the movies directory, which is a sibling of videogames. How would you use the cd command to achieve this?

    Directory Tree

     .
    ├── movies
    └── videogames
    
  2. You want to quickly switch between two directories named work and projects multiple times. How would you use the cd command to achieve this?

results matching ""

    No results matching ""