📜 ⬆️ ⬇️

A stack of directories. Again, a simple thing + add-ons

Few people know about bash commands like pushd and popd .
They allow you to work with a stack of directories.
pushd $ DIR allows you to go to $ DIR and add the current one to the stack. Each time the command is executed, the stack is replenished with the current directory.
popd allows you to go to the last directory in the stack and extract it from there. Thus, after each command execution, the stack is reduced by one directory, and the current directory becomes the directory at the top of the stack.

For example:
[user@laptop ~]
basg# cd /
[user@laptop /]
bash# pushd /var/log/
/var/log /
[user@laptop log]
bash# pushd /usr/share/
/usr/share /var/log /
[user@laptop share]
bash# popd
/var/log /
[user@laptop log]
bash# popd
/
[user@laptop /]
bash#



This thing is extremely convenient in scripts when there is a lack of a simple “cd -” (which, by the way, goes to the directory that was before the last execution of the cd command), as well as in everyday work on the console.

Additions
')
by CycaHuH
> And how can I see the stack?

echo $ {DIRSTACK [*]}

$ {DIRSTACK [0]} - current directory


from UUSER
`cd` go to the user's home directory.
`cd ~ user` go to another user's home directory.
`cat / proc / cpuinfo`
`watch !!` will substitute the previous command with the current one.
`! 123` will execute the command number from bash_history.
`sl [ctrl + t]` swap two characters.

Source: https://habr.com/ru/post/121225/


All Articles