foxfirefey: A fox colored like flame over an ornately framed globe (Default)
[personal profile] foxfirefey posting in [community profile] command_liners
I learned this one recently from a coworker:

cd -


This will allow you to switch to your previous current working directory. So:

# Go into first directory
cd ~/path/to/first/dir
# Go into second directory
cd ~/path/to/second/dir
# This takes us back to the first directory
cd -
# This takes us back to the second directory
cd -


This is really useful if you have two main directories you're currently working in.

Date: 2009-06-25 06:56 pm (UTC)
azurelunatic: Vivid pink Alaskan wild rose. (Default)
From: [personal profile] azurelunatic
Ooo, useful!

Date: 2009-06-25 06:57 pm (UTC)
wychwood: chess queen against a runestone (Default)
From: [personal profile] wychwood
That does sound convenient!

Date: 2009-06-25 06:58 pm (UTC)
yvi: Dreamsheep Teal'c (Dreamsheep - Sheepl'c)
From: [personal profile] yvi
Very useful, thanks!

Date: 2009-06-28 10:18 pm (UTC)
marnanel: (Default)
From: [personal profile] marnanel
I didn't know about that. Thank you!

Another way of switching between two directories is to use "pushd" with no arguments, which switches the top two directories in the "dirs" stack. So:

cd ~/fred
pushd ~/barney
(now in ~/barney)
pushd
(now in ~/fred)
pushd
(now in ~/barney)

Works with BSD, too.

Date: 2009-06-29 06:14 am (UTC)
From: [personal profile] babysprite
You can also use the [up] arrow key to repeat the "cd -" command and any previous commands you have entered to the prompt.
/\__/\
(='.'=)
(")_(")
Music: Kathleen MacInnes - Gaol Ise Gaol ("My Love is She") from 'The Highland Sessions'

Date: 2009-06-29 10:22 am (UTC)
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)
From: [personal profile] pne
The Korn shell also has a useful extension to the cd command: if you give it two arguments, it'll look for the first argument in the current working directory and replace it with the second argument, then try to change directory to the result.

For example, if I'm in /home/pne/src/foo/monitor/impl and want to have a look at my co-worker's sources, I could cd pne rku and end up in /home/rku/src/foo/monitor/impl.

Or if I'm in /home/pne/proj/cool-app/src/com/example/coolapp/core/ui/buttons and want to have a look at the compiled classes, I could type cd src bin and end up at the appropriate position in the compiled-classes hierarchy (assuming a fairly typical(?) setup).

Pity that AFAIK bash hasn't adopted this syntax, especially since (again AFAIK) it doesn't conflict with any existing syntax.
Edited Date: 2009-06-29 10:22 am (UTC)

Date: 2009-07-01 07:14 pm (UTC)
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)
From: [personal profile] sophie
It doesn't, though you can still do it with the command cd ${PWD/pne/rku}, although that's admittedly not easy to remember at all.

Profile

command_liners: A command line prompt with a blinking cursor after it, green against black. (Default)
Command Liners

January 2022

S M T W T F S
      1
2 345678
9101112131415
16171819202122
23242526272829
3031     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 10th, 2025 04:13 pm
Powered by Dreamwidth Studios