Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CLI: Creating and Navigating the Dirextory Stack

1 view
Skip to first unread message

Alan Connor

unread,
Jul 12, 2005, 6:06:42 AM7/12/05
to
CLI = Command Line Interface (bash in this case)

Creating and Navigating the Directory Stack
-------------------------------------------

All of you know about changing directories with 'cd'.
Here's a much more effective way of negotiating the
directory tree on your linux box.

Create and navigate a directory stack (dirs) with a function
that calls pushd, and displays it with dirs.

Run these:

help pushd
help dirs

To make this easy, put the following functions abd
alias in your ~/.bashrc:

This one runs pushd and then ls -shF on the directory
it takes you to. It supersedes your normal cd:

cd()
{
pushd "${@:-$HOME}" >/dev/null
ls -shF
}

This one makes using the directory stack (dirs) very easy. First
hit "d" (see below) to bring up the directory stack, then enter
"c" followed by a space, followed by the number of the dir you
want to return to. (Read on, this will become clear.)

c ()
{
eval cd +${1}
}


alias d='dirs -v'

----------------------------------

Now have bash reread your ~/.bashrc with:

$ source ~/.bashrc # or '. ~/.bashrc' Same thing

Do:

d

Nothing there? That's because you haven't put any directories
in the stack with the above function "cd" that calls the pushd
command, which does a conventional cd and adds the new directory
to the stack.

Try this, starting in your home directory, entering "d -c" first,
to clear the stack:

cd /etc
cd /bin
cd /
cd /usr

Now do

d

to display the directory stack. It will look like this:

0 /usr
1 /
2 /bin
3 /etc
4 ~


That's your directory stack now. Suppose that you want
to return to /bin without typing 'cd /bin and without
losing track of where you've been? Do:

c 2

It doesn't seem like much with the above short stack,
and short directory names and paths, but if you are 6
levels deep in a directory and you want to return to
another directory you were at a while ago that is 5
levels deep in a different directory, and you can't
quite remember where that file was, then it makes ALL
the difference in the world.

Take the small amount of time it takes to set up and
learn the above, and you will find yourself in much
more control of your navigations around your directory
tree. And you will begin to understand why the CLI
makes the GUI look like the child's playpen it is.


AC

--
If you are posting through google for any purpose
other than learning to use a real newsreader,
many of us will not answer your questions.
---- news.software.readers ----

Alan Connor

unread,
Jul 12, 2005, 10:14:17 AM7/12/05
to
On comp.os.linux.misc, in <SCMAe.21563$eM6....@newsread3.news.atl.earthlink.net>, "Alan Connor" wrote:
>
>
> CLI = Command Line Interface (bash in this case)
>
> Creating and Navigating the Directory Stack
> -------------------------------------------
>
> All of you know about changing directories with 'cd'.
> Here's a much more effective way of negotiating the
> directory tree on your linux box.
>
> Create and navigate a directory stack (dirs) with a function
> that calls pushd, and displays it with dirs.
>
> Run these:
>
> help pushd
> help dirs
>
> To make this easy, put the following functions abd
> alias in your ~/.bashrc:
>
> This one runs pushd and then ls -shF on the directory
> it takes you to. <snip>

This function below should be called "pd" (for pushd) , not "cd",
to avoid cluttering up the directory stack with "~" every time
you return $HOME, and allow you to do aliases like:

alias sd='cd /usr/share/doc/slrn'

for other directiories you commmonly visit, without cluttering
up the directory stack with THEM too.

To make this change you need to do:

unset cd

change the name of the function, and source your ~/.bashrc again.


>
> cd()
> {
> pushd "${@:-$HOME}" >/dev/null
> ls -shF
> }
>

pd ()

{
pushd "${@:-$HOME}" >/dev/null

/bin/ls -shF
}

Notice that the full path to ls is there, to get around any
aliases for ls, which are common in most stock distros.


