Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Advanced Unix Filesystem Navigation

News Recommended Links OFM Norton Change Directory
(NCD) clones
Bash history reuse and bang commands cdpath Pushd, popd and dirs
pushd/popd/dirs DirB, directory booksmarks Readline and inputrc Brace Expansion Command completion Directory favorites Bash Built-in Variables
Organizing shell aliases into knowledge database screen ln Dotfiles      
Care and Feeding of Functions in Shell Configuring and Customizing bash   bash Tips and Tricks  Unix shells history Humor Etc

Introduction

There are a surprising number of wrong ways to get from one point of Unix filesystem directory to another and the only one right way (to use orthodox file manager style file manger  ;-). None of commercial versions of Unix/Linux have OFM installed by default, so in large enterprise environment you need either to install it on all servers or imitate it with other tools.   The simplest way might be imitate it with screen which is installed by default on several Linux distributions and is available in vendor supported packages for all major Unixes.

If you do not have mc or similar OFM the first step in increasing you productivity would be to use aliases and capabilities of CDPATH. Collection of aliases should generally be treated similarly to favorites in browsers:  and it make sense to keep it in a separate file or even a directory with multiple files.  You can also imitate favorites using symlinks, but never do it from you home directory. Use a separate directory like /fav. In case you use you home directory operations like chown -R myself * might have a disastrous effect on the system.  

cd command by-and-large outlived its usefulness for accessing complex directory maze

While cd command is really outdated and belongs to previous century as a way to navigate complex maze of directories typical for modern enterprise Unixes/Linuxes, it usage can be enhanced to make it more tolerable and less "clicks wasting" instrument.  There are multiple way or make it shorter for accessing deeply nested directory.

Unlike its more modern counterparts such as NDD, cd command does not has no its own history. Still there is access to previous directly used: entering cd - returns you to the last directory we used; entering it a second time "toggles" us back again). This might be more or less convenient way to work between two directories but it does not scale to three directory or more directories though.  

Another useful shortcut is cd which is built in alias for cd ~. It returns you to your home directory ( "~" is a shortcut for your home directory as defined by $HOME like in  . ~/.profile).  You can manipulate this point of return by changing variable $HOME but this is somewhat of a stretch...

Designers of  later versions of Unix OS and developers of second generation shells like ksh93 understood that something is wrong here but never were able to figure out what actually is wrong. \

Using CDPATH capability

The variable CDPATH defines the search path for the directory containing directories. So it served much like "directories home". The dangers are in creating too complex  CDPATH.  Often a single directory works best. For example export CDPATH = /srv/www/public_html . Now, instead of typing cd /srv/www/public_html/CSS I can simply type: cd CSS

Attempts to enhance the functionality of cd command

Naive and inconsistent attempts to fix the problem can be seen in various Unix commands. Historically the first was the introduction of  pushd/popd/dirs  troika in C shell. The idea was brilliant like many innovations introduced by Bill Joe in C-shell (he beat crap out of the while AT&T shell development team, especially out , but its implementation is almost useless as the data structure used (stack) is not very conductive to the task and there is no obvious way to imitate favorites with this troika.  Another negative factor is that instead of extending cd command in pretty obvious way three new were introduced. Later in Korn shell a limited version on return to prev directory was implemented as cd -

The second command that was enhanced to simplify navigation in Unix was "cd with replacement":

cd [new] [old]

This is ksh only trick, but this is very nice trick if you get used to it.  This form of "cd" takes two arguments. The first argument is a string to insert in the previous cd [whatever] command, and the second argument is the string we want it to replace.

For example, if we'd last typed

cd /spam/DB/etc

Then we could jump over to  /spam/Logs/etc by entering

cd DB Logs

If directory is not given, the value of the HOME shell variable is used.

But the most successful (albeit very limited) attempt to simplify navigation in Unix was the introduction of the variable CDPATH.  If the shell variable CDPATH exists, it is used as a search path. If directory begins with a slash or dot, CDPATH is not used.

As we mentioned before in cd command if  the directory is `-', it is equivalent to $OLDPWD.

The return status is zero if the directory is successfully changed, non-zero otherwise.

Pathname Completion

The shells ksh and bash both provide another useful feature, which, among other things, can help us get across directories. These shells have the ability to "guess" the directory name you're typing after you've typed the first few characters. Here's an example.

cd /usr/loc

and stop there, before entering the text, and then type TAB in bash (or the escape key twice in ksh), the shell completes the pathname for us:

cd /usr/local/

and the cursor waits at the end of the line for us to add the next part of the path. After some practice, pathname and filename completion can save a lot of keystrokes!

What happens if there are two or more directories that match the part we've typed? For instance, if we enter

cd /usr/lo

and type the escape key twice, no completion is performed. This will happen whenever the text we've entered matches two or more possible directories in the given path.

In this case, we can type the sequence [esc][=] instead. The following text is then displayed:

$ cd /usr/lo
1) local/
2) lost+found/
$ cd /usr/lo

what's happened is that a list of the possible selections that match the text is displayed. The unfinished command is reprinted, and the cursor waits for us to add some additional characters so we can complete enough of the pathname for completion to work properly.

bash needs no equivalent for the [esc][=] sequence, since it will automatically generate a list of possible entries if more than one exists:

bash$ cd /home/j
john jrl jsiler
bash$ cd /home/j

For more information about pathname completion see Command completion

You can use screen to provide multiple points of view of the filesystem

The first successful implementation of the ability to have multiple points of view on the filesystem was  screen.  Often the administrator work concentrates in a handful of directories (/etc/, /var/log and couple of others). Screen permits to preserve the context of each directory by having multiple virtual screens, one for each key directory.  In other words instead of using cd command you can open another session and then switch between multiple sessions.

Usage of .inputrc to scroll cd history

One of things I most often find myself doing is searching my command history for previous cd commands. With the two line addition to the standard /etc/inputrc or to you private ~/.inputrc, you can type in the first few letters, say "cd  " (note trailing space) and press PageUp and PageDown keys and it will scroll through your cd commands history. In order to get this functionality you need to add  the following two commands:

"\e[5~": history-search-backward
"\e[6~": history-search-forward

See Readline and inputrc for more information.

Note: escape codes for PageUp and PageDown vary depending on your terminal type; check out this tip for a technique on how to find out what your terminal expects.

Using multiple windows with one window containing automatically update list of file in the other

The first really useful way to enhance Unix navigation was introduced by John Socha in his  famous Norton Commander functionality of which was later generalized in the concept of orthodox file managers(OFM).  Later it was enhanced by incorporating  functionality of Norton Change Directory (NCD). The latter has multiple Unix clones. See (Norton Change Directory (NCD).

Large part of OFM functionality can be emulated in screen or even in two regular terminal windows (the idea of automatically updatable list of files is especially easy to implement.

The most popular OFM for Unix is mc (Midnight Commander). MC has a very primitive terminal window emulator which might annoy seasoned administrators, but one can also use mc as ncd -- launch it, navigate to the necessary directory and then exit (F10).

Static set of directory favorites via pushd and use dirs to access them

One of the most productive ways of usage of pushd/popd/dirs troika is to use it as a proxy to the directories favorite list.  To do this we need better  understand the capabilities of each of the commands  in pushd/popd/dirs troika

dirs
dirs [+N | -N] [-clvp]
Display the list of currently remembered directories. Directories are added to the list with the pushd command; the popd command removes directories from the list.
+N
Displays the Nth directory (counting from the left of the list printed by dirs when invoked without options), starting with zero.
-N
Displays the Nth directory (counting from the right of the list printed by dirs when invoked without options), starting with zero.
-c
Clears the directory stack by deleting all of the elements.
-l
Produces a longer listing; the default listing format uses a tilde to denote the home directory.
-p
Causes dirs to print the directory stack with one entry per line.
-v
Causes dirs to print the directory stack with one entry per line, prefixing each entry with its index in the stack.
popd
popd [+N | -N] [-n]
When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory. The elements are numbered from 0 starting at the first directory listed with dirs; i.e., popd is equivalent to popd +0.
+N
Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
-N
Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
-n
Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
pushd
pushd [dir | +N | -N] [-n]
Save the current directory on the top of the directory stack and then cd to dir. With no arguments, pushd exchanges the top two directories.
+N
Brings the Nth directory (counting from the left of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
-N
Brings the Nth directory (counting from the right of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
-n
Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
dir
Makes the current working directory be the top of the stack, and then executes the equivalent of `cd dir'. cds to dir.
DIRSTACK
An array variable (see section Arrays) containing the current contents of the directory stack. Directories appear in the stack in the order they are displayed by the dirs built-in. Assigning to members of this array variable may be used to modify directories already in the stack, but the pushd and popd built-ins must be used to add and remove directories. Assignment to this variable will not change the current directory. If DIRSTACK is unset, it loses its special properties, even if it is subsequently reset.

dirs command is very useful if we try to use pushd/popd as a favorites list. If nothing else it provides you with the list of favorites: small but useful improvement over pitiful standard bash mode ;-).  To put directory into the stack you can use the ability to use pushd simply store directories using option -n.   Initial population of the list of favorites can be done in the .profile  from predefined list.  

