Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What Does cd do on Linux
The cd command stands for "change directory" and is used to navigate the file system on a Linux computer. When used with a specific directory path as an argument, cd will change the current working directory to that location. For example, the command cd /home/user/documents will change the current working directory to the "documents" folder located within the "user" folder in the root directory. If you use cd command without any argument it will take you to your home directory.
Basic cd Command Usage
Here are the most common ways to use the cd command
# Change to home directory cd ~ cd # Change to parent directory cd .. # Change to specific directory (absolute path) cd /home/user/documents # Change to subdirectory (relative path) cd documents # Change to root directory cd /
The Meaning of With cd
The "" (dash) symbol is a shortcut that can be used with the cd command to quickly switch between the current directory and the previous directory. For example, if you are currently in the directory /home/user/documents and you run the command cd /home/user/pictures, you can then use the command cd - to quickly switch back to the /home/user/documents directory.
This feature is particularly useful when you need to toggle between two directories frequently during your work session.
# Navigate to a new directory cd /home/user/pictures # Switch back to previous directory cd - # Switch back again (toggles between the two) cd -
Equivalent Command to cd
The equivalent command to cd - is cd "$OLDPWD". The $OLDPWD environment variable is automatically set by the shell to the previous working directory, so using this command will have the same effect as using cd -.
# These commands are equivalent cd - cd "$OLDPWD"
Examples Using $OLDPWD
Here are some practical examples
# Start in /home/user/documents pwd # Output: /home/user/documents # Change to different directory cd /home/user/pictures # Return to previous directory using $OLDPWD cd "$OLDPWD" pwd # Output: /home/user/documents
Key Points
The cd command is case-sensitive directory names must match exactly
You need appropriate permissions to access the target directory
Using
cdwithout arguments takes you to your home directoryThe
$OLDPWDvariable stores the path of the previous working directoryBoth
cd -andcd "$OLDPWD"provide the same functionality
Conclusion
The cd command is essential for Linux navigation, allowing you to change directories efficiently. The dash () shortcut and $OLDPWD variable provide convenient ways to switch between your current and previous directories, making file system navigation much more efficient during terminal sessions.