> This one makes using the directory stack (dirs) very easy. First
> hit "d" (see below) to bring up the directory stack, then enter
> "c" followed by a space, followed by the number of the dir you
> want to return to. (Read on, this will become clear.)

Change this to pd too:

>
> c ()
> {
> eval cd +${1}
> }
>

c ()
{
eval pd +${1}
}


Be sure to source your ~/.bashrc after any changes to it.


>
> alias d='dirs -v'
>
> ----------------------------------
>
> Now have bash reread your ~/.bashrc with:
>
> $ source ~/.bashrc # or '. ~/.bashrc' Same thing
>
> Do:
>
> d
>
> Nothing there? That's because you haven't put any directories

> in the stack with the above function ["pd"] that calls the pushd


> command, which does a conventional cd and adds the new directory

> to the stack (as well as running /bin/ls -shF on the new directory)..


>
> Try this, starting in your home directory, entering "d -c" first,
> to clear the stack:


>
> cd /etc
> cd /bin
> cd /
> cd /usr
>
> Now do
>
> d
>

Now:

pd /etc
pd /bin
pd /
pd /usr

Now do

d


> to display the directory stack. It will look like this:
>
> 0 /usr 1 / 2 /bin 3 /etc 4 ~
>
>
> That's your directory stack now. Suppose that you want to

> return to /bin without typing 'cd /bin' [or pd /bin] and


> without losing track of where you've been? Do:
>
> c 2

Which does 'pushd +2 ; /bin/ls -shF' for you, or 'pd +2'

Lee Sau Dan

unread,
Jul 20, 2005, 6:08:55 AM7/20/05
to
>>>>> "Alan" == Alan Connor <zzz...@xxx.yyy> writes:

Alan> Create and navigate a directory stack (dirs) with a function
Alan> that calls pushd, and displays it with dirs.

Alan> Run these:

Alan> help pushd help dirs

I've been using these for years.

BTW, you didn't mention anything about the substitutions ~0, ~1, ~2, ...


Alan> Take the small amount of time it takes to set up and learn
Alan> the above, and you will find yourself in much more control
Alan> of your navigations around your directory tree. And you will
Alan> begin to understand why the CLI makes the GUI look like the
Alan> child's playpen it is.

Next session: teach the use of variables for accessing frequently used
directories/files.

e.g.

$ PJ1=~/project1
$ PJ2=~/project2
$ BK=~/backups
$ tar zcvf $BK/project1.tgz -C $PJ1 .
$ tar zcvf $BK/project2.tgz -C $PJ2 .

or something like that. Needless to say, you can "cd $PJ1" as easily.
Such variables let you essentially "fly" to the named destionations in
no time! Makes much more sense than those stupid drive letters!


GUIs? No way! Why click and click and click for 10 seconds just to
specify a file/directory?


--
Lee Sau Dan 李守敦 ~{@nJX6X~}

E-mail: dan...@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee

Alan Connor

unread,
Jul 20, 2005, 7:28:48 AM7/20/05
to
On comp.os.linux.misc, in

>> "Alan" == Alan Connor <zzz...@xxx.yyy> writes:
>
> Alan> Create and navigate a directory stack (dirs) with a

> Alan> function that calls pushd, and displays it with dirs.


>
> Alan> Run these:
>
> Alan> help pushd help dirs
>
> I've been using these for years.
>
> BTW, you didn't mention anything about the substitutions ~0,
> ~1, ~2, ...
>
>

What are those?

Just mentioning them isn't very helpful :-)

> Alan> Take the small amount of time it takes to set up and

> Alan> learn the above, and you will find yourself in much
> Alan> more control of your navigations around your directory
> Alan> tree. And you will begin to understand why the CLI
> Alan> makes the GUI look like the child's playpen it is.
>


> Next session: teach the use of variables for accessing
> frequently used directories/files.

