That's a fantastic question, and it points to a really handy shortcut in the Linux terminal!
The cd - command is most useful when you frequently need to switch back and forth between two directories.
Imagine you are working on a project. You might have your main project code in one directory, say /home/labex/project/my_app, and your configuration files in another, perhaps /home/labex/config_files.
Here's how cd - becomes incredibly useful:
- You are in your project directory:
pwd # Output: /home/labex/project/my_app - You need to quickly jump to your config files:
cd /home/labex/config_files pwd # Output: /home/labex/config_files - Now, you've made some changes to the config and want to go right back to your project directory. Instead of typing the full path again, or even a relative path, you can just do:
cd - pwd # Output: /home/labex/project/my_app (You've toggled back!) - And if you need to go back to the config files again:
cd - pwd # Output: /home/labex/config_files (You've toggled again!)
It essentially acts as a "previous directory" button, allowing you to instantly jump to the directory you were in just before your last cd command. This saves a lot of typing and makes navigating between two specific locations much faster and more efficient!
It's a small command, but it can significantly speed up your workflow once you get used to it!