Page 1 of 1

Subdirectory jumper

Posted: Sun Mar 05, 2017 10:09 am
by bwich12
A first stab at a utility whose first version I wrote many years ago for a Windows command processor (then) called 4NT and have used ever since. Its purpose is to make cd'ing around a complex tree of subdirectories faster and easier (I spend my days mostly in CLI). It may help some... but I am actually also very interested to hear from Bash wizards as to how to make this better and more idiomatic.

Basically, j receives a string with the starting letters of a subdirectory to jump to. Say you have /home/Mike/.wine/drive_c/myPrograms/Irfanview/ you can go there via "j /hM.dmI" or via "j ~.dmI" if in /home/Mike or via "j M.dml" if in /home. Obviously, this works better for subdirectories deeper in the hierarchy. "j /es" will just present a menu of the many subdirectories in /etc that start with an s (or any other directory that fits "/e*/s*"). "*" as character is a wildcard, ie matches all directories at that level. The script needs the locate command and works best with an up-to-date database:-).

Code: Select all

j() {
   local jcd=""; local sep=${1:0:1}
   if [[ $sep == "/" ]]; then
   elif [[ $sep == "~" ]]; then
      if [[ $jcd == "/" ]]; then
   for ((i=0;i<${#sep};i++)); do
      local c=${sep:$i:1}
      if [[ $c == "." ]]; then
   #echo $jcd
   jcd=$(locate -r "$jcd$" | xargs -d \\n sh -c 'for i do [ -d "$i" ] && printf "%s\n" "$i"; done' sh {} + )
   if [[ -z $jcd ]]; then
      echo Nothing found, try again.
   local oIFS=$IFS
   if [[ ${#jcd[@]} > 1 ]]; then
      select jcd in "${jcd[@]}"; do break; done
   command cd "$jcd"

I've "stolen" a few bits and pieces from various sites (SO et al) though I have in general a pretty good idea what the various commands do. The one thing that's still puzzling me is the xargs bit... I know what it does and I have a fuzzy picture why it works, but not much more. So I am not entirely sure this is the best (or even a safe) way to proceed. The quoting may seem excessive but some of it is required to support directories with spaces in the name. Comments and suggestions welcome.

Re: Subdirectory jumper

Posted: Sun Mar 05, 2017 12:00 pm
by eemaestro
I just use the BASH alias command.

Code: Select all

$ pcb
jon@mepis1:~/ee/pcb$ alias | grep pcb
alias pcb='cd ~/ee/pcb'
jon@mepis1:~/ee/pcb$ cd

and Dolphin and find when I can't remember where I put a file. I always rename files with spaces in their names to replace_spaces_with_underscore_characters.

Re: Subdirectory jumper

Posted: Mon Mar 06, 2017 3:43 pm
by skidoo
> cd'ing around a complex tree of subdirectories faster and easier

If it's just an academic excercise, okayfine. Otherwise, it sure seems like you're "reinventing the wheel".
Have you explored the inbuilt (and extensible) auto-completion provided by the bash shell? ... etion.html ... ion_part_1 ... ion_part_2 ... etion.html ... c-commands
FWIW, the zsh shell provides even more robust auto-completion.

Code: Select all

add this line to your .bashrc
        bind ‘set completion-ignore-case on’
so that
        cd pic[TAB]
        cd Pictures

> the xargs bit... ... eside-in-a ... to-xargs-l ... Files.html (xargs vs exec)

From this fellow traveler's POV (not a "wizard", I continually lookup references at point of need) my only tweak for your xargs line
would be to test whether inclusion of the xargs -r (aka --no-run-if-empty) option provides a speed improvement (or breaks the script)

For intensive use of `locate` in a live/frugal scenario, check man updatedb.conf and consider changing whatall filesystems are,
or are not, ignored from indexing. IIRC, intermittently (temporarily) changing --prunefs default of not indexing tmpfs has been useful.

Re: Subdirectory jumper

Posted: Tue Mar 07, 2017 6:52 am
by bwich12
skidoo wrote:If it's just an academic excercise, okayfine. Otherwise, it sure seems like you're "reinventing the wheel".
Have you explored the inbuilt (and extensible) auto-completion provided by the bash shell?
It is definitely more than an academic exercise though learning to do stuff with Bash has played a role as well.

I do know about the Bash auto-complete stuff (though I have not yet thoroughly investigated its extensibility options) and I have configured it, as far as possible, to work like the tab-completion I have on my Windows boxen. Bash auto-complete is not bad as far as it goes but I hop around a lot in the directory tree and I have, over time, memorised many dozens shortcuts for my current setup. (The "trouble" is that most of the stuff I type into a CLI is automatic... I want to encode and tag an MP3, say, so I just automatically, w/o much thinking type the right commands. Much of my CLI typing is actually like driving a car, it happens in a sort of auto-pilot mode, if that makes sense.)

As practically all my data files are on NTFS volumes which I can access from both Windows and MX, being able to use those very same shortcuts (and of course to add new ones) will indeed be a valuable time-saver for me... so in this regard the links you mentioned about locate/updatedb are high on my to-do list. I know that I can index NTFS drives but I have not yet looked into the gory details. So thanks for that.

So obviously YMMV... my brain is perhaps wired in a weird way... or is that weird in a wired way:-)

As to zsh... I looked into it and it has some very nice features indeed. However, I do have the feeling that there's a lot more help and advice out there for bash. I may in time graduate to another shell (fish also looks promising) but for the time being I'll stick with bash.

Re: Subdirectory jumper

Posted: Sat Mar 25, 2017 9:39 am
by eemaestro
Yes, you can do it with bash. No, I am not a bash wizard, just a dabbler. Here, for example some BASH gems: ... explained/
If you want to do it with BASH, chances are good somebody already did it. Many functions have been written to extend BASH's capabilities. These are similar to C functions in that they do just one thing, and they do it well. Look through the functions at this website--white text on green background. You might find something that is similar to what you seek. If you can get it work on your computer, then obviously you can modify it, perhaps to do what you want:

Re: Subdirectory jumper

Posted: Thu Apr 20, 2017 10:50 pm
by skidoo
Fasd – A Commandline Tool That Offers Quick Access to Files and Directories

Fasd (pronounced as “fast“) is command-line productivity booster, a self-contained POSIX shell script
which enables quick and more efficient access to files and directories.