After  populating the DIRSTACK you can use shortcuts to directories via cd ~1, cd ~2, ... notation.   For example if :
   dirs -v
       0  ~
       1  /var/log
       2  /usr/local

Then
      cd ~1  is equal to cd /var/log
and
      cd ~2  is equal to cd /usr/local

Notes

Automatic maintenance of directories favorites

The first successful implementation of the idea of reusable history of visited directories as a basis for advanced navigation was implemented in Xtree.  There are several good Xtree clones for Unix so one can try to use this concept. See for example UnixTree, and Ytree. As they say on the webpage "Don't leave $HOME without it!"

Dr. Nikolai Bezroukov


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Oct 14, 2018] autojump A cd command that learns - easily navigate directories from the command line

Apr 10, 2012 | github.com

autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line.

Directories must be visited first before they can be jumped to. USAGE

j is a convenience wrapper function around autojump. Any option that can be used with autojump can be used with j and vice versa.

For more options refer to help:

autojump --help
KNOWN ISSUES
autojump does not support directories that begin with -.
For bash users, autojump keeps track of directories by modifying $PROMPT_COMMAND. Do not overwrite $PROMPT_COMMAND:

export PROMPT_COMMAND="history -a"

Instead append to the end of the existing $PROMPT_COMMAND:

export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ;} history -a"
REPORTING BUGS

For any questions or issues please visit:

https://github.com/joelthelion/autojump/issues
AUTHORS

autojump was originally written by JoÃ"l Schaerer, and currently maintained by William Ting. More contributors can be found in AUTHORS. COPYRIGHT

Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html >. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

[Oct 14, 2018] Jump-Location - A Change Directory (CD) PowerShell Command that reads your mind by Scott Hanselman

Oct 14, 2018 | www.hanselman.com

September 18, '14

There's a lovely little utility called autojump for *nix consoles that makes the 'cd' command very smart. More that just auto-completion, it's a kind of "auto guessing." Hence, autojump. There is some beginning Windows support, but instead I turned to Tim Kellogg's open source PowerShell implementation " Jump-Location ."

What a joy.

j this and j that

First, I was like "jump-location?" I'm not going to type that. But then, of course, duh. Aliases.

Jump-Location is aliased to just j , which means I can now do awesome stuff like this:

c:\> j sc
c:\users\scott> j g
c:\users\AppData\Local\GitHub> j des
c:\users\scott\Desktop>

But there's more. It's actually watching how long you are in a directory and keeping stats. You can see the weighted stats with "jumpstat" and the "database" is just a text file in ~\jump-location.txt.

If "j d" isn't enough to get me into C:\GitHub\DisProject then I can do "j g d" and I'm there. It's amazing.

Installation is easy, and I hope it gets on PsGet soon for even easier installation. Just unzip, unblock, ensure that your PowerShell execution policy allows scripts, and run ./install.ps1.

NOTE : Don't run install from your desktop, or a temp folder. Put the Jump-Location folder somewhere where it will live, and it's going to add a line like this to your user profile ("C:\Users\YOU\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1") like this, so you'll want to install from a final path:

Import-Module 'C:\Users\Scott\Dropbox\utils\Jump-Location-0.5.1\Jump.Location.psd1'

I'm excited about this great little utility. Head over to https://github.com/tkellogg/Jump-Location and STAR it in GitHub, and give it a go! Tim, the author, is on Twitter at @kellogh . Other contributors include Sergey Vorobyev .

[Oct 14, 2018] Port of NCD (Norton Change Directory) for debian-ubuntu

Oct 14, 2018 | www.linuxquestions.org
Old 05-02-2017, 04:19 AM # 1
deepcore LQ Newbie
Registered: Dec 2005 Location: Denmark, Copenhagen Distribution: Ubuntu Ultimate v.6 Posts: 10
Rep: Reputation: 0
Port of NCD (Norton Change Directory) for debian/ubuntu

[ Log in to get rid of this advertisement] Does anyone know of a current port of ncd (Norton Change Directory) for Debian/Ubuntu systems? Preferably accessible through the package manager so it can easily be added to newly installed systems.

My best effords to find a similar utility has led me to:

https://github.com/KellyLSB/KCD
http://kcd.sourceforge.net/
https://waterlan.home.xs4all.nl/

... but as stated i would like to have it from packagemanager.

