Linux Commands – cd
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed ls command in Linux which is used for listing files and directories in Linux.
https://cloudaffaire.com/linux-commands-ls/
In this blog post, we will discuss cd command in Linux. cd stands for change directory and is used to change the current working directly. You can use cd command to navigate in Linux filesystem.
Linux Commands – cd:
You can use cd /some/dir to navigate to the target directory. The path provided to the cd command can be absolute (full path) or relative path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
######################### ## Linux Commands | cd ## ######################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##--- ## cd ##--- ## cd [options] [dir] pwd ## currently in user home directory (/home/user) mkdir -p mydir1/mydir2/mydir3 ## create three directory one inside another in order cd mydir1/mydir2/mydir3 ## navigate to mydir1/mydir2/mydir3 path relative to /home/user/ cd /home/user/mydir1/mydir2/mydir3 ## same using absolute path cd ~/mydir1/mydir2/mydir3 ## will also work pwd ## returns /home/user/mydir1/mydir2/mydir3 |
You can use cd – to return to previous working directory and cd — to return to the directory from where cd – was last executed.
1 2 3 4 5 |
pwd ## returns /home/user/mydir1/mydir2/mydir3 cd - ## returns to previous working directory (/home/user) pwd ## returns /home/user cd -- ## returns back to the directory from where cd - was last executed pwd ## returns /home/user/mydir1/mydir2/mydir3 |
cd command without any option and argument sets the user’s home directory as current working directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
cd ## returns to user's home directory as set in environmet variable HOME pwd ## returns /home/user echo $home ## returns /home/user export HOME=~/mydir1 ## change the home directory to /home/user/mydir1 cd ## returns to user home directory which is now /home/user/mydir1 pwd ## returns /home/user/mydir1 cd ~ ## also represents user's home directory currently /home/user/mydir1 pwd ## returns /home/user/mydir1 export HOME=/home/user ## change the home directory to previous value, change user as per you username ## this time need to give the full path of home directory as ~ represent new home directory cd ## navigate to home directory (/home/user) pwd ## returns /home/user echo ~ ## returns /home/user echo $HOME ## returns /home/user |
cd . represents the current working directory and cd .. represents parent directory of the current working directory. You can navigate two or more step up to parent directory using cd ../..
1 2 3 4 5 6 7 8 |
cd mydir1/mydir2/mydir3 pwd ## returns /home/user/mydir1/mydir2/mydir3 cd . ## remains in current working directory pwd ## returns /home/user/mydir1/mydir2/mydir3 cd .. ## move one step up to parent directory pwd ## returns /home/user/mydir1/mydir2 cd ../.. ## move two step up to parent directory pwd ## returns /home/user |
cd supports two option parameters -L and -P. cd -L forces symbolic links to be followed, this is the default behavior of cd. cd -P use the physical directory structure without following symbolic links.
1 2 3 4 5 6 7 |
ln -s ~/mydir1/mydir2/mydir3 mylink ## create a softlink to mydir1/mydir2/mydir3 ls -l ## returns mylink -> /home/user/mydir1/mydir2/mydir3 cd -L mylink ## equivalent to cd mylink pwd ## returns /home/user/mylink cd - ## returns to previous working directory (/home/user) cd -P mylink ## follows the physical directory structure pwd ## returns /home/user/mydir1/mydir2/mydir3 |
cd command returns 0 if executed successfully and 1 if failed to execute.
1 2 3 4 5 |
cd echo $? ## returns 0 as previous cd command executed successfully cd not_present ## not_present: No such file or directory echo $? ## returns 1 as previous cd command failed to execute rm -r my* |
Hope you have enjoyed this article. In the next blog post, we will discuss mkdir command in Linux.