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
dir1 dir2 dir3 dir4
root@mypc:/main/$ dirs # prints the current stack
root@mypc:/main/$ pushd dir1 # Will cd to dir1 and document dir1 in dir stack, stack is now:
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
Let's navigate backwards now:
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/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:
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)
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!