deepcore
View Public Profile
View LQ Blog
View Review Entries
View HCL Entries
Find More Posts by deepcore
Old 05-02-2017, 04:48 AM # 2
pan64 LQ Guru
Registered: Mar 2012 Location: Hungary Distribution: debian/ubuntu/suse ... Posts: 11,334
Rep: Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408 Reputation: 3408
I don't know what do you really need, but for example zsh has a built-in cd command which has a lot of features (although a bit different).
pan64
View Public Profile
View LQ Blog
View Review Entries
View HCL Entries
Find More Posts by pan64
Old 05-02-2017, 05:26 AM # 3
michaelk Moderator
Registered: Aug 2002 Posts: 17,578
Rep: Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312 Reputation: 2312
What about wcd? It should be available in the debian/Ubuntu repositories.

https://linux.die.net/man/1/wcd

[Jul 29, 2017] linux - Directory bookmarking for bash - Stack Overflow

Notable quotes:
"... May you wan't to change this alias to something which fits your needs ..."
Jul 29, 2017 | stackoverflow.com

getmizanur , asked Sep 10 '11 at 20:35

Is there any directory bookmarking utility for bash to allow move around faster on the command line?

UPDATE

Thanks guys for the feedback however I created my own simple shell script (feel free to modify/expand it)

function cdb() {
    USAGE="Usage: cdb [-c|-g|-d|-l] [bookmark]" ;
    if  [ ! -e ~/.cd_bookmarks ] ; then
        mkdir ~/.cd_bookmarks
    fi

    case $1 in
        # create bookmark
        -c) shift
            if [ ! -f ~/.cd_bookmarks/$1 ] ; then
                echo "cd `pwd`" > ~/.cd_bookmarks/"$1" ;
            else
                echo "Try again! Looks like there is already a bookmark '$1'"
            fi
            ;;
        # goto bookmark
        -g) shift
            if [ -f ~/.cd_bookmarks/$1 ] ; then 
                source ~/.cd_bookmarks/"$1"
            else
                echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
            fi
            ;;
        # delete bookmark
        -d) shift
            if [ -f ~/.cd_bookmarks/$1 ] ; then 
                rm ~/.cd_bookmarks/"$1" ;
            else
                echo "Oops, forgot to specify the bookmark" ;
            fi    
            ;;
        # list bookmarks
        -l) shift
            ls -l ~/.cd_bookmarks/ ;
            ;;
         *) echo "$USAGE" ;
            ;;
    esac
}

INSTALL

1./ create a file ~/.cdb and copy the above script into it.

2./ in your ~/.bashrc add the following

if [ -f ~/.cdb ]; then
    source ~/.cdb
fi

3./ restart your bash session

USAGE

1./ to create a bookmark

$cd my_project
$cdb -c project1

2./ to goto a bookmark

$cdb -g project1

3./ to list bookmarks

$cdb -l

4./ to delete a bookmark

$cdb -d project1

5./ where are all my bookmarks stored?

$cd ~/.cd_bookmarks

Fredrik Pihl , answered Sep 10 '11 at 20:47

Also, have a look at CDPATH

A colon-separated list of search paths available to the cd command, similar in function to the $PATH variable for binaries. The $CDPATH variable may be set in the local ~/.bashrc file.

ash$ cd bash-doc
bash: cd: bash-doc: No such file or directory

bash$ CDPATH=/usr/share/doc
bash$ cd bash-doc
/usr/share/doc/bash-doc

bash$ echo $PWD
/usr/share/doc/bash-doc

and

cd -

It's the command-line equivalent of the back button (takes you to the previous directory you were in).

ajreal , answered Sep 10 '11 at 20:41

In bash script/command,
you can use pushd and popd

pushd

Save and then change the current directory. With no arguments, pushd exchanges the top two directories.

Usage

cd /abc
pushd /xxx    <-- save /abc to environment variables and cd to /xxx
pushd /zzz
pushd +1      <-- cd /xxx

popd is to remove the variable (reverse manner)

fgm , answered Sep 11 '11 at 8:28

bookmarks.sh provides a bookmark management system for the Bash version 4.0+. It can also use a Midnight Commander hotlist.

Dmitry Frank , answered Jun 16 '15 at 10:22

Thanks for sharing your solution, and I'd like to share mine as well, which I find more useful than anything else I've came across before.

The engine is a great, universal tool: command-line fuzzy finder by Junegunn.

It primarily allows you to "fuzzy-find" files in a number of ways, but it also allows to feed arbitrary text data to it and filter this data. So, the shortcuts idea is simple: all we need is to maintain a file with paths (which are shortcuts), and fuzzy-filter this file. Here's how it looks: we type cdg command (from "cd global", if you like), get a list of our bookmarks, pick the needed one in just a few keystrokes, and press Enter. Working directory is changed to the picked item:

It is extremely fast and convenient: usually I just type 3-4 letters of the needed item, and all others are already filtered out. Additionally, of course we can move through list with arrow keys or with vim-like keybindings Ctrl+j / Ctrl+k .

Article with details: Fuzzy shortcuts for your shell .

It is possible to use it for GUI applications as well (via xterm): I use that for my GUI file manager Double Commander . I have plans to write an article about this use case, too.

return42 , answered Feb 6 '15 at 11:56

Inspired by the question and answers here, I added the lines below to my ~/.bashrc file.

With this you have a favdir command (function) to manage your favorites and a autocompletion function to select an item from these favorites.

# ---------
# Favorites
# ---------

__favdirs_storage=~/.favdirs
__favdirs=( "$HOME" )

containsElement () {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
}

function favdirs() {

    local cur
    local IFS
    local GLOBIGNORE

    case $1 in
        list)
            echo "favorite folders ..."
            printf -- ' - %s\n' "${__favdirs[@]}"
            ;;
        load)
            if [[ ! -e $__favdirs_storage ]] ; then
                favdirs save
            fi
            # mapfile requires bash 4 / my OS-X bash vers. is 3.2.53 (from 2007 !!?!).
            # mapfile -t __favdirs < $__favdirs_storage
            IFS=$'\r\n' GLOBIGNORE='*' __favdirs=($(< $__favdirs_storage))
            ;;
        save)
            printf -- '%s\n' "${__favdirs[@]}" > $__favdirs_storage
            ;;
        add)
            cur=${2-$(pwd)}
            favdirs load
            if containsElement "$cur" "${__favdirs[@]}" ; then
                echo "'$cur' allready exists in favorites"
            else
                __favdirs+=( "$cur" )
                favdirs save
                echo "'$cur' added to favorites"
            fi
            ;;
        del)
            cur=${2-$(pwd)}
            favdirs load
            local i=0
            for fav in ${__favdirs[@]}; do
                if [ "$fav" = "$cur" ]; then
                    echo "delete '$cur' from favorites"
                    unset __favdirs[$i]
                    favdirs save
                    break
                fi
                let i++
            done
            ;;
        *)
            echo "Manage favorite folders."
            echo ""
            echo "usage: favdirs [ list | load | save | add | del ]"
            echo ""
            echo "  list : list favorite folders"
            echo "  load : load favorite folders from $__favdirs_storage"
            echo "  save : save favorite directories to $__favdirs_storage"
            echo "  add  : add directory to favorites [default pwd $(pwd)]."
            echo "  del  : delete directory from favorites [default pwd $(pwd)]."
    esac
} && favdirs load

