- [Instructor] Welcome to the next video of section three.
Fast command-line navigation using pushd and popd.
In the previous video, we've learned about
listing only directories, alternative methods.
In this video we'll store the directory paths
in a stack and switch between them
using the push and pop operations.
When dealing with multiple locations on a terminal
or shell prompt, a common practice
is to copy and paste the paths.
However, this is ineffective
when there is only command-line access without a GUI.
For example, if we are dealing with location
slash var www slash home, slinux and user slash source,
when we need to navigate to these locations one-by-one,
it's really difficult to type the path every time
when we need to switch between the paths.
Hence the command-line interface,
CLI-based navigation techniques
such as pushd and popd are used.
Let's see how to use them in this video.
Pushd and popd are used to switch between
multiple directories without copying and pasting
of directory paths.
Pushd and popd operate on a stack.
We know that a stack is a last-in first-out.
LIFO data structure.
We omit the use of the cd command
when using pushd and popd.
To push and change a directory to a path,
use the following command.
Now the stack contains var slash www tilde,
and the current directory has changed to var slash www.
Now again, push the next directory path as follows.
Now the stack contains slash user, slash source, slash var
slash www tilde.
And the current directory is user slash source.
You can similarly push as many directory paths as needed.
To view the stack contents, use the following command.
Now when you want to switch to any path on the list,
number each path from zero to n,
then use the path number for which we need to switch.
For example.
Now it will rotate the stack
and switch to the slash user slash share directory.
Pushd will always add paths to the stack.
To remove paths from the stack, use popd.
To remove a last pushed path
and change directory to a next directory,
use the following command.
To remove a specific path from the list,
use popd plus num.
Let's go through the essential
directory navigation practices.
Most frequently used directory switching, pushd and popd,
can be used when there are
more than three directory paths used.
But when you only use two locations,
there is an alternative and easier way.
That is cd minus.
The current path is var slash www.
Now switch back to slash var slash www.
You don't have to type again, but just execute.
Now we're going to switch to
slash user slash source as follows.
Awesome.
We've successfully learned about
fast command-line navigation using pushd and popd.
In the next video,
we'll learn how to count the number of lines,
words, and characters in a file.