>
> e.g.
>
> $ PJ1=~/project1
> $ PJ2=~/project2
> $ BK=~/backups
> $ tar zcvf $BK/project1.tgz -C $PJ1 .
> $ tar zcvf $BK/project2.tgz -C $PJ2 .
>

> or something like that. Needless to say, you can "cd $PJ1" as
> easily. Such variables let you essentially "fly" to the named
> destionations in no time! Makes much more sense than those
> stupid drive letters!
>

I don't like cdable_vars ( a shell option = shopt ).

Better to use aliases:

alias usds='cd /usr/share/doc/slrn'

or shorten it to "sd" for "slrn docs"

Much quicker, either way.

And all you need with the directory stack and command history at
your fingertips, too.

>
> GUIs? No way! Why click and click and click for 10 seconds
> just to specify a file/directory?
>

I'm with you. The TUI is where real control and freedom and speed
are found.

There was a guy who posted here last year who'd created a full
editor interface to the shell, which looked pretty cool. But
he'd chosen vim, which is a bloated, monolithic monstrosity
that's catching up with the dread emacs. If he'd chosen vanilla
vi, I'd have been all over it.


AC

Alan Connor

unread,
Jul 21, 2005, 5:22:21 AM7/21/05
to
On comp.os.linux.misc, in <QzqDe.6314$oZ....@newsread2.news.atl.earthlink.net>, "Alan Connor" wrote:
>
>
> On comp.os.linux.misc, in
><87vf35r...@informatik.uni-freiburg.de>, "Lee Sau Dan" wrote:
>
>>> "Alan" == Alan Connor <zzz...@xxx.yyy> writes:
>>
>> Alan> Create and navigate a directory stack (dirs) with a
>> Alan> function that calls pushd, and displays it with dirs.
>>
>> Alan> Run these:
>>
>> Alan> help pushd help dirs
>>
>> I've been using these for years.
>>
>> BTW, you didn't mention anything about the substitutions ~0,
>> ~1, ~2, ...
>>
>>
>
> What are those?
>
> Just mentioning them isn't very helpful :-)

It turns out that ~0 ~1, ~2, ... are shorthand for
the 0th, 1st, 2nd, ... directories in the directory stack.

My function "c" (see original post) works better. Why
do:

cd ~4

when you can just do:

c 4

??

But good to know anyway. Thanks for the clue.

<snip>

AC

Lee Sau Dan

unread,
Jul 23, 2005, 6:58:22 AM7/23/05
to
>>>>> "Alan" == Alan Connor <zzz...@xxx.invalid> writes:

>> BTW, you didn't mention anything about the substitutions ~0,
>> ~1, ~2, ...

Alan> What are those?

Why don't you try them out?


Alan> Just mentioning them isn't very helpful :-)

Not trying them out is even more useless.

Alan> I don't like cdable_vars ( a shell option = shopt ).

Alan> Better to use aliases:

Alan> alias usds='cd /usr/share/doc/slrn'

Alan> or shorten it to "sd" for "slrn docs"

Alan> Much quicker, either way.

But you can't do the following with such aliases:

cp $PROJ1/Makefile $PROJ2/

or things like that. Variables are more generic than cd-aliases.

Alan> I'm with you. The TUI is where real control and freedom and
Alan> speed are found.

I can't agree more! And TUI can be easy and productive to use, too.
Anyone remember PCTools R5.1 Deluxe? That was my favourite. (The UI
of later PCTools sucks!) Fortunately, I've found Emacs, which is even
more productive. :)


Alan> There was a guy who posted here last year who'd created a
Alan> full editor interface to the shell, which looked pretty
Alan> cool.

You mean "M-x terminal-mode" in Emacs?


Alan> But he'd chosen vim, which is a bloated, monolithic
Alan> monstrosity that's catching up with the dread emacs. If he'd
Alan> chosen vanilla vi, I'd have been all over it.

I use both Emacs and vi -- enjoying the best from both worlds. :D

Lee Sau Dan

