From: gyro on
On Nov 28, 10:44 am, yard-ape <yard-...(a)telus.net> wrote:
> gyro wrote:
> > Hi,
> > Does anyone know how to save and restore the directory stack in bash?
> > I would like to be able to access and manipulate past directories
> > between logins.
>
> Better to do it externally:http://www.xs4all.nl/~waterlan/
>
> -yard-ape


Thanks for all of the suggestions.
I'll try some of these ideas.
-g
From: Kaz Kylheku on
On 2009-11-28, gyro <gyromagnetic(a)gmail.com> wrote:
> Hi,
> Does anyone know how to save and restore the directory stack in bash?
> I would like to be able to access and manipulate past directories
> between logins.

You can show the stack with ``dirs''. This enables you to save it to a file.
With dirs -p you can print one entry per line, which is more convenient
(not to mention robust against spaces in path names).

I don't see a way to re-construct the stack directly from such a printed
representation, but, obviously, what you can do is push the contents
onto the stack one by one.

You have to do it in reverse order, though. The last line in the
dirs -p output is the bottom of the stack, rather than the first.

An easy way to do that is to push onto the stack in the wrong order,
then capture the output of ``dirs -p'' again, which reverses it for you.
Then clear the stack with dirs -c, and re-push the captured directories.

I.e. something like

# untested
# assumes directory stack has been saved in ~/.bash_dirstack

dirs -c
while read directory ; do
pushd "$directory"
done < ~/.bash_dirstack

dirs -p > ~/.bash_dirstack.tmp.$$

dirs -c
while read directory ; do
pushd "$directory"
done < ~/.bash_dirstack.tmp.$$

rm -f ~/.bash_dirstack.tmp.$$

From: mop2 on
On Sat, 28 Nov 2009 17:57:13 -0200, gyro <gyromagnetic(a)gmail.com> wrote:

> On Nov 28, 10:27 am, mop2 <inva...(a)mail.address> wrote:
>> On Sat, 28 Nov 2009 14:49:03 -0200, gyro <gyromagne...(a)gmail.com> wrote:
>> > Hi,
>> > Does anyone know how to save and restore the directory stack in bash?
>> > I would like to be able to access and manipulate past directories
>> > between logins.
>>
>> Which stack?
>> Well, you can create a file and routines for this purpose, or:
>>
>> If your request is for command line, possibly you can do this:
>>
>> grep search /etc/inputrc
>> "\e[A": history-search-backward
>> "\e[B": history-search-forward
>>
>> Add the above lines to correspondent file.
>>
>> set|grep HIS
>> HISTCONTROL=ignorespace:erasedups
>> HISTFILE=/root/.bash_history
>> HISTFILESIZE=500
>> HISTSIZE=500
>>
>> Add "erasedups" to your HISTCONTROL.
>>
>> Then, at prompt, you write cd and press key "up" and only
>> commands started by "cd" are showed, then select the
>> desired one.
>>
>> This solution works in command line only.
>
> Hi,
> The bash docs uses the term 'directory stack':
> http://www.faqs.org/docs/bashman/bashref_73.html
>
> -g
>

Hi gyro, thank you, this is a new buitin to me. :)

Seems to me now that you already know these buitins.

If you are already habituated with them you can try in your "logout script"
dirs >~/.dirs

And, to restore previous contition, in your "login script"
pushd `<~/.dirs`

If these history if very important to you, write a function do manage these
DIRs can be a convenient resource in your day to day, with the behavior
you want.
But the suggestion in my previous post is practical (I think) and with a suitable HISTSIZE
perhaps does the job.

Tell us more about the use intended, if you need more help or ideas.

From: gyro on
On Nov 28, 1:17 pm, Kaz Kylheku <kkylh...(a)gmail.com> wrote:
> On 2009-11-28, gyro <gyromagne...(a)gmail.com> wrote:
>
> > Hi,
> > Does anyone know how to save and restore the directory stack in bash?
> > I would like to be able to access and manipulate past directories
> > between logins.
>
> You can show the stack with ``dirs''. This enables you to save it to a file.
> With dirs -p you can print one entry per line, which is more convenient
> (not to mention robust against spaces in path names).
>
> I don't see a way to re-construct the stack directly from such a printed
> representation, but, obviously, what you can do is push the contents
> onto the stack one by one.
>
> You have to do it in reverse order, though. The last line in the
> dirs -p output is the bottom of the stack, rather than the first.
>
> An easy way to do that is to push onto the stack in the wrong order,
> then capture the output of ``dirs -p'' again, which reverses it for you.
> Then clear the stack with dirs -c, and re-push the captured directories.
>
> I.e. something like
>
>   # untested
>   # assumes directory stack has been saved in ~/.bash_dirstack
>
>   dirs -c
>   while read directory ; do
>      pushd "$directory"
>   done < ~/.bash_dirstack
>
>   dirs -p > ~/.bash_dirstack.tmp.$$
>
>   dirs -c
>   while read directory ; do
>      pushd "$directory"
>   done < ~/.bash_dirstack.tmp.$$
>
>   rm -f ~/.bash_dirstack.tmp.$$

Thanks, Kaz.
This looks interesting.

-g
From: Chris F.A. Johnson on
On 2009-11-28, gyro wrote:
> Hi,
> Does anyone know how to save and restore the directory stack in bash?
> I would like to be able to access and manipulate past directories
> between logins.

The stack is contained in the DIRSTACK array.

## save DIRSTACK
printf "%s\n" "${DIRSTACK[@]}" > ~/.dirstack

## restore DIRSTACK
DIRSTACK=( $( < ~/.dirstack ) )

## restore DIRSTACK (bash4)
mapfile -t DIRSTACK < ~/.dirstack

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====