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:
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:
The command output can be printed in the "-p" column:
You can also number it "-v". This will allow us to switch between directories more conveniently in the future:
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:
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:
If we want to completely clear the stack, then either run "dirs -c" or call popd until we see:
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.
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.