unread,
Jul 23, 2005, 7:00:15 AM7/23/05
to
>>>>> "Alan" == Alan Connor <zzz...@xxx.invalid> writes:

Alan> It turns out that ~0 ~1, ~2, ... are shorthand for the 0th,
Alan> 1st, 2nd, ... directories in the directory stack.

Alan> My function "c" (see original post) works better. Why do:

Alan> cd ~4

Alan> when you can just do:

Alan> c 4

Alan> ??

But I can do "tar zxvf ~4/blahblah.tgz", "less ~3/Makefile", "vi
~2/a.c", etc. Your "c 4" can't be generalized to work for ANY command
(such as "tar").


Alan> But good to know anyway. Thanks for the clue.

You're welcome!

Alan Connor

unread,
Jul 23, 2005, 7:46:03 AM7/23/05
to
On comp.os.linux.misc, in
<871x5pi...@informatik.uni-freiburg.de>, "Lee Sau Dan" wrote:

<snip>

>
> Alan> I don't like cdable_vars ( a shell option = shopt ).
>
> Alan> Better to use aliases:
>
> Alan> alias usds='cd /usr/share/doc/slrn'
>
> Alan> or shorten it to "sd" for "slrn docs"
>
> Alan> Much quicker, either way.
>
> But you can't do the following with such aliases:
>
> cp $PROJ1/Makefile $PROJ2/
>
> or things like that. Variables are more generic than
> cd-aliases.
>

True.

>
>
> Alan> I'm with you. The TUI is where real control and

> Alan> freedom and speed are found.


>
> I can't agree more! And TUI can be easy and productive to
> use, too. Anyone remember PCTools R5.1 Deluxe? That was my
> favourite. (The UI of later PCTools sucks!)

Pctools isn't even mentioned in the Debian package list (stable).

> Fortunately, I've
> found Emacs, which is even more productive. :)

I hate bloated and monolithic apps like emacs.

>
>
> Alan> There was a guy who posted here last year who'd

> Alan> created a full editor interface to the shell, which
> Alan> looked pretty cool.


>
> You mean "M-x terminal-mode" in Emacs?

No. It was original code, and, unfortunately, graphical (gvim, I
think it is called). You logged in and found yourself in vim
and never left it except for apps openned in seperate windows.
Like I said, pretty interesting. Been thinking about writing
something like it in bash for vi.

>
>
> Alan> But he'd chosen vim, which is a bloated, monolithic
> Alan> monstrosity that's catching up with the dread

> Alan> emacs. If he'd chosen vanilla vi, I'd have been all
> Alan> over it.


>
> I use both Emacs and vi -- enjoying the best from both worlds.
> :D

They just aren't equivalent. Vi is an editor and emacs is an
integrated desktop environment.

I have one of those too, but I wrote it myself. And you don't
need to learn another programming language to use it. Just
plain old bash.

AC

Alan Connor

unread,
Jul 23, 2005, 7:46:04 AM7/23/05
to
On comp.os.linux.misc, in
<87wtnhh...@informatik.uni-freiburg.de>, "Lee Sau Dan" wrote:

<snip>

> "Alan" == Alan Connor <zzz...@xxx.invalid> writes:
>
> Alan> It turns out that ~0 ~1, ~2, ... are shorthand for the

> Alan> 0th, 1st, 2nd, ... directories in the directory stack.


>
> Alan> My function "c" (see original post) works better. Why
> do:
>
> Alan> cd ~4
>
> Alan> when you can just do:
>
> Alan> c 4
>
> Alan> ??
>
> But I can do "tar zxvf ~4/blahblah.tgz", "less ~3/Makefile",
> "vi ~2/a.c", etc. Your "c 4" can't be generalized to work for
> ANY command (such as "tar").
>

True. Like cdable_vars, they are more flexible. I was just
trying to cover the basics, but in the next update will include
both of your suggestions.

<snip>

AC

0 new messages