The Computer Oracle

Is there a directory history for bash?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Is There A Directory History For Bash?
00:36 Accepted Answer Score 17
01:26 Answer 2 Score 5
01:47 Answer 3 Score 11
02:23 Answer 4 Score 8
03:05 Thank you

--

Full question
https://superuser.com/questions/299694/i...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #history #filesearch #cd

#avk47



ACCEPTED ANSWER

Score 17


Have a look at autojump:

One of the most used shell commands is “cd”. A quick survey among my friends revealed that between 10 and 20% of all commands they type are actually cd commands! Unfortunately, jumping from one part of your system to another with cd requires you to enter almost the full path, which isn’t very practical and requires a lot of keystrokes.

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. The jumpstat command shows you the current contents of the database. You need to work a little bit before the database becomes usable. Once your database is reasonably complete, you can “jump” to a commonly "cd"ed directory by typing:
j dirspec




ANSWER 2

Score 11


There is

cd -

that is "cd[space][hyphen]" command, which goes to the directory you were before, essentially a "history of depth 1". Repeated "cd -" switches back and forth between two directories.

Quoting man page:

The following operands shall be supported: [...]

When a [hyphen] is used as the operand, this shall be equivalent to the command:

      cd "$OLDPWD" && pwd

Unfortunately, I don't know of a real built-in directory history.




ANSWER 3

Score 8


You can build your own cd command with pushd, popd, dirs builtin commands.

Usage

  • cd -- ( list current history )

  • cd -num ( go to num directory )

  • cd - ( go to previous directory )


function cd ()
{
local hnum=16;
local new_dir index dir cnt;
if ! [ $# -eq 0 ]; then
if [[ $# -eq 2 && $1 = "--" ]]; then
shift;
else
if ! {
[ $# -eq 1 ] && [[ $1 =~ ^(-[0-9]{,2}|-|--|[^-].*)$ ]]
}; then
builtin cd "$@";
return;
fi;
fi;
fi;
[ "$1" = "--" ] && {
dirs -v;
return
};
new_dir=${1:-$HOME};
if [[ "$new_dir" =~ ^-[0-9]{,2}$ ]]; then
index=${new_dir:1};
if [ -z "$index" ]; then
new_dir=$OLDPWD;
else
new_dir=$(dirs -l +$index) || return;
fi;
fi;
pushd -- "$new_dir" > /dev/null || return;
popd -n +$hnum &> /dev/null || true;
new_dir=$PWD cnt=1;
while dir=$(dirs -l +$cnt 2> /dev/null); do
if [ "$dir" = "$new_dir" ]; then
popd -n +$cnt > /dev/null;
continue;
fi;
let cnt++;
done
}



ANSWER 4

Score 5


bash has pushd/popd/dirs. I have this in my .bashrc to auto-push directories onto bash's stack.

#let cd also pushd directories into stack. Use popd to reverse stack
function cd ()
{
if [ -e $1 ]; then
pushd $1 &> /dev/null #dont display current stack
fi
}

Pop these using popd and display the stack using dirs