function __favdirs_compl_command() {
    COMPREPLY=( $( compgen -W "list load save add del" -- ${COMP_WORDS[COMP_CWORD]}))
} && complete -o default -F __favdirs_compl_command favdirs

function __favdirs_compl() {
    local IFS=$'\n'
    COMPREPLY=( $( compgen -W "${__favdirs[*]}" -- ${COMP_WORDS[COMP_CWORD]}))
}

alias _cd='cd'
complete -F __favdirs_compl _cd

Within the last two lines, an alias to change the current directory (with autocompletion) is created. With this alias ( _cd ) you are able to change to one of your favorite directories. May you wan't to change this alias to something which fits your needs .

With the function favdirs you can manage your favorites (see usage).

$ favdirs 
Manage favorite folders.

usage: favdirs [ list | load | save | add | del ]

  list : list favorite folders
  load : load favorite folders from ~/.favdirs
  save : save favorite directories to ~/.favdirs
  add  : add directory to favorites [default pwd /tmp ].
  del  : delete directory from favorites [default pwd /tmp ].

Zied , answered Mar 12 '14 at 9:53

Yes there is DirB: Directory Bookmarks for Bash well explained in this Linux Journal article

An example from the article:

% cd ~/Desktop
% s d       # save(bookmark) ~/Desktop as d
% cd /tmp   # go somewhere
% pwd
/tmp
% g d       # go to the desktop
% pwd
/home/Desktop

Al Conrad , answered Sep 4 '15 at 16:10

@getmizanur I used your cdb script. I enhanced it slightly by adding bookmarks tab completion. Here's my version of your cdb script.
_cdb()
{
    local _script_commands=$(ls -1 ~/.cd_bookmarks/)
    local cur=${COMP_WORDS[COMP_CWORD]}

    COMPREPLY=( $(compgen -W "${_script_commands}" -- $cur) )
}
complete -F _cdb cdb


function cdb() {

    local USAGE="Usage: cdb [-h|-c|-d|-g|-l|-s] [bookmark]\n
    \t[-h or no args] - prints usage help\n
    \t[-c bookmark] - create bookmark\n
    \t[-d bookmark] - delete bookmark\n
    \t[-g bookmark] - goto bookmark\n
    \t[-l] - list bookmarks\n
    \t[-s bookmark] - show bookmark location\n
    \t[bookmark] - same as [-g bookmark]\n
    Press tab for bookmark completion.\n"        

    if  [ ! -e ~/.cd_bookmarks ] ; then
        mkdir ~/.cd_bookmarks
    fi

    case $1 in
        # create bookmark
        -c) shift
            if [ ! -f ~/.cd_bookmarks/$1 ] ; then
                echo "cd `pwd`" > ~/.cd_bookmarks/"$1"
                complete -F _cdb cdb
            else
                echo "Try again! Looks like there is already a bookmark '$1'"
            fi
            ;;
        # goto bookmark
        -g) shift
            if [ -f ~/.cd_bookmarks/$1 ] ; then
                source ~/.cd_bookmarks/"$1"
            else
                echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
            fi
            ;;
        # show bookmark
        -s) shift
            if [ -f ~/.cd_bookmarks/$1 ] ; then
                cat ~/.cd_bookmarks/"$1"
            else
                echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
            fi
            ;;
        # delete bookmark
        -d) shift
            if [ -f ~/.cd_bookmarks/$1 ] ; then
                rm ~/.cd_bookmarks/"$1" ;
            else
                echo "Oops, forgot to specify the bookmark" ;
            fi
            ;;
        # list bookmarks
        -l) shift
            ls -1 ~/.cd_bookmarks/ ;
            ;;
        -h) echo -e $USAGE ;
            ;;
        # goto bookmark by default
        *)
            if [ -z "$1" ] ; then
                echo -e $USAGE
            elif [ -f ~/.cd_bookmarks/$1 ] ; then
                source ~/.cd_bookmarks/"$1"
            else
                echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
            fi
            ;;
    esac
}

tobimensch , answered Jun 5 '16 at 21:31

Yes, one that I have written, that is called anc.

https://github.com/tobimensch/anc

Anc stands for anchor, but anc's anchors are really just bookmarks.

It's designed for ease of use and there're multiple ways of navigating, either by giving a text pattern, using numbers, interactively, by going back, or using [TAB] completion.

I'm actively working on it and open to input on how to make it better.

Allow me to paste the examples from anc's github page here:

# make the current directory the default anchor:
$ anc s

# go to /etc, then /, then /usr/local and then back to the default anchor:
$ cd /etc; cd ..; cd usr/local; anc

# go back to /usr/local :
$ anc b

# add another anchor:
$ anc a $HOME/test

# view the list of anchors (the default one has the asterisk):
$ anc l
(0) /path/to/first/anchor *
(1) /home/usr/test

# jump to the anchor we just added:
# by using its anchor number
$ anc 1
# or by jumping to the last anchor in the list
$ anc -1

# add multiple anchors:
$ anc a $HOME/projects/first $HOME/projects/second $HOME/documents/first

# use text matching to jump to $HOME/projects/first
$ anc pro fir

# use text matching to jump to $HOME/documents/first
$ anc doc fir

# add anchor and jump to it using an absolute path
$ anc /etc
# is the same as
$ anc a /etc; anc -1

# add anchor and jump to it using a relative path
$ anc ./X11 #note that "./" is required for relative paths
# is the same as
$ anc a X11; anc -1

