Changing directories in the Linux terminal is a fundamental skill that lets you navigate the file system efficiently. This guide covers the cd command, its syntax, and practical examples to help you move between folders with confidence.
What You’ll Need
- A Linux system (or a terminal emulator on macOS/WSL)
- Basic familiarity with opening a terminal window
- Knowledge of your file system layout (optional but helpful)
Step-by-Step
- Open the terminal. Press
Ctrl+Alt+T(on most Linux distributions) or find “Terminal” in your applications menu. On macOS, useCmd+Spaceand type “Terminal”. - Check your current directory. Type
pwd(print working directory) and press Enter. This shows where you are, e.g.,/home/username. - List the contents. Use
lsto see folders and files in the current directory. This helps you know which directory names you can use. - Change to a subdirectory. Type
cd foldername(replacefoldernamewith the actual directory name) and press Enter. For example,cd Documentsmoves into the Documents folder. - Use a full path. To jump directly to a directory from anywhere, type
cd /path/to/directory. For instance,cd /var/logtakes you to the log directory. - Go up one level. Type
cd ..to move to the parent directory. Repeatcd ../..to go up two levels. - Return to your home directory. Simply type
cd(with no arguments) orcd ~to jump back to your home folder. - Go to the previous directory. Type
cd -to switch to the last directory you were in. This is handy when toggling between two locations. - Use tab completion. Start typing a directory name and press
Tabto auto-complete it. If multiple options exist, pressTabtwice to see them. - Handle spaces in names. If a directory name contains spaces, enclose it in quotes or escape the space with a backslash. For example,
cd "My Documents"orcd My\ Documents.
Common Mistakes
- Forgetting the path. Typing
cd Documentswhen you’re not in the parent directory will fail. Usepwdto confirm your location. - Using backslashes on Linux. Windows uses backslashes (
\), but Linux uses forward slashes (/) in paths. - Missing spaces in quoted names. If you don’t quote a name with spaces, the shell treats it as multiple arguments and errors.
- Typing
cdwith a file name.cdonly works with directories, not files. Usecatorlessto view files. - Assuming
cdchanges the shell’s working directory for other terminals. Each terminal session has its own working directory; changing it in one doesn’t affect others.
Tips
- Use
cd ~/Documentsto go to a subdirectory of your home from anywhere. - Create aliases for frequently visited directories. For example, add
alias work='cd /home/username/projects/work'to your.bashrcfile. - Use
pushdandpopdto maintain a stack of directories for quick navigation. - Remember that
cdis a shell built-in, not an external program, so it only affects the current shell.
Troubleshooting
“No such file or directory” – This means the path you typed doesn’t exist. Double-check spelling, case sensitivity, and your current location with pwd.
“Permission denied” – You may not have execute permission on the directory. Use ls -l to check permissions and consider using sudo (if appropriate) or ask the administrator.
Command not found – If you see cd: command not found, your shell environment is broken. Try /bin/cd or check your PATH variable.
Tab completion not working – Ensure you’re using a shell that supports it (like Bash or Zsh) and that the directory name is correct.
Frequently Asked Questions
How do I go to the root directory?
Type cd / to jump to the root of the filesystem.
What does cd ~ do?
It changes to your home directory, which is the default location when you open a terminal.
Can I use cd to go to a file?
No, cd is only for directories. To view a file, use cat, less, or an editor.
How do I navigate to a directory with spaces?
Quote the entire path, e.g., cd "My Folder", or escape spaces with backslashes: cd My\ Folder.
Why does cd .. not work?
It should work unless you’re at the root (/). Check your current directory with pwd.
What is the difference between cd and pwd?
cd changes the directory, while pwd prints the current working directory.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Chromebook Tips and Tricks: 25+ Real Fixes
- FaceTime on Samsung: How to Join a Call
- iPhone Favorite Contacts Not Working? Fixes
- BitLocker USB Decryption: Step-by-Step Guide
- Windows Could Not Start the Service on Local Computer Error: Fixes
- How to Unblock Someone on Snapchat: Step-by-Step