Welcome!

Please read this important information about MX sources lists.
News
  • MX Linux on social media: here
  • Mepis support still here
Current releases
  • MX-16.1 release info here
  • antiX-17-b1-full release info here
  • antiX-16.2 release info here
New users
  • Please read this first, and don't forget to add system and hardware information to posts!
  • Read Forum Rules

Subdirectory jumper

Here you can exchange scripts that you created or have permission to share with other users.
Message
Author
User avatar
bwich12
Forum Regular
Forum Regular
Posts: 159
Joined: Tue Jul 12, 2016 9:14 am

Subdirectory jumper

#1 Postby bwich12 » Sun Mar 05, 2017 10:09 am

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
      sep=${1:1}
   elif [[ $sep == "~" ]]; then
      jcd="$HOME"
      sep=${1:1}
   else
      jcd="$(pwd)"
      if [[ $jcd == "/" ]]; then
         jcd=""
      fi
      sep=$1
   fi
   jcd="^$jcd"
   for ((i=0;i<${#sep};i++)); do
      local c=${sep:$i:1}
      if [[ $c == "." ]]; then
         c="\."
      fi
      jcd="$jcd/$c[^/]*"
   done
   #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.
      return
   fi
   local oIFS=$IFS
   IFS=$'\n'
   jcd=($jcd)
   IFS=$oIFS
   if [[ ${#jcd[@]} > 1 ]]; then
      select jcd in "${jcd[@]}"; do break; done
   fi
   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.

User avatar
eemaestro
Forum Guide
Forum Guide
Posts: 1010
Age: 2016
Joined: Wed Aug 02, 2006 12:12 pm

Re: Subdirectory jumper

#2 Postby eemaestro » Sun Mar 05, 2017 12:00 pm

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
jon@mepis1:~$

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.

skidoo
Forum Regular
Forum Regular
Posts: 548
Joined: Tue Sep 22, 2015 6:56 pm

Re: Subdirectory jumper

#3 Postby skidoo » Mon Mar 06, 2017 3:43 pm

> 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?
https://www.gnu.org/software/bash/manua ... etion.html
https://debian-administration.org/artic ... ion_part_1
https://debian-administration.org/artic ... ion_part_2
http://fahdshariff.blogspot.com/2011/04 ... etion.html
http://unix.stackexchange.com/questions ... c-commands
FWIW, the zsh shell provides even more robust auto-completion.
Might

Code: Select all

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

> the xargs bit...
http://unix.stackexchange.com/questions ... eside-in-a
http://stackoverflow.com/questions/8759 ... to-xargs-l
https://www.gnu.org/software/findutils/ ... 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.

User avatar
bwich12
Forum Regular
Forum Regular
Posts: 159
Joined: Tue Jul 12, 2016 9:14 am

Re: Subdirectory jumper

#4 Postby bwich12 » Tue Mar 07, 2017 6:52 am

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.

User avatar
eemaestro
Forum Guide
Forum Guide
Posts: 1010
Age: 2016
Joined: Wed Aug 02, 2006 12:12 pm

Re: Subdirectory jumper

#5 Postby eemaestro » Sat Mar 25, 2017 9:39 am

Yes, you can do it with bash. No, I am not a bash wizard, just a dabbler. Here, for example some BASH gems: http://www.catonmat.net/blog/top-ten-on ... 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:
http://www.commandlinefu.com/commands/browse

skidoo
Forum Regular
Forum Regular
Posts: 548
Joined: Tue Sep 22, 2015 6:56 pm

Re: Subdirectory jumper

#6 Postby skidoo » Thu Apr 20, 2017 10:50 pm

Fasd – A Commandline Tool That Offers Quick Access to Files and Directories
http://www.tecmint.com/fasd-quick-access-to-linux-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.


Return to “Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest