From: ela on
cd - can go back one directory

is there any way to go backwards several directories? (Not asking cd ../../
etc)


From: Sidney Lambe on
On comp.os.linux.misc, ela <ela(a)yantai.org> wrote:

> cd - can go back one directory
>
> is there any way to go backwards several directories? (Not
> asking cd ../../ etc)
>
>

I use this script. It is called by the function "d"
immediately below. It must be sourced.


d ()
{
. /usr/local/bin/dirstack.sh
}

#!/bin/bash

# dirstack.sh

if ! [ -s $HOME/.dirstack ]
then touch $HOME/.dirstack
fi

head -n 20 $HOME/.dirstack > $HOME/.dirstacktmp
mv $HOME/.dirstacktmp $HOME/.dirstack

ds ()
{
awk -v pwd="$PWD" 'NR==1{print pwd}1' $HOME/.dirstack > $HOME/.dirstacktmp
mv $HOME/.dirstacktmp $HOME/.dirstack
}

set -- $(cat $HOME/.dirstack)

echo

echo "a) $1"
echo "b) $2"
echo "c) $3"
echo "d) $4"
echo "e) $5"
echo "f) $6"
echo "g) $7"
echo "h) $8"
echo "i) $9"
echo "j) ${10}"
echo "k) ${11}"
echo "l) ${12}"
echo "m) ${13}"
echo "n) ${14}"
echo "o) ${15}"
echo "p) ${16}"
echo "q) ${17}"
echo "r) ${18}"
echo "s) ${19}"
echo "t) ${20}"

echo

read -s -n1 bb

case "$bb" in

a) cd $1 && ls && ds ;;
b) cd $2 && ls && ds ;;
c) cd $3 && ls && ds ;;
d) cd $4 && ls && ds ;;
e) cd $5 && ls && ds ;;
f) cd $6 && ls && ds ;;
g) cd $7 && ls && ds ;;
h) cd $8 && ls && ds ;;
i) cd $9 && ls && ds ;;
j) cd ${10} && ls && ds ;;
k) cd ${11} && ls && ds ;;
l) cd ${12} && ls && ds ;;
m) cd ${13} && ls && ds ;;
n) cd ${14} && ls && ds ;;
o) cd ${15} && ls && ds ;;
p) cd ${16} && ls && ds ;;
q) cd ${17} && ls && ds ;;
r) cd ${18} && ls && ds ;;
s) cd ${19} && ls && ds ;;
t) cd ${20} && ls && ds ;;

esac



Output:


a) /usr/local/bin
b) /root
c) /root/story
d) /root/housekeeping
e) /root
f) /root/housekeeping
g) /root/housekeeping
h) /root/story
i) /usr/local/bin
j) /home/sid/usenet
k) /root/housekeeping
l) /root
m) /root
n) /root/sunrise
o) /home/sid/news
p) /root
q) /root/housekeeping
r) /root
s) /root/housekeeping
t) /root/housekeeping

You just hit a letter and you change to that
directory. No need to hit <enter>.

"a" is the last directory I visited, "b"
is the one before that, and so on.


You can use bash's dirstack and popd, but it's
much clumsier.

$ help popd

Sid


From: ela on
What do you mean by "sourced"? I find .dirstack is always of size 0 and so
although the script exits without error, it's always an empty list and it
does not wait for my choice input and exits.


From: Sidney Lambe on
On comp.os.linux.misc, ela <ela(a)yantai.org> wrote:

> What do you mean by "sourced"? I find .dirstack is always of
> size 0 and so although the script exits without error, it's
> always an empty list and it does not wait for my choice input
> and exits.

First of all, did you do:

$ type -a d

to make sure that "d" isn't in use already as the name of a
script/executable/function/alias?

Secondly, where did you put the function "d"? It should go in
your ~/.bashrc.

Sourcing makes a script run in your current shell rather than
the default subshell.

$ . scriptname

$ source scriptname

Those are two ways to do it.

If you don't source the script it won't work.


If that doesn't fix it, try putting "set -x" just below
"#!/bin/bash" to troubleshoot the script.

I use this script all the time. Couldn't live without it.


Sid


From: Chris F.A. Johnson on
On 2009-12-30, ela wrote:
> cd - can go back one directory
>
> is there any way to go backwards several directories? (Not asking cd ../../
> etc)

n=2
pushd "${DIRSTACK[@]:$n:1}"


--
Chris F.A. Johnson, author | <http://cfajohnson.com>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence