The cd (change directory) command is used to navigate between directories in the Linux terminal. It's essential for moving around the file system. Below, we'll cover common uses with examples. Assume you're starting in /home/user/Documents for these examples.
Use cd followed by the absolute or relative path to the directory.
cd /path/to/directoryExample:
If you want to go to /home/user/Pictures:
cd /home/user/PicturesBefore: /home/user/Documents
After: /home/user/Pictures (check with pwd)
Use cd .. to move up one level in the directory tree.
cd .. # Change to parent directoryExample:
From /home/user/Documents:
cd ..Before: /home/user/Documents
After: /home/user (check with pwd)
Use cd ~ to quickly return to your home directory.
cd ~ # Change to home directoryExample:
From any location, like /var/log:
cd ~Before: /var/log
After: /home/user (assuming your home is /home/user)
Use cd - to switch back to the directory you were in before the last cd command.
cd - # Change to previous directoryExample:
If you were in /home/user/Documents, then did cd /tmp, then:
cd -Before: /tmp
After: /home/user/Documents
Use cd / to go to the root of the file system.
cd / # Change to root directoryExample:
From /home/user/Documents:
cd /Before: /home/user/Documents
After: /
Tip: Always use pwd (print working directory) to confirm your current location. Practice these in a safe environment, like a virtual machine, to avoid confusion.