How to use cd -?

QuestionsQuestions8 SkillsProBasic Files OperationsSep, 02 2025
0116

To use the cd - command in Bash, follow these steps:

  1. Navigate to a Directory: Start by changing to a directory. For example:

    cd /home/user/documents
  2. Change to Another Directory: Move to a different directory:

    cd /home/user/downloads
  3. Return to the Previous Directory: Now, to go back to the directory you were just in (in this case, /home/user/documents), simply run:

    cd -

What Happens:

  • When you execute cd -, Bash will switch you back to the last directory you were in and display its path.

Example in Action:

cd /home/user/documents   # Current directory: /home/user/documents
cd /home/user/downloads    # Current directory: /home/user/downloads
cd -                       # Returns to: /home/user/documents

This command is particularly useful for quickly toggling between two directories. If you have more questions or need further examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!