# using wildcards you can add many anchors at once
$ anc a $HOME/projects/*

# use shell completion to see a list of matching anchors
# and select the one you want to jump to directly
$ anc pro[TAB]

Cảnh Toàn Nguyễn , answered Feb 20 at 5:41

Bashmarks is an amazingly simple and intuitive utility. In short, after installation, the usage is:
s <bookmark_name> - Saves the current directory as "bookmark_name"
g <bookmark_name> - Goes (cd) to the directory associated with "bookmark_name"
p <bookmark_name> - Prints the directory associated with "bookmark_name"
d <bookmark_name> - Deletes the bookmark
l                 - Lists all available bookmarks

,

For short term shortcuts, I have a the following in my respective init script (Sorry. I can't find the source right now and didn't bother then):
function b() {
    alias $1="cd `pwd -P`"
}

Usage:

In any directory that you want to bookmark type

b THEDIR # <THEDIR> being the name of your 'bookmark'

It will create an alias to cd (back) to here.

To return to a 'bookmarked' dir type

THEDIR

It will run the stored alias and cd back there.

Caution: Use only if you understand that this might override existing shell aliases and what that means.

[Jul 24, 2017] Bash history handling with multiple terminals

Add to your Prompt command history -a to preserve history from multiple terminals. This is a very neat trick !!!
get=

Bash history handling with multiple terminals

The bash session that is saved is the one for the terminal that is closed the latest. If you want to save the commands for every session, you could use the trick explained here.

export PROMPT_COMMAND='history -a'

To quote the manpage: "If set, the value is executed as a command prior to issuing each primary prompt."

So every time my command has finished, it appends the unwritten history item to ~/.bash

ATTENTION: If you use multiple shell sessions and do not use this trick, you need to write the history manually to preserver it using the command history -a

See also:

[Feb 04, 2017] Use CDPATH to access frequent directories in bash - Mac OS X Hints

Feb 04, 2017 | hints.macworld.com
The variable CDPATH defines the search path for the directory containing directories. So it served much like "directories home". The dangers are in creating too complex CDPATH. Often a single directory works best. For example export CDPATH = /srv/www/public_html . Now, instead of typing cd /srv/www/public_html/CSS I can simply type: cd CSS
Use CDPATH to access frequent directories in bash
Mar 21, '05 10:01:00AM • Contributed by: jonbauman

I often find myself wanting to cd to the various directories beneath my home directory (i.e. ~/Library, ~/Music, etc.), but being lazy, I find it painful to have to type the ~/ if I'm not in my home directory already. Enter CDPATH , as desribed in man bash ):

The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".:~:/usr".
Personally, I use the following command (either on the command line for use in just that session, or in .bash_profile for permanent use):
CDPATH=".:~:~/Library"

This way, no matter where I am in the directory tree, I can just cd dirname , and it will take me to the directory that is a subdirectory of any of the ones in the list. For example:
$ cd
$ cd Documents 
/Users/baumanj/Documents
$ cd Pictures
/Users/username/Pictures
$ cd Preferences
/Users/username/Library/Preferences
etc...
[ robg adds: No, this isn't some deeply buried treasure of OS X, but I'd never heard of the CDPATH variable, so I'm assuming it will be of interest to some other readers as well.]

cdable_vars is also nice
Authored by: clh on Mar 21, '05 08:16:26PM

Check out the bash command shopt -s cdable_vars

From the man bash page:

cdable_vars

If set, an argument to the cd builtin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.

With this set, if I give the following bash command:

export d="/Users/chap/Desktop"

I can then simply type

cd d

to change to my Desktop directory.

I put the shopt command and the various export commands in my .bashrc file.

[Dec 15, 2014] CLI Magic Bash complete By Shashank Sharma

May 08, 2006 | Linux.com

The auto complete feature of the Bourne Again SHell makes bash one of the most loved and newbie-friendly Linux shells. Just by pressing the Tab key you can complete commands and filenames. Press the Tab key twice and all files in the directory get displayed. But you can do more with autocomplete -- such as associating file types with applications, and automatically designating whether you're looking for directories, text, or MP3 files. With simple commands such as complete and the use of Escape sequences, you can save time and have fun on the command line.

You can use the dollar sign ($), tilde (~), and at (@) characters along with the Tab key to get quick results in autocomplete.

For instance, if you want to switch to the testing subdirectory of your home directory, you can either type cd /ho[Tab]/tes[Tab] to get there, or use the tilde -- cd ~tes[Tab]. If the partial text -- that is, the portion before you press Tab -- begins with a dollar sign, bash looks for a matching environment variable. The tilde tells bash to look for a matching user name, and the at-sign tells it to look for a matching hostname.

Escaping is good

The Tab key can complete the names of commands, files, directories, users, and hosts. Sometimes, it is overkill to use the Tab key. If you know that you are looking for a file, or only user names, then use the Escape key instead for completion, as it limits bash's completion field.

You can use several Escape key combinations to tell bash what you are looking for. Invoke Escape key combinations by pressing a key while keeping the Escape key pressed. When looking for a file, you can use the Esc-/ (press / along with Escape) key combination. This will attempt filename completion only. If you have one file and one directory beginning with the letter 'i,' you will have to press the Tab key twice to see all the files:

$ less i <tab><tab>
ideas im articles/

When you type less i and press '/' while keeping the Escape key pressed, bash completes the filename to 'ideas.'

While Control key combinations work no matter how long you keep the Ctrl key pressed before pressing the second key, this is not the case with Escape key sequences. The Esc-/ sequence will print out a slash if you delay in pressing the / key after you press the Escape key.

You can also use Escape along with the previously discussed $, ~, and @ keys. Esc-$, for example, completes only variable names. You can use Esc-! when you wish to complete command names. Of course you need to press the Shift key in order to use any of the "upper order" characters.

Even smarter completion

By default, Tab completion is quite dim-witted. This is because when you have already typed cd down before pressing Tab, you'd expect bash to complete only directory names. But bash goes ahead and displays all possible files and directories that begin with 'down.'

You can, however, convert bash into a brilliant command-reading whiz. As root, edit the /etc/bash.bashrc file. Scroll down to the end of the file till you see the section:

# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion
#fi

Uncomment this section and voilà, you have given bash powers far beyond your imagination! Not only is bash now smart enough to know when to complete only directory names, it can also complete man pages and even some command arguments.

Don't despair if you don't have root previleges. Just edit the last section of your ~/.bashrc file.

Associating application with file types

The complete command in bash lets you associate file types with certain applications. If after associating a file type to an application you were to write the name of the application and press Tab, only files with associated file types would be displayed.

complete -G "*.txt" gedit would associate .txt files with gedit. The downfall of using complete is that it overwrites bash's regular completion. That is, if you have two files named invoice.txt and ideas.txt, gedit [Tab][Tab] displays both the files, but gedit inv[Tab], which should complete to invoice.txt, no longer works.

complete associations last only for the current bash session. If you exit and open a console, gedit will no longer be associated with .txt files. You need to associate file types to applications each time you start a new console session.

For permanent associations, you need to add the command to one of the bash startup scripts, such as ~/.bashrc. Then, whenever you are at the console, gedit will be associated with .txt files.

Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.

[Mar 30, 2009] Bash Navigation Toolkit

A prototype for directory favorites and navigation using bash by Richard Landon. In the current version is pretty much useless -- not batter then pushd/opod/dirs with static list of directories. The key idea is not new: using a PATH-like variable for string user-defined set of directory favorites which then can be addresses by their relative position in the list (1, 2, 3, etc). For example jump 3 will get to the third directory. This can be emulated by cd ~3 using pushd/popd/dirs troika.
freshmeat.net

The Bash Navigate Toolkit provides a set of functionality that allows users of the Bash (or Korn) shell to create and use sets of directory "favorites." These functions allow simple navigation within the file system, as well as deployment of automated scripts to iterate directories and perform various activities.

README
20-March-2008: Richard Landon -- A prototype for directory favorites and navigation using bash
               This prototype is provided AS-IN, with no warranty either expressed or implied.
 
ALL OPINIONS EXPRESSED HERE ARE SOLELY THOSE OF THE AUTHOR (if you agree with them).
OTHERWISE, THEY ARE OPINIONS OF SOMEONE ELSE

Design:
   Various attempts to define a mechanism or tools for assisting with directory navigation
   have not been satisfactory in the opinion of the author.
   
   Various examples are cite in the URL http://www.softpanorama.org/Scripting/Shellorama/advanced_navigation.shtml

   Developed a solution more suitable to the authors needs for navigation and manipulation of a class of directory
   favorites. 

   The basic idea behind this solution is to represent the history of directory navigation (favorites) using
   an environmental variable that follows the design of standard PATH variables common to Unix systems.
   
   The format of PATH related variable (such as path) is as follows: ::...:
  
   Various mechanisms (in the form of bash functions) were developed to assit the author with routine navigation
   commands. These include recording a directory favorite, and then navigating to that favorite with a simple 
   command or set of commands.  

Files:
      .navigate --  The navigation command set implementation
      .navigaterc -- A persistent record of navigation favorites (kept in the users home directory)
  

Assumptions:
   1) Developed under Cygwin platform.
   2) GNU version of cut is available

Commands:
 
   showhomes 
    List the current value of the SET_HOME environment variable

   showhome [position]  
    Show the value of the [index] component in the navigation structure
    Default [index] is first (1) component

  filterhome [path]
     Determine if the structure contains the path; returns 0 under this case
     [path] is required. Used internally to filter additions to the structure
     (see sethome for more details). Probably not otherwise useful.
 
  sethome [path]
    Add the given [path] or current path (`pwd`) to the top of the navigation structure.
    [path] will be added only if it does already exist within the navigation structure.
    If specified path does not exist, it is ignored

  resethome 
    Completely remove the current navigation structure from the environment.

  unsethome [position] 
    Remove the element at the indicated [position] (defaults to first, 1) within
    the navigation structure. 

  jumphome [position]
    Navigate (cd, change directory) to the indication position (defaults to first, 1) within
    the navigation structure. If position is greater than the largest element, then the 
    command will navigate to the last location in the structure. Command ouputs the absolute
    path to the location of the request position (ie, the target directory)
 
  listhomes 
    Displays in a readable format an enumerated list of the elements within the navigation structure.
    Each element has the form d:)path, where d is a digit (index) and path is a fully-qualified
    path element.

  persisthome
    Save the current navigation structure on disk. Output is written to the .navigaterc  file in
    the user home directory. The persistent navigation structure can be loaded later later using the
    loadhome command.
 
   loadhome
     Load a persistent navigation structure from disk into the environment. A persistent navigation
     structure can be stored to disk using the persisthome command. The environment is loaded
     using the contents of the .navigaterc file from the user home directory.

   purgehome
     Remove the currently persisted navigation structure from disk. This 

Standard alias
 
   The following standard set of aliases are provided for convenience 
   show  => showhome
   push  => sethome
   pop   => unsethome
   jump  => jumphome
   homes => listhomes
   load  => loadphome
   purge => purgehome
   reset => resethome

Examples:
   Here are some basic examples that use the standard aliases to demostrate the usefullness of the commands
   
   Some actual data in the listing has been converted to protect privacy of author

(1) List current favorities and display environmental variable
mylocalhost: [~] {52} $ homes
mylocalhost: [~] {53} $ showhome

(2) Add a directory to the favorites
mylocalhost: [~] {54} $ push /usr/local/bin
mylocalhost: [~] {55} $ ls `jump 1`
bashdb-3.1-0.09/  bn

(3) Load persistent favorites from disk
mylocalhost: [~] {56} $ cat ~/.navigaterc
/cygdrive/c/apps/java7/closures-2008-03-05:/cygdrive/c/apps/java7:/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts:/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts
mylocalhost: [~] {57} $ load
mylocalhost: [~] {58} $ homes
1:)/cygdrive/c/apps/java7/closures-2008-03-05
2:)/cygdrive/c/apps/java7
3:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts
4:)/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts

(4) Navigate around favorites
mylocalhost: [~] {60} $ jump 3
/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts
mylocalhost: [buildScripts] {61} $ 

(5) Clear the current set of favorites
mylocalhost: [~] {60} $ reset
mylocalhost: [buildScripts] {66} $ homes
mylocalhost: [buildScripts] {67} $ showhome

(6) Show integration with standard Unix pipes and filters
mylocalhost: [buildScripts] {68} $ push $(which tar| xargs dirname) 
mylocalhost: [buildScripts] {69} $ homes
1:)/usr/bin
mylocalhost: [buildScripts] {70} $ jump 1
/usr/bin
mylocalhost: [bin] {71} $ pwd
/usr/bin
mylocalhost: [bin] {72} $ ls tar
tar*

(7) Display more favorites
mylocalhost: [bin] {73} $ cd
mylocalhost: [~] {74} $ homes
1:)/usr/bin

(8) Load persistent favorites (note: overwrites current list of favorites)
mylocalhost: [~] {75} $ cat .navigaterc
/cygdrive/c/apps/java7/closures-2008-03-05:/cygdrive/c/apps/java7:/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts:/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts
mylocalhost: [~] {76} $ load
mylocalhost: [~] {77} $ homes
1:)/cygdrive/c/apps/java7/closures-2008-03-05
2:)/cygdrive/c/apps/java7
3:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts
4:)/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts

(9) Add more favorites
mylocalhost: [buildScripts] {80} $ push $(which tar| xargs dirname)
mylocalhost: [buildScripts] {81} $ homes
1:)/usr/bin
2:)/cygdrive/c/apps/java7/closures-2008-03-05
3:)/cygdrive/c/apps/java7
4:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts
5:)/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts

mylocalhost: [buildScripts] {83} $ ls -l `jump 1`/tar
-rwxrwxrwx+ 1 RIRL mkgroup-l-d 295K Feb 28 19:54 /usr/bin/tar*

(10) Save updated favorites
mylocalhost: [buildScripts] {85} $ persist
mylocalhost: [buildScripts] {86} $ homes
1:)/usr/bin
2:)/cygdrive/c/apps/java7/closures-2008-03-05
3:)/cygdrive/c/apps/java7
4:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts
5:)/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts

mylocalhost: [~] {90} $ homes
1:)/usr/bin
2:)/cygdrive/c/apps/java7/closures-2008-03-05
3:)/cygdrive/c/apps/java7
4:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts
5:)/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts
6:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts

(11) Remove a favorite 
mylocalhost: [~] {109} $ pop 2
mylocalhost: [~] {110} $ homes
1:)/usr/bin
2:)/cygdrive/c/progra~1/someapp/build/rel1.0/buildScripts
3:)/cygdrive/c/apps/java7/closures-2008-03-05
4:)/cygdrive/c/apps/java7
5:)/cygdrive/c/progra~1/someapp/build/rel2.0/buildScripts

(12) Reset all favorites
mylocalhost: [~] {112} $ reset
mylocalhost: [~] {113} $ homes

[Sep 27, 2007] freshmeat.net Project details for Closebracket

This is essentially reinvention and re-implementation of OFM context sensitive linkage of extension to commands (via ext file) with he additional twist that if there is no extension file type (for example as discovered by file) is used.

Closebracket lets you define multiple shell actions in a single command to speed up the typing of the most repetitive shell commands. It includes ']' and '][' commands, which are located near the "Enter" key and are easy to type quickly. They invoke primary and secondary actions respectively.

wcd 3.1.6

kcd looks to better for Linux users

Sep 29, 2005 OS: MS-DOS/Windows 3.1/95/98/ME/NT/2000 License: Freeware

N/A Wcd is a program to change directory fast (another Norton Change Directory clone). It saves time typing at the keyboard. One needs to type only a part of a directory name and wcd will jump to it. Wcd has a fast selection method in case of multiple matches and allows aliasing and banning of directories. Wcd also includes a full-screen interactive directory browser with speed search. Features:

Phil Braham this not NCD variant but attempt to enhance pushd/popd functionality.

cdll

readme

Bash scripts A replacement for cd. Keeps unlimited history, setup special directories for quick access

cdll allows easy moving about between directories. When changing to a new directory the current one is automatically put onto a stack. By default 50 entries are kept but this is configurable. Special directories can be kept for easy access - by default up to 10 but this is configurable. The most recent stack entries and the special entries can
be easily viewed.

The directory stack and special entries can be saved to, and loaded from, a file. This allows them to be set up on login, saved before logging out or changed when moving project to project.

In addition, cdll provides a flexible command prompt facility that allows, for example, a directory name in colour that is truncated from the left if it gets too long.

History of visited directories in BASH LG #109

Do you realize how many times you type cd per day? Do you realize how many times you retype the same directory names again and again? Ever since I migrated from 4DOS/NT shell on Windows to using Bash on Unix platforms, I've missed its cd history access. In 4DOS/NT the history of the visited directories can be navigated by Ctrl+PgUp/Dn. Every time you go to a new directory by cd, its name automatically goes on top of an easily accessible history list.

In Bash, cd - switches between the last two directories. This is a function in the right direction but many times I wanted to go to the directory before the last, I dreamed of something like cd -2.

A little scripting creates some sanity in the directory navigation of Bash.

Installing the CD history function

To install the modified CD function, copy acd_func.sh to any directory in your $PATH, or even your home directory. At the end of your .bashrc add source acd_func.sh. Restart your bash session and then type cd --.

lotzmana@safe$ cd --
0  ~

Type cd -- to verify if the installation works. Above you may see the result 0 ~. This shows that you have one directory in your history.

lotzmana@safe$ cd work
lotzmana@safe$ cd scripts
lotzmana@safe$ pwd
/home/petarma/work/scripts
lotzmana@safe$ cd --
 0  ~/work/scripts
 1  ~/work
 2  ~
lotzmana@safe$ cd -2
lotzmana@safe$ pwd
/home/petarma

The cd command works as usual. The new feature is the history of the last 10 directories and the cd command expanded to display and access it. cd -- (or simply pressing ctrl+w) shows the history. In front of every directory name you see a number. cd -num with the number you want jumps to the corresponding directory from the history.

How CD with history works

lotzmana@safe$ nl -w2 -s' '  acd_func.sh
 1 # do ". acd_func.sh"
 2 # acd_func 1.0.5, 10-nov-2004
 3 # petar marinov, http:/geocities.com/h2428, this is public domain

 4 cd_func ()
 5 {
 6   local x2 the_new_dir adir index
 7   local -i cnt

 8   if [[ $1 ==  "--" ]]; then
 9     dirs -v
10     return 0
11   fi

12   the_new_dir=$1
13   [[ -z $1 ]] && the_new_dir=$HOME

14   if [[ ${the_new_dir:0:1} == '-' ]]; then
15     #
16     # Extract dir N from dirs
17     index=${the_new_dir:1}
18     [[ -z $index ]] && index=1
19     adir=$(dirs +$index)
20     [[ -z $adir ]] && return 1
21     the_new_dir=$adir
22   fi

23   #
24   # '~' has to be substituted by ${HOME}
25   [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

26   #
27   # Now change to the new dir and add to the top of the stack
28   pushd "${the_new_dir}" > /dev/null
29   [[ $? -ne 0 ]] && return 1
30   the_new_dir=$(pwd)

31   #
32   # Trim down everything beyond 11th entry
33   popd -n +11 2>/dev/null 1>/dev/null

34   #
35   # Remove any other occurence of this dir, skipping the top of the stack
36   for ((cnt=1; cnt <= 10; cnt++)); do
37     x2=$(dirs +${cnt} 2>/dev/null)
38     [[ $? -ne 0 ]] && return 0
39     [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
40     if [[ "${x2}" == "${the_new_dir}" ]]; then
41       popd -n +$cnt 2>/dev/null 1>/dev/null
42       cnt=cnt-1
43     fi
44   done

45   return 0
46 }

47 alias cd=cd_func

48 if [[ $BASH_VERSION > "2.05a" ]]; then
49   # ctrl+w shows the menu
50   bind -x "\"\C-w\":cd_func -- ;"
51 fi

4-7: cd_func() is a function, variables are declared local and are automatically deleted at the end of the function

8-11: if the function is called with a parameter "--" then it dumps the current content of the directory history. It is stored in the same place pushd/popd keep names -- the directory stack. Storage is the same, access is different.

12-13: Argument $1 is transferred into $the_new_dir for some post-processing. Immediately after that, if there are no parameters we assume that user asked for his home directory.

14-22: If parameter begins with '-' then the user is attempting to access one of the names in the history list. $index gets the number of the directory, then we extract the corresponding name into $adir. For example, dirs +3 dumps directory #3 from the stack.

At this point in $the_new_dir we have either a name specified explicitly as a parameter or a name obtained from the history of previously visited directories.

23-25: If a directory name begins with '~' then this character has to be replaced by the actual home directory name.

26-30: pushd does the actual 'cd'. It also puts the name on top of the directory stack. stdout is redirected to /dev/null in order to completely imitate how 'cd' works. Notice that any output to stderr, for example a message telling that the directory specified by the user doesn't exist will show up, which is again similar to what 'cd' does. The function aborts if pushd fails. We also need the new directory name for further analysis and $the_new_dir carries it down the function.

31-33: Keeping track of more than 10 directories is unproductive. Since we have just pushed one on top of the stack, we trim off any that fall below 11 names deep.

34-44: We loop through all the names in the directory stack. Any name that matches the new current directory is eliminated. Again, we have to translate any name from the list which begins with '~' to its format of fully expanded home directory.

47: We assign cd to be cd_func().

48-51: If the bash version allows for macros to be assigned we make ctrl+w summon the history of visited directories.

This script defines a function. It must be sourced and not executed, so that cd_func() is parsed and stored in the current environment. Try env and you must see it after all environment variables.

Documentation page of the script

Visit the acd_func.sh man page.

For comments on this article please visit or join zepp mailing list.
The text of this page is public domain.

[Mar 27, 2003] Accelerator for Changing Directory

An interesting idea of first letter abbreviations. Implementation can be better, but idea is sound.

Since I moved to Win XP my old and trusted companion for easily jumping between different spots in the directory tree, NCD (Norton Change Directory) has ceased to work.
NCD worked by building a database and by using NCD to make and remove directories, the database could be kept in sync, well almost anyway.
If the directory you wanted to go to had a unique name all you had to type was NCD dir_name but if it was a common name like test you might have had to recall the commandline one or more times to get to the destination.

Another approach would be to note that if you consider part of or the whole branch, from root to the destination, the situation will be less ambigous or even unique Using that idea I came up with this simple solution which IMHO works quite well. The user interface may be rendered idiosyncratic by some, but I welcome you to suggest improvements. The documentation is nothing fancy just a few examples included in the code. The program is tested under Win XP, but I think it could work on other OS's with some minor tweaks. PS. As directory delimiter "\", "/" or even "," can be used. The comma is due to laziness, because on the Swedish keyboard one has to use Shift or Alt Gr key to get slashes!!

So instead of typing
cd "C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\Machin eKeys"
at the DOS prompt, typing the more dense command of
a ,d,a,a,m,c,r,m
will suffice. Or even horrors a ,d,,,,,r,m ;-), but that one is tougher on the filesystem.

@perl -Swx %0 %*
@c:\a_cd.bat
@goto :eof

#!perl
# 
# Utility for quickly changing directory
# Named to "a.bat" in homage to Pr1mOS's change working directory comm
+and *a* (attach) ;-]
# 
# Syntax: 
#   a ,wi,ja,pa ===> cd \Windows\java\Packages
#   a ...\me    ===> cd \Windows\Media
#   a           ===> cd \Windows\Media\Microsoft Office 2000 *OR* a se
+lection of subdirectories
#   a /         ===> cd \
#   a mys,d,*
#   [a] /mysql/data/mysql/
#   [b] /mysql/data/tast/
#   [c] /mysql/data/test/
#   [d] /mysql/Docs/Flags/
#   select:
#
# if ambigous the correct target is selected with alpha keys /a .. zz/

#
# Versions:
# 0.2 2003-02-22 Cleanup
# 0.1 2003-01-31 First working
# 0.0 2003-01-15 Start of Coding
#

use strict;
use warnings;

use Cwd;

my $DEBUG = 0;
my $DELIM = qr{,|\\|\/};  ## Either "," or "/" or \" 
  
my @to = @ARGV ? split(/$DELIM/, $ARGV[0] ) : ();

my @cwd = split(/$DELIM/, cwd);
shift @cwd;  # Remove disk

unless ( @ARGV ) {
    push @to, @cwd, '*';
}
elsif ( $ARGV[0] =~ /^$DELIM/ and not @to ){ 
}
elsif ( $to[0] eq '' ) {
    shift @to;
}
else {
    unshift @to, @cwd;
}

## Lazy-dots
foreach my $part (0 .. $#to) {
    if ( $to[$part] =~ /\.(\.+)/ ) {
        $to[$part] = join('/',  ('..') x length($1));
    }
}

@to = split(/\//, join('/', @to));

## Rel2Abs
my @fixed;
for (@to) {
    if ($_ eq '..') {
        pop @fixed if @fixed;
    } elsif($_ eq '.') {
        ## Skip
    } else {
        push @fixed, $_
    }
}

my @choices = expand('/', @fixed);


my %hash;
my $enum = 'a';
my $choice = '';
if (1 == @choices) {    # Autoselect if only one item to choose
    $hash{a} = $choices[0];
    $choice = $enum;
} elsif(1 < @choices) {
    foreach my $c (@choices) {
        ($hash{$enum} = $c) ;
        print '[', $enum++, "] $c\n";
    }
    
    print "select: ";
    $choice = <STDIN>;
    chomp $choice;
}

# Create a batch file to change the directory in the shell.
open (CD, '>', 'C:/A_CD.BAT') or die "Failed to create CD bat file $!\
+n";
if ( defined $hash{$choice} ) {
    $hash{$choice} =~ s/\//\\/g; # Make windows happy
    print CD "\@CD \"$hash{$choice}\"\n";
} else {
    print CD "\@echo No match\n";
}
close CD;

sub expand {
    my ($bough, @twigs) = @_;

    return $bough unless @twigs; ## Looked it up in the thesaurus ;-]
    
    opendir(my $dh, $bough) || die "Can not check $bough for twigs due
+ to $!\n";
    my $regexp = shift @twigs;
    $regexp =~ s/\*/.*/;
    my @found;  
    foreach my $f ( grep { $_ =~ /^$regexp/i and $_ !~ /^\./}  readdir
+($dh) ) {
        push @found, expand("$bough$f/", @twigs) if (-d "$bough$f");
    }     
    closedir($dh) || die "Bad close for $bough $!\n";
    
    return @found;
}             

