Main Content

pushd and popd

Archive - Originally posted on "The Horse's Mouth" - 2004-10-05 06:54:00 - Graham Ellis

On every programming course, you're taught never to use the goto statement even if the language has one available, as you're creating spaghetti code that's hard to follow and maintain. I agree with the sentiment.

So .... why, oh why, .... when you're navigating around directories at a command prompt do you use the cd command - it's the shell's equivalent of directory goto as it leaves you jumping away from your current directory, and not leaving a return address for when you want to go back.

Have you come across pushd and popd? When you "pushd" to a directory, you save away on a stack the name of the directory you're moving from; when you "popd" you return. Simple, effective, ang great when you want to pay a temporary visit to a remote directory in a Unix or Linux shell.

P.S. They're supported in Microsoft's Windows XP too!