Package: dirlog
ContentsRamblingsInstallationUsagecfunctiondlogcommand wrapperdirlog is a wrapper forcdthat keeps a database of all the
directories you visit, so you only have to type the first few letters of
the basename to get back to them the next time.RamblingsThe tradition of our unix forefathers teaches us that the command line
is the most efficient way to do any task on a unix system. This is
frequently the case. However, there are a few tasks which are more
difficult. For example, editing video.dirlogwill not fix that.dirlogdoes something else useful however; It makes it so you have
to type long directory names less frequently, or cd+ls your way around
the filesystem until you get where you’re going. The primary function of
dirlog is to replace thecdcommand.Now, you can’t actually replace thecdcommand. It’s a shell builtin,
and itmustbe a shell builtin. Each process has its own working
directory, inherited from its parent. There is no (sane) way for a child
process to change the directory of its parent. This is whycdmust
run as part of the shell’s process itself. This is whydirlogis
used as part of a shell function to wrap thecdcommand. There is no
other way.InstallationYou can install dirlog from PyPI withpip3or by cloning this repo
and usingpip3. e.g. (you can usepip2as well, but you may
encounter some bugs.$gitclonehttps://github.com/ninjaaron/dirlog.git$sudopip3installdirlogOr, perhaps you’ll prefer to install in your user directory (in which
case you must have~/.local/bin/in your path, or wherever your
user’s python stuff is).$pip3install--userdirlogAlternatively, There is an AUR Arch Linux and derived distros.After that, run the thedirlogcommand, which will give you the
function you need to getdirlogandcdto work together.c(){dir="$(dirlog-cd"$@")"if["$dir"!=""];thencd"$dir"&&lsfi}If you run a non-POSIX shell (huge mistake), like fish or tcsh, you’ll
need something else. Here’s the fish version:functioncsetdir(dirlog-cd$argv)if["$dir"!=""]cd"$dir";andlsendendIn fish, you can just enter this at the command line and then usefuncsave cto make it permanent.I don’t know tcsh, so I leave it to you to figure it out.Naturally, you may omit thelscommand, if you wish. I find it
handy.Tip:I tweak the above POSIX script slightly for quickly switching back and
forth betweeen two directories:c(){localdir=$(dirlog-cd"$@")if["$dir"!=""];thenLAST="$PWD"cd"$dir"&&lsfi}b(){c"$LAST"}localis not strictly POSIX, but it works in many shells, and then
I stick the previous directory in a global variable so I can get back
to it quickly. If you want some more sophisticated directory history,
I suppose it would be easy enough to use pushd and popd in a dirlog
wrapper.UsagecfunctionTo start off with, you can doalmosteverything with thecfunction that you can withcd. (Some version ofcdhave some
extra flags.chas none.) However, whenever you usec, it will
remember the complete path of the directory you move to. To return to
that directory, you can simply type the first part of the name of the
directory, and it will take you back to the last directory where the
beginning of the name of matches the hint you give.~$csrc/stupid-projectsetup.pystupid.py~/src/stupid-project$cDownloadsDocumentsMusicPicturespr0nsrc~$# now watch close~$cstsetup.pystupid.py~/src/stupid-project$The more directories you visit, the more will be stored in your history.
Makes it quick to get around.Now, what if you have to directories with the same name, or similar for
the first few characters? It takes you to the matching directory
that was most recently visited. If you want to go back to an earlier
directory that match, you may use numbers to indicate how far back it
is on the list.2is the match before last,3the one before
that, etc.~/src/stupid-project$c~/Documents/stupid-listsamimals-that-smellpeople-who-smellgoverment-agencies-that-smell~/Documents/stupid-lists$cstuamimals-that-smellpeople-who-smellgoverment-agencies-that-smell~/Documents/stupid-lists$# takes us back to this directory~/Documents/stupid-lists$# because it is most recent match~/Documents/stupid-lists$cstu2setup.pystupid.py~/src/stupid-project$This is really fairly trivial, but I have found it to be extremely
handy, if I do say so myself. I use it much more frequently that any
other, eh, “software,” I’ve written. The history is stored in an
independent sqlite database, so it is updated across all shell sessions
simultaneously.You may alsofrom dirlog import cin a python shell to get a native
implementation. The syntax is a bit “magical” for convenience in the
shell. It’s use is documented in the docstring. However, because it is
rather magical, it breakshelp(). (“oops”), so I’ll copy it here.>>>c# goes to home dirDocumentsDownloadsMovies(etc...)>>># prints and extra newline because this is a trick with __repr__>>>c.Mo# assuming you have been there in the past...'Lord of The Rings Trilogy'(etc...)>>># if you need to type a full path, use `/` operator and a string.>>>c/'/etc/sshd'(sshdconfigfiles...)>>># if you don't like all the magic, call with normal syntax:>>>c('/etc/sshd')Don’t use this object in a script. Its __repr__ is a lie.. If you need
dirlog functionality in a script (which you shouldn’t…), use thegetpath()function, orget_and_update()These functions are
non-magicall.dlogcommand wrapperIt recently occurred to me that it might be useful the have this
directory  history mechanism available to other commands.dlogis a
simple way to do this. Put thedlogcommand in front of the command
you wish to run, and it will expand the last argument to the last
matching directory you visited.~/Documents/boring-work$dlogln-srdata.csvstuln-srdata.cvs/home/luser/src/stupid-project~/Documents/boring-work$cDownloadsDocumentsMusicPicturesjunk.txtpr0nsrc~$dlogmvjunk.txtbomvjunk.txt/home/luser/Documents/boring-work~$You may add a subpath, if you wish. No globbing yet :(~$dlogcp-Rsrcbo/boring-codecp-Rsrc/home/luser/Documents/boring-work/boring-code~$As you see, dlog will echo back the command it executes to stderr.You may also access directories further back in the history, using the@symbol (this symbol was chosen because it is not used by any of
the popular shells for globbing, as far as I know).~$dloglsst@2ls/home/luser/Documents/stupid-listsamimals-that-smellpeople-who-smellgoverment-agencies-that-smell~$History and subpaths can be combined, like this:st@2/animals-that-smell.If you wish to use any other argument than the last one for directory
expansion, it must be prefixed with@.~$dlogcp@Mr@2/egg.mp3.cp'/home/luser/Music/Mr. Bungle/Mr. Bungle/egg.mp3'.~$If you have any arguments prefixed in this way, the final argument will
no longer automatically be expanded. However, you can prefix as many
arguments as you like with@in a single command~$dlogtrue@st@bortrue/home/luser/src/stupid-project/home/luser/Documents/boring-work~$Ifdlogis given only one argument, it will simply print the name of
all matching directories to stdout, and not try to execute a command.~$dlogMr/home/luser/Music/Mr.Bungle~$