sodcalculator

10 Tech How-To

How to Change Directory in Linux Terminal

Guide 10-60105 · Updated · Step by step

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

  1. Open the terminal. Press Ctrl+Alt+T (on most Linux distributions) or find “Terminal” in your applications menu. On macOS, use Cmd+Space and type “Terminal”.
  2. Check your current directory. Type pwd (print working directory) and press Enter. This shows where you are, e.g., /home/username.
  3. List the contents. Use ls to see folders and files in the current directory. This helps you know which directory names you can use.
  4. Change to a subdirectory. Type cd foldername (replace foldername with the actual directory name) and press Enter. For example, cd Documents moves into the Documents folder.
  5. Use a full path. To jump directly to a directory from anywhere, type cd /path/to/directory. For instance, cd /var/log takes you to the log directory.
  6. Go up one level. Type cd .. to move to the parent directory. Repeat cd ../.. to go up two levels.
  7. Return to your home directory. Simply type cd (with no arguments) or cd ~ to jump back to your home folder.
  8. Go to the previous directory. Type cd - to switch to the last directory you were in. This is handy when toggling between two locations.
  9. Use tab completion. Start typing a directory name and press Tab to auto-complete it. If multiple options exist, press Tab twice to see them.
  10. 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" or cd My\ Documents.

Common Mistakes

  • Forgetting the path. Typing cd Documents when you’re not in the parent directory will fail. Use pwd to 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 cd with a file name. cd only works with directories, not files. Use cat or less to view files.
  • Assuming cd changes 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 ~/Documents to 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 .bashrc file.
  • Use pushd and popd to maintain a stack of directories for quick navigation.
  • Remember that cd is 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

The whole Tech How-To bed →