Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»cd command in Linux with Examples

    cd command in Linux with Examples

    RahulBy RahulJune 28, 20173 Mins ReadUpdated:December 10, 2020

    cd is the basic command used by the Linux users. It is used to change the current working directory of a shell.

    Syntax:

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

    Before reading this tutorial, you must know few basics about directories on a Linux system. Get the basic understanding about 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 file path except /. For example: www, ./script.sh, ../../www etc.

    Now you must memorize these symbols and special characters use the cd command. What’re the symbols 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 is more examples to use with cd commands on Linux system.

    1. Sample cd command – Provide the remote directory path to switch current directory location of the working shell.
      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 it full path.
      cd /opt 
      cd /data/backup 
      cd /var/www/html 
      

      All the above examples, uses 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 root (/) file system.
      cd backup 
      cd www/html
      

      In the above example, first command will change change to “backup” directory available under current directory. The second command will change to “www/html”, which is located in 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 same as “cd public_html” where second command navigates shell to backup directory available under parent directory.

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

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

    Conclsion

    This tutorial descirbes you to uses of cd command on Linux system.

    cd cd command command linux command
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Setup Commit Username and Email for Git
    Next Article How to convert PPK to PEM file using Command

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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