__END__

Backtracking with bash

This is a novice take or the subject, all info is wrong ;-)
I was working with linux quite a bit today, and frequently changing between directories, when I wondered if there was a way to go back to the directory I was in previously.

Turns out there is a way:

 cd ~-
So if I was doing something like this:
[pete@bigred /]$ cd /etc
[pete@bigred etc]$ cd /usr/local
[pete@bigred local]$ cd ~-
[pete@bigred etc]$ pwd
/etc
If you want to create a command so you don't have to type ~- you can create an alias:
alias cdb='cd ~-'
This ~- thing works great if you only need to go back one directory, but what if you wanted to go back two directories. Continuing the last code sample:
[pete@bigred etc]$ cd ~-
[pete@bigred local]$ cd ~-
[pete@bigred etc]$ pwd
/etc
We are back to /etc and not / our starting point. What I want is something that keeps a history of the directories I've been to.

It turns out that the Bash (the "Bourne again shell") has a directory stack builtin. Three command line tools for manipulating the stack are avaliable dirs, pushd, and popd. More info about the directory stack in bash here.

If we pushd a directory onto the directory stack, we can retreive the top of the stack using dirs +1. I tried setting up some aliases to get it to work the way I wanted:

alias cdd='pushd'
alias cdb='cd `dirs +1`'
Those worked a bit, but I ran into a lot of problems, especially when in the home directory. Also when you run pushd, popd, or dirs it always prints the contents of the stack, I don't know how to suppress that. So I figured I would post it here, and see if anyone can come up with a solution, or if anyone knows of a better way of going about this.

Isn't it funny how software developers will spend hours of time trying to save a few seconds of their future time.

Permalink | Add Comment | Linux

Comments

On 01/23/2004 at 01:19 AM John wrote:

cd - works. I don't think you need cd ~-.

On 04/24/2004 at 04:50 AM Pritam wrote:

You can suppress the output redirecting the output to /dev/null e.g. pushd <dir-name> >> /dev/null popd >> /dev/null

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites

Internal links

External links



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: April 23, 2019