How can I change to the previous directory instead of going up?

214

43

I currently spend a lot of my working hours moving back and forth between two paths which are very far down the tree and divert from each other at root. It strikes me that my life would be a lot easier if there was an argument for cd that takes the user to the last directory they were in.

That is, if I'm in:

/etc/foo/bar/baz/moo

and then type:

cd /var/lib/fubarred_app/blargh/logs

I would like to be able to go back to the first directory without having to type the whole path again.

The memory key does not cut it since I use enough commands in each place that it's just as difficult to go back and find the path I want as it is to type it myself.

Is there a short command that would just let me go to the previous directory?

Yitzchak

Posted 2011-08-17T20:18:17.287

Reputation: 4 084

See also http://superuser.com/questions/113219/go-back-to-previous-directory-in-shell

– jiggunjer – 2015-05-16T18:43:55.430

8

in addition to @bryan's excellent answer, you might consider creating a pair of aliases for each path so you can just type something like: "bazmoo" or "blarghlog" at the prompt :)

– warren – 2011-08-17T20:30:53.243

4

Why don't you just use GNU screen?

– Shi – 2011-08-17T20:43:28.983

2Which shell? that makes a huge difference. – Randal Schwartz – 2011-08-18T02:22:40.300

@warren ditto. that's my preferred way to do everything. half of what i use my computer for has its own alias now... =) – ixtmixilix – 2011-08-19T15:58:13.437

Fasd is awesome. Check it out. – majkinetor – 2013-11-05T08:58:42.893

Answers

304

The command

cd -

will perform the swap you need on most of the mainstream shells, the older longer variant is

cd "$OLDPWD"

which will use the environment variable that contains the previous working directory.


The POSIX man page for cd mentions:

DESCRIPTION

If, during the execution of the above steps, the PWD environment variable is changed, the OLDPWD environment variable shall also be changed to the value of the old working directory (that is the current working directory immediately prior to the call to cd).

OPERANDS

  -  When a hyphen is used as the operand, this shall be equivalent to the command:

cd "$OLDPWD" && pwd 

which changes to the previous working directory and then writes its name.

bryan

Posted 2011-08-17T20:18:17.287

Reputation: 7 848

glad it helped :) – bryan – 2014-07-21T14:35:50.907

7Keep in mind this really depends on the shell you're using, and since you didn't specify, it'd be hard to give the non-existent "portable" answer. – Randal Schwartz – 2011-08-18T02:22:10.363

16@Randall, Perhaps you could combine all your individual shell comments into one Voltron-Shell answer. – hyperslug – 2011-08-18T02:46:14.440

@Randall. I was using bash. It works in ksh and zsh too. By the way, I first learned to program from the 1994 edition of Learning Perl so excuse me while I fanboy a bit. :) – Yitzchak – 2011-08-18T15:23:06.280

Hmm, I used cd $OLDPWD for this. It's nice that mainstream shells offer a way shorter form. – ulidtko – 2011-08-18T16:11:13.993

cd - a time saver command you can put an alias getback :D – Necronet – 2011-08-18T17:41:01.907

I wonder why Linux doesn't have popd and pushd. Those are cd on steroids, although only available on Windows. pushd changes the directory and puts the last directory on a stack, popd takes this last directory, and navigates back to it. – sinni800 – 2011-08-21T12:40:40.887

2@sinni Certain flavors do have pushd, and at least it's a builtin in bash. – slhck – 2011-08-24T07:00:39.733

@slhck huh. I tried in a bash on centos6 just now... and yeah, true, it's there... I think I didn't read good enough when there was a "-bash: pushd: no other directory" message and I somehow misplaced it with the "command not found" message. – sinni800 – 2011-08-24T08:07:10.847

105

In addition to bryan's answer, it's worth mentioning there's also pushd and popd, which build up directories like a stack. This is also available on Windows NT; however, it is not available in all shells.

For example, we can go to three different directories, and you'll always see your stack when you call pushd:

charon:~ werner$ pushd Documents/
~/Documents ~

