What is the cd command in Linux?
Overview
The cd command in Linux is used to navigate directories.
Syntax
cd folder_path
Parameters
folder_path: This is the path to the folder which we want to navigate to.
Options
-
cd ..: This moves one directory up. -
cd: This moves to the home folder. -
cd ~: This moves to a previous directory.
Code
Example 1
# print current directorypwdecho ............# navigate to current directorycd /usercode# list files in current directoryls
Explanation
-
Line 2: We print the current directory to the console.
-
Line 6: We navigate to the
/usercodedirectory. -
Line 8: We print the files in the
/usercodedirectory to the console.
Example 2
# print the current folderecho "print current folder..."pwd# list files in curren folderls# move to a directory one level upecho "changing folder..."cd ..# print files in the navigated directoryls
Explanation
-
Line 3: We print the current directory.
-
Line 6: The files in the current directory are printed to the console.
-
Line 10: We move to a directory one level up.
-
Line 13: We print all the files present in the directory.