-
Notifications
You must be signed in to change notification settings - Fork 0
pushd and popd Cheat Sheet
While working in the terminal, you may find yourself cycling between a few directories. However, it can get cumbersome to remember and navigate the relative paths ("where was that other file again? hm..." cd ../../
... tab tab tab).
If you're just jumping back and forth between two directories, you can easily do that with cd -
. I use this often enough that I defined an alias k
for it in my bash profile. A related alias, courtesy of one of my early computational science mentors, is d
to quickly zip up a directory tree. The last relevant shortcut listed here, fp
, is a bash function rather than an alias. It prints the full path location of a directory or file.
alias k='cd -' # change to previous directory
alias d='cd ..' # change to upper directory
fp() { # call "fp filename" to get full path location of file
readlink -f $1
}
For switching between 3+ directories, the tools pushd
and popd
can come in handy. Here's a cheat sheet of working with these shell commands.
command | purpose |
---|---|
dirs -v |
examine the stack [1] |
pushd mydir |
add directory to top (1 position) of stack and cd there [2] [3] |
pushd -n mydir |
add directory without going there |
dirs -c |
purge the stack |
popd |
remove 1 position (top) and cd there |
popd ~3 |
remove 3 position and cd there [4] |
popd +3 |
remove 3 position without going there |
cd ~3 |
cd to item 3 in stack without removing it |
[1]: The 0
position should always be your current location.
[2]: You can add relative paths with the pushd
command but this might break down if your directories are not all in the same level with the same layout.
[3]: It's less straightforward to push multiple directories at once, but it can be done. See this post on StackExchange.
[4]: This should supposedly work but it didn't for me.