Pushd popd global directory stack?

I do not know if there is a right way to do this. But, I always wanted to see if this is possible.

I know that pushd, popd and dirs are useful for many things, such as copying between directories that you recently visited.

But is there a way to keep the global stack? Therefore, if I push something (using pushd) in one terminal, it is reflected in another (perhaps only the terminals in this login session).

+5
source share
2 answers

You should be able to do this with a couple of shell functions and a temporary file.

Your temporary file will be called as " /home/me/.directory_stack" and will simply contain a list of directories:

/home/me
/etc
/var/log

'push_directory' . "pop_directory" . , ( ).

(: )

directory_stack=/home/me/.directory_stack
function push_dir() {
    echo $(pwd) >> $directory_stack
    cd $1
}
function pop_dir() {
    [ ! -s $directory_stack ] && return
    newdir=$(sed -n '$p' $directory_stack)
    sed -i -e '$d' $directory_stack
    cd $newdir
}

.bashrc, .

+4

, pushd popd. - () :

mypushd() { echo "$1" >> ~/.dir_stack ; cd "$1" }
mypopd() { dir=`tail -1 ~/.dir_stack` ; cd "$dir" ;
           foo=`wc -l ~/.dir_stack | egrep -o '[0-9]+'` ;
           ((foo=$foo-1)) ;
           mv ~/.dir_stack ~/.dir_stack_old ;
           head -n $foo ~/.dir_stack_old > ~/.dir_stack }

, , .

+3

All Articles