Using cd and cd - allows you to toggle between only your two most recently used directories. Your "directory working set" size is two.
Using pushd, you can keep an arbitrarily large number of directories in your working set.
I use pushd most of the time rather than cd. Once you've built up a stack of active directories with pushd directory_name, you can then jump between them all day with pushd ~#.
pushd dir1 pushd ../dir2 pushd /full/path/to/dir3 # There are now three directories in the stack. pushd ~3 pushd ~2 # The same three directories are still on the stack, # just in a different order. I use popd rarely, only when I want to remove a directory from the stack when I know I'm done using that directory.
Go to directory and remove it from the stack:
popd ~2 Stay in current directory and remove another directory from the stack:
popd +2 You end up with a working style that is similar to having multiple terminal windows or tabs open (one for each directory whenin which you're actively working), but all in one terminal. This saves screen real estate, and you can copy files, etc.plus, between directories you are currently working with (becausebecause the directory paths are all available in one shell)., you can do things like:
- copy files between directories you are currently working with
- view or edit files in another directory without going there
Examples:
cp ~2/myfile.txt ~4 less ~2/myfile.txt In tcsh (but not bash), you can even save your directory stack to a file and restore it later.
Save:
dirs -S ~/dirstack Restore:
dirs -L ~/dirstack Otherwise, just replace ~ in the bash examples with = for use in tcsh.
pushd =2 popd =4 popd +1