One minute
push and pop on directories
Again a post about a well spread but unknown command pair on the terminal. It’s the pushd / popd
pair to control the directory stack of the shell. The pushd
command adds the directory given as argument to the directory stack and changes the working directory to that location. The popd
restores the current directory to the first entry of the directory stack and removes this entry. The dirs
command lists the content of the directory stack. But what does this mean? Consider the following example: %dirs ~ %pushd /usr/local/etc/ /usr/local/etc ~ %pwd /usr/local/etc %pushd /etc /etc /usr/local/etc ~ %pwd /etc %pushd /var/named/etc/ /var/named/etc /etc /usr/local/etc ~ %pwd /var/named/etc %popd /etc /usr/local/etc ~ %pwd /etc %popd /usr/local/etc ~ %pwd /usr/local/etc %popd ~ %pwd ~
This comes extreme handy for shell scripts if you have to visit a bunch of directories, do some work there and revisit them in reversed order again. For the daily use I can’t come up with any real world example, but this doesn’t mean there is none!