• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

Linux Pushing the Limits in Filesystem Work

dEEpEst

☣☣ In The Depths ☣☣
Staff member
Administrator
Super Moderator
Hacker
Specter
Crawler
Shadow
Joined
Mar 29, 2018
Messages
13,861
Solutions
4
Reputation
32
Reaction score
45,552
Points
1,813
Credits
55,350
‎7 Years of Service‎
 
56%
Pushing the Limits in Filesystem Work.

Let's imagine a workflow where we need to move between 4 directories located on completely different paths.

Using "cd -" is not an option, because the shell remembers only 1 previous directory. You can use CDPATH, but then you need to remember the names of the directories you are working with.

The pushd, popd, and dirs shell commands allow you to bypass this limitation. Let's say you are creating a local site and working with resources:

Bash:
/var/www/html
/etc/apache2
/etc/ssl/certs
~/Web/src

You can quickly switch between them using the shell function - "directory stack".

The directory stack is a list of directories that you have visited and decided to track in the current shell instance. Initially, it contains only the current path.

You can view the stack using the dirs command:

Bash:
$ dirs
/etc/ssl/certs /etc/apache2 /var/www/html ~/Web/src

The command output can be printed in the "-p" column:

Bash:
$ dirs -p
/etc/ssl/certs
/etc/apache2
...

You can also number it "-v". This will allow us to switch between directories more conveniently in the future:

Bash:
$ dirs -v
0 /etc/ssl/certs
1 /etc/apache2
...

You manage the stack by performing two operations: pushing and popping (by the way, there is such a dance style - it looks funny).

Pushing a directory adds it to the beginning of the list (the top of the stack) and jumps to it:

Bash:
$ pushd /etc/apache2
/etc/apache2 /var/www/html

To only add a directory to the stack and not jump to it, you need to use the "-n" flag.

Popping removes the top directory from the stack and returns us to the next one after it:

Bash:
$ popd
/var/www/html

If we want to completely clear the stack, then either run "dirs -c" or call popd until we see:

Bash:
$ popd
bash: popd: directory stack empty

The principle of working with the stack is that we add directories there, and then jump between them using arguments for pushd and popd, which we will consider below.
 

Practice of use: pushd, popd, dirs​


So, let's get back to the task we started working on here. In order to quickly switch between 4 directories, you first need to add them to the stack. The current one will be the leftmost (upper):

Bash:
$ pushd /var/www/html
/var/www/html ~/Web/src

$ pushd /etc/apache2
/etc/apache2 /var/www/html ~/Web/src

$ pushd /etc/ssl/certs
/etc/ssl/certs /etc/apache2 /var/www/html ~/Web/src

The pushd command without arguments swaps the two upper directories in the stack and moves to the new upper one. Let's jump back and forth between "/etc/ssl/certs" and "/etc/apache2" a few times:

Bash:
$ pwd
/etc/ssl/certs

$ pushd
/etc/apache2 /etc/ssl/certs /var/www/html ~/Web/src

$ pushd
/etc/ssl/certs /etc/apache2 /var/www/html ~/Web/src

What if we need to move to other directories in the stack, besides the top two? pushd and popd accept integer arguments for this task. Let's print the current stack in a column for clarity:

Bash:
$ dirs -v
0 /etc/ssl/certs
1 /etc/apache2
2 /var/www/html
3 ~/Web/src

The command "pushd +N" moves N directories from the top of the stack to the very bottom, and then moves to the new top one. If you want to get to "/var/www/html" and preserve the stack structure, use:

Bash:
$ pushd +2
/var/www/html ~/Web/src /etc/ssl/certs /etc/apache2

A negative argument (-N) pushes directories in the opposite direction. To get to the directory at the bottom of the stack, you can either push all but the last one, or just use "pushd -0":

Bash:
$ dirs
/etc/apache2 /etc/ssl/certs ~/Web/src /var/www/html

$ pushd -0
/var/www/html /etc/apache2 /etc/ssl/certs ~/Web/src

You can also remove directories from the stack using popd with an argument. The command "popd +N" removes the directory at position N from the stack. A negative argument (-N) means counting from the bottom. Counting starts from zero, so "popd +1" deletes the second directory from the top:

Bash:
$ dirs
/var/www/html /etc/apache2 /etc/ssl/certs ~/Web/src

$ popd +1
/var/www/html /etc/ssl/certs ~/Web/src

$ popd +2
/var/www/html /etc/ssl/certs

Be careful and attentive. If you use the cd command, the topmost element of the stack will be overwritten by the current active directory. We will learn how to solve this problem on Friday)
 
Back
Top