Tips: The power of “cd -” command

I’m often posting advanced tips, today I will talk about something very simple, but that I noticed even experienced terminal users are not aware of: the cd - command.

The cd command, short for “change directory” is used in both Windows Command Prompt and Linux terminals to navigate through the file system.

The following command will
bring you to a folder /var/log

~$ cd /var/log
Enter fullscreen mode
Exit fullscreen mode

This is basic, OK.

Let’s go further.

~$ cd src/foo/bar
src/foo/bar$ #do something, but you have to move to another folder
src/foo/bar$ cd /var/log
/var/log$ # do something, but you have to move back to src/foo/bar
/var/log$ cd ~/src/foo/bar
src/foo/bar: $ # do something, but you have to move to log folder
src/foo/bar$ cd /var/log
/var/log$ # whatever
Enter fullscreen mode
Exit fullscreen mode

OK these are basic moves.

You all know cd .., cd ~, or cd, but do you know about cd - ?

cd - will bring you back to previous folder.

Let’s try the same navigation example

~$ cd src/foo/bar
src/foo/bar$ #do something, but you have to move to another folder
src/foo/bar$ cd /var/log
/var/log$ #do something, but you have to move back to src/foo/bar, we could use cd ~/src/foo/bar, but let's try cd -
/var/log$ cd -
src/foo/bar$ #do something, but you have to move to log folder, cd - will bring you back to 
src/foo/bar$ cd -
/var/log$ # whatever
Enter fullscreen mode
Exit fullscreen mode

This something I’m using multiple times a day


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Please set api key.