What does "cd -" do in Linux?
- User
What does "cd -" do in Linux?
Assistant
A neat Linux trick!
In Linux,
cd -is a command that might look mysterious at first, but it's actually quite useful. Here's what it does:cd -switches between the current directory and the previous working directory.More specifically:
-
First use: When you run
cd -for the first time in a shell session, it will simply print a message indicating the current directory, and then do nothing else, because there's no "previous" directory to switch to yet. The output will look something like:/current/working/directory -
Subsequent uses: After navigating to a different directory using
cd(e.g.,cd /path/to/new/directory), runningcd -will then change the directory back to the previous working directory. Essentially, it toggles between the last two directories you were in.
Example Use Case:
- You start in your home directory:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.