Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»cd command in Linux with Examples

    cd command in Linux with Examples

    By RahulJune 28, 20173 Mins ReadUpdated:August 8, 2022

    The cd command is the most basic command for working with directories in the shell. That’s because it’s used so often. Any time you need to change locations in a file system, you’ll be using a cd to do so. You might not know this, but the cd (change directory) command has many different uses and applications that extend beyond simply changing your current working directory.

    Advertisement

    In this article, we will introduce you to these various uses of the cd command in Linux and become familiar with its most common usages.

    Syntax:

    cd [-L|[-P [-e]] [[email protected]]] [dir]
    

    Before continuing, you should get a basic understanding of the absolute vs relative paths in Linux.

    • Absolute Path is the full path of directory and always starts with / in Linux system. For example: /tmp, /usr/bin, /var/www etc.
    • Relative Path is the partial path of any directory or subdirectory. It can be started from any location of the file path except /. For example: www, ./script.sh, ../../www etc.

    Now you must memorize these symbols and special characters and use the cd command. What’s the symbol’s meaning when used with the cd command?

    • Single dot (.) : current directory (present directory).
    • Double dot (..) : parent directory.
    • Tiled (~) sign : home directory of current users.
    • $HOME variable: home directory of current users.

    cd Command Examples

    Here are more examples to use with cd commands on the Linux system.

    1. Sample cd command – . Provide the destination directory path to switch the current directory location of the working shell. For example, change to the /opt directory:
      cd /opt 
      
    2. Using absolute directory path – Any directory path started with the root (/) file system is known as absolute directory path. In other words, you can say its full path.
      cd /opt 
      cd /data/backup 
      cd /var/www/html 
      

      All the above examples use directory location with root (/) are the absolute directory paths.

    3. Using relative directory path – Any directory path refers from current directory is called relative path. The relative path never started with the root (/) file system.
      cd backup 
      cd www/html
      

      In the above example, the first command will change to the “backup” directory available under the current directory. The second command will change to “www/html”, which is located in the current directory.

    4. Home directory – You can also use $HOME environment variable for navigation useful for the scripting. Here $HOME always refers to the home directory of the logged-in user.
      cd $HOME 
      cd $HOME/public_html 
      

      The “~” also refers to the logged-in user home directory and can be used with cd command.

      cd ~ 
      cd ~/public_html 
      
    5. Using . and .. in directory path – The . refers to the current directory and .. refers to the parent directory except /root. This is generally used with relative paths.
      cd ./public_html 
      cd ../backup/files 
      

      The first command is the same as “cd public_html” where the second command navigates the shell to the backup directory available under the parent directory.

      We can also use multiple .. (as much as required) to navigate to any directory structure.

      cd ../../../backup/files 
      

    Conclsion

    Change directory is the most used command in Linux systems. In this tutorial, we have described the cd command on a Linux system.

    cd cd command command linux command
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Sed Command to Delete Lines in a File

    Sed Command to Delete Lines in a File

    Sort Command in Linux with Practical Examples

    15 Practical Examples of Sed Command

    Sed Command in Linux with 15 Practical Examples

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.