The pushd/popd is such a simple concept which took me awhile to comprehend since people tend to teach it by defining these commands as commands that 'manipulate the directory stack' which in my opinion is very confusing.
I look at it in a different way:
pushd [folder_name] - will cd into [folder_name] and will document the destination which is [folder_name] in a dir-stack.
While the top directory in the stack will always be the current dir you are in.
popd - will cd into the directory record which is documented at the top of the stack and then remove the documentation (from the dir-stack).
dirs - Will print the dir-stack (which can be treated as the dir Db where the leftmost entry is the current directory (top of the stack).
So the 2 most popular use cases are:
Use case 1: Navigating using pushd and popd
root@mypc:/main/$ ls
dir1 dir2 dir3 dir4
root@mypc:/main/$ dirs # prints the current stack
/main
root@mypc:/main/$ pushd dir1 # Will cd to dir1 and document dir1 in dir stack, stack is now:
/main/dir1 /main
root@mypc:/main/dir1$ # I am now in /main/dir1
root@mypc:/main/dir1$ # Now let's go wild and document whatever I want
root@mypc:/main/dir1$ pushd ../dir2
root@mypc:/main/dir2$ # Woo I am in /main/dir2
root@mypc:/main/dir2$ pushd ../dir3
root@mypc:/main/dir3$ # Woo I am in /main/dir3
root@mypc:/main/dir3$ pushd ../dir4
root@mypc:/main/dir4$ # Woo I am in /main/dir4
root@mypc:/main/dir4$ dirs # Now dir stack is:
/main/dir4 /main/dir3 /main/dir2 /main/dir1 /main
I did the above since I would like to navigate back to those folders I documented! (using popd, instead of typing the relative or absolute path of each dir I want to go back into).
Note that if I manually cd, I will affect the top dir stack entry (which is always the current dir)
root@mypc:/main/dir4$ cd .. # Now dir stack is:
# (note that /main appear in the leftmost as well which is the top of the stack)
/main /main/dir3 /main/dir2 /main/dir1 /main
root@mypc:/main$
Let's navigate backwards now:
root@mypc:/main$ popd
root@mypc:/main$ # Still in /main since it was at the top of the dir stack
root@mypc:/main$ dirs # Stack is now:
/main/dir3 /main/dir2 /main/dir1 /main
root@mypc:/main$ popd
root@mypc:/main/dir3$ popd # Woo in dir3 now, about to navigate to dir2
root@mypc:/main/dir2$ popd # Woo in dir2, about to navigate to dir1
root@mypc:/main/dir1$ dirs # Stack is now:
/main
Again I can document whatever dir I want and then navigate manually to another dir then I will be able to easily return to the documented dir I inserted to the stack.
Use case 2: Navigating using numeric stack index
Lets say I pushed using pushd dir4 dir3 dir2 dir1, now running dir -v will show:
root@mypc:/main$ dirs -v
0 /main/dir1 (this is the current dir you are in always)
1 /main/dir2
2 /main/dir3
3 /main/dir4
Now you can do any Linux operation which involves directories using the stack index:
root@mypc:/main$ cp ~2/temp.txt ~3/new_temp.txt # this will run in the background, something like:
# cp /main/dir2/temp.txt /main/dir3/new_temp.txt
You can even delete a specific entry from the dir-stack:
root@mypc:/main$ popd ~4
Hope that using the words "documenting" or thinking about the dir-stack as some kind of Db simplifies the concept!
cd -
, aliases for shorteningcd ..
, etc.). – syntagma May 25 '13 at 18:26pushd
&popd
in scripts b/c they save me from having to remember where I was coming from, I can always justpopd
to get back from where I came. I usually dopopd >/dev/null 2>&1
to make it silent. I use cd- everyday in my shell. There are some other time saving tips in this article as well: http://www.thegeekstuff.com/2008/10/6-awesome-linux-cd-command-hacks-productivity-tip3-for-geeks/. – slm May 25 '13 at 18:31$CDPATH
. It's pretty powerful if you're looking for more efficient ways to get around via cd. – slm May 25 '13 at 18:38popd >/dev/null 2>&1
in a script rather thancd - >/dev/null 2>&1
? – Garrett Aug 22 '14 at 00:59cd -
only tracks the last directory, i imagine it would be possible to have issues if you call a function which also changes directory internally. in that case, the function would end up resetting - to your current directory, not the directory you want to pop back to. pushd/popd is the safest method. Note: i haven't tested my theory. – Binary Phile Nov 10 '14 at 20:52~/somedir/dir3
after the firstpopd
? – Ziyuan Feb 18 '16 at 22:03jumpd
to a dir on the stack by index or something ? – TheEagle Feb 24 '24 at 20:41