charon:Documents werner$ pushd ../Movies/
~/Movies ~/Documents ~

charon:Movies werner$ pushd ../Downloads/
~/Downloads ~/Movies ~/Documents ~

And when you call popd three times in a row, you get to those directories in the stack in reverse order. At the same time, the stack will be emptied again.

charon:Downloads werner$ popd
~/Movies ~/Documents ~

charon:Movies werner$ popd
~/Documents ~

charon:Documents werner$ popd
~

charon:~ werner$ popd
-bash: popd: directory stack empty

If you are using Zsh; it has an AUTO_PUSHD option, which will automatically push cd's onto the stack.

slhck

Posted 2011-08-17T20:18:17.287

Reputation: 182 472

pushd and popd are not available in all shells. – Randal Schwartz – 2011-08-18T02:23:53.513

5@Wiesław: Really? I wish bash had that... Of course, I could alias cd to pushd, and it'd mostly be the same. – Chris Charabaruk – 2011-08-18T12:12:20.827

3Or alias pd to pushd and have both. BTW pushd (or pd, if you alias it) by itself swaps the current directory and last-pushed directory, essentially accomplishing what 'cd -' does. – JRobert – 2011-08-18T16:06:51.843

I like the concept but never got used to them. – Thorbjørn Ravn Andersen – 2011-08-18T18:39:25.777

13

There are some "jump" programs

  • autojump (maintained with basic features)
  • j2 (apparently unmaintained with some advanced features)
  • z (maintained version of "j" with advanced features)

These ease any kind of directory navigation. You use it by giving a portion of the path and it just works.

In your case

~$ j baz 
/etc/foo/bar/baz/moo$

~$ j bla 
/var/lib/fubarred_app/blargh/logs$

You can assign any letter you want to these programs, "j" is tradition :)

j2 and z support multiple search terms,...

~$ j baz src
/home/me/projects/baz/repository/trunk/src$

... and more options.

~$ j -l  # list directories by "frecency"(frequency + recency) score
~$ j -r PATTERN # match by rank only, not recency
~$ j -t PATTERN # match by recency only, not rank

hayalci

Posted 2011-08-17T20:18:17.287

Reputation: 1 652

2"j" is clearly shell specific. You didn't say that. – Randal Schwartz – 2011-08-18T02:23:37.023

3@Randal: How is it shell-specific? It's a program. – Fixee – 2011-08-21T15:42:56.150

3I'm an idiot. :) I see it now. – Randal Schwartz – 2011-08-24T00:23:52.943

10

In addition to cd - and cd $OLDPWD

You can use history search by pressing CTRL-R and typing a few letters of the cd command you entered before. Pressing CTRL-R repeatedly will bring older matches.

This method will be more useful if you have more than two paths to change.

musa

Posted 2011-08-17T20:18:17.287

Reputation: 501

1CTRL-R won't necessarily help. You might have been in /some/obscure/directory/deep and done "cd evendeeperdir" then gone to ~/somehere/different. – justintime – 2011-08-18T14:24:07.937

1Great! It works for other things than cd, great help! – Niloct – 2011-08-19T15:57:27.980

9

You can use this to easily make aliases for directories:

a() { alias $1=cd\ $PWD; }

a 1

and later:

1

Mr Example

Posted 2011-08-17T20:18:17.287

Reputation: 99

so this skips typing the alias = parts? hmmmm – Xen2050 – 2016-05-07T10:11:43.737

1Only on a shell that understands aliases and functions. Not all do. – Randal Schwartz – 2011-08-18T02:23:14.607

6

bashmarks will let you bookmark a series of folders and jump between them with tab completion:

To bookmark a folder, simply go to that folder, then bookmark it like so:

bookmark foo

The bookmark will be named "foo". When you want to get back to that folder use:

go foo

To see a list of the bookmarks:

bookmarksshow

Tab completion works, to go to the shoobie bookmark, simply:

go sho[tab]

Handyman5

Posted 2011-08-17T20:18:17.287

Reputation: 258

4

Another possibility would be to just keep two windows open, with one positioned at each directory.

Alger

Posted 2011-08-17T20:18:17.287

Reputation: 339

No shared history though, if you use/edit previous commands regularly (maybe a reset would get them...?) – Xen2050 – 2016-05-07T10:13:31.420

@Xen2050 See the shell option share_history in zsh. – Radon Rosborough – 2017-05-06T02:14:28.403

4

I know this isn't strictly speaking an answer to your question, but it's helpful in reaching the goal of taking you to your important directories.

in any descent UNIX-bash you can use CDPATH to extend the folder the cd-command searches in.

from my .bashrc-file:

export CDPATH='.:~/source/'

cd first searches in your current folder, then searches in my coding-project-directory.

cd myproject

.. takes me to ~/sources/myproject from where ever I am currently standing.

Simple little feature that has helped my directory-navigation a lot.

phareim

Posted 2011-08-17T20:18:17.287

Reputation: 149

1+1 This adds the CDPATH folder to cd's autocomplete (TAB) list, which is Good. But if the . is not included and if there's a folder in a CDPATH folder with the same name as one in your current directory, then cd samename will go to the CDPATH directory and not the one in the current directory, which is Bad. Have to use cd ./samename to get into a dup folder. – Xen2050 – 2016-05-07T10:21:04.350

Yeah, good comment. Always include the first ., or you'll experience weird stuff. – phareim – 2016-05-09T07:37:04.043

3

Yup, cd - is the way to go but I worry about the lack of power you've got in your shell. popd and pushd are also good, and that j thing also looks good.

I'll throw my own utility into the ring... it's just something I've been building, customising and migrating with me for the last 10 years or so and it works just fine for what I want it to do.

Blog post for my directory management utility

If it works for you, then awesome, but if it doesn't then promise me you'll get something else :) You simply can't live with cd alone.

Derek Wyatt

Posted 2011-08-17T20:18:17.287

Reputation: 131

3

If you only have 2 directories, the easiest way is, as mentioned,

cd -

When I've had such things in the past, I've had a couple of tricks that I've done, that might be helpful.

  1. Put alias commands in the .cshrc file, something like this

    alias moo /etc/foo/bar/baz/moo  
    alias logs cd /var/lib/fubarred_app/blargh/logs
    
  2. Link shortcuts to the paths of interest from my home directory. This is a one time thing.

    cd ~
    ln -s /var/lib/fubarred_app/blargh/logs blargh
    ln -s /etc/foo/bar/baz/moo
    

This would allow changing a directory as easy as:

   cd ~/moo
   cd ~/blargh

PearsonArtPhoto

Posted 2011-08-17T20:18:17.287

Reputation: 476

2

Autojump is a bash/sh/zsh "cd" command that from your actions, see this video.

so this would move you to moo after it has learnt the last folders

j moo

oluies

Posted 2011-08-17T20:18:17.287

Reputation: 265

2

beam

I have my own tool, but will have a look at Dereks tool and j too.

Of course the first choice for jumping back and forth is

cd -

My tool is meant for directories with unique names on systems, where locate is available; it searches for a matching directory name, and jumps into the first match:

beam () 
{ 
    ldir=$(locate $1 | egrep "$1$" | head -n 1);
    if [[ -d $ldir ]]; then
        echo $ldir;
        cd $ldir;
    else
        echo "no directory "$ldir;
    fi
}

Dependencies:

  • locate
  • grep
  • head

Drawbacks:

  • newer directories than the last updatedb-run aren't found
  • if more than one directory matches, it depends on your luck, whether you're beamed to the one you had in mind.

Improvements/modifications:

  • instead of choosing the first directory, the code can be modified to display a list of choices, prompting the user to input 1,2,3,... to go to /bin, /usr/bin, /usr/local/bin ...

Usage:

  • put the function into you /etc/bash.bashrc or ~/.bashprofile, to use it in a convenient way.

Advantage of my solution:

  • not limited to 2 directories, like cd -
  • you don't have to visit the directories once, before using the beam.

user unknown

Posted 2011-08-17T20:18:17.287

Reputation: 1 623