Interactive command line

After you start crab, e.g. by scanning a directory

	$ crab myproject 

you'll get an interactive Crab command line:

	CRAB>

You can enter SQL queries or Crab commands. SQL queries need a semicolon on the end; commands do not.

Use %quit to finish the Crab session.
Use %show to see current Crab settings
Use %help to access Crab reference info

You can cancel a running scan or query with Ctrl+C

Multi line queries

If you write each query as one long line, you can recall and edit queries from your history (with Ctrl+P). But it is difficult to see what is going on, especially when there are subqueries.

Another option is to edit multi-line queries in a text editor such as TextEdit or Notepad, then paste them into the Crab command line. You can paste in one go, you don't need to paste line by line. Don't forget the semicolon at the end of the last line.

A third option is to write the query in a text editor, save it as a Crab script file, and run the script from Crab using the %read command. e.g.

	CRAB> %read '/somepath/myscript.crab'

Layout of query results

By default, query results at the Crab command line are shown as a set of field:value pairs, called a dictionary. This is convenient for displaying long fields such as paths. Set this output mode with

    %mode dict

CRAB> select * from files limit 3;
{fileid: 1, name: PYTHONDROPBOX, bytes: 782, depth: 0, accessed: 2015-08-15T17:52:12, modified: 2015-07-28T10:14:45, basename: PYTHONDROPBOX, extension: , type: d, mode: drwxr-xr-x, parentpath: /Users/peterdalloz/Dropbox/, fullpath: /Users/peterdalloz/Dropbox/PYTHONDROPBOX/}
{fileid: 2, name: .DS_Store, bytes: 14340, depth: 1, accessed: 2015-08-15T14:42:25, modified: 2015-08-11T10:20:52, basename: .DS_Store, extension: , type: f, mode: -rw-r--r--, parentpath: /Users/peterdalloz/Dropbox/PYTHONDROPBOX/, fullpath: /Users/peterdalloz/Dropbox/PYTHONDROPBOX/.DS_Store}
{fileid: 3, name: getMssql.py, bytes: 694, depth: 1, accessed: 2015-08-07T17:31:52, modified: 2013-09-08T22:37:24, basename: getMssql, extension: .py, type: f, mode: -rw-r--r--, parentpath: /Users/peterdalloz/Dropbox/PYTHONDROPBOX/, fullpath: /Users/peterdalloz/Dropbox/PYTHONDROPBOX/getMssql.py}

Other layout modes for query results can be set with the %mode command.
Each field on a line of its own:

    %mode line

CRAB> select * from files limit 3;
    fileid = 
1
      name = 
PYTHONDROPBOX
     bytes = 
782
     depth = 
0
  accessed = 
2015-08-15T17:52:12
  modified = 
2015-07-28T10:14:45
  basename = 
PYTHONDROPBOX
 extension =
      type = 
d
      mode = 
drwxr-xr-x
parentpath = 
/Users/peterdalloz/Dropbox/
  fullpath = 
/Users/peterdalloz/Dropbox/PYTHONDROPBOX/

    fileid = 
2
      name = 
.DS_Store
     bytes = 
14340
     depth = 
1
  accessed = 
2015-08-15T14:42:25
  modified = 
2015-08-11T10:20:52
  basename = 
.DS_Store
 extension =
      type = 
f
      mode =
-rw-r--r--
parentpath = 
/Users/peterdalloz/Dropbox/PYTHONDROPBOX/
  fullpath = 
/Users/peterdalloz/Dropbox/PYTHONDROPBOX/.DS_Store

    fileid = 
3
      name = 
getMssql.py
     bytes = 
694
     depth = 
1
  accessed = 
2015-08-07T17:31:52
  modified = 
2013-09-08T22:37:24
  basename = 
getMssql
 extension = 
.py
      type = 
f
      mode = 
-rw-r--r--
parentpath = 
/Users/peterdalloz/Dropbox/PYTHONDROPBOX/
  fullpath = 
/Users/peterdalloz/Dropbox/PYTHONDROPBOX/getMssql.py

One column per field, truncated to 10 chars (N.B. use the %width command to change number of characters per column):

    %mode column

CRAB> select * from files limit 3;
fileid      name        bytes       depth       accessed    modified    basename    extension   type        mode        parentpath  fullpath  
1           PYTHONDROP  782         0           2015‑08‑15  2015‑07‑28  PYTHONDROP              d           drwxr‑xr‑x  /Users/pet  /Users/pet
2           .DS_Store   14340       1           2015‑08‑15  2015‑08‑11  .DS_Store               f           ‑rw‑r‑‑r‑‑  /Users/pet  /Users/pet
3           getMssql.p  694         1           2015‑08‑07  2013‑09‑08  getMssql    .py         f           ‑rw‑r‑‑r‑‑  /Users/pet  /Users/pet

Comma delimited output fields (N.B. use the %separator command to change the delimiter):

    %mode list

CRAB> select * from files limit 3;
fileid,name,bytes,depth,accessed,modified,basename,extension,type,mode,parentpath,fullpath
1,PYTHONDROPBOX,782,0,2015-08-15T17:52:12,2015-07-28T10:14:45,PYTHONDROPBOX,,d,drwxr-xr-x,/Users/peterdalloz/Dropbox/,/Users/peterdalloz/Dropbox/PYTHONDROPBOX/
2,.DS_Store,14340,1,2015-08-15T14:42:25,2015-08-11T10:20:52,.DS_Store,,f,-rw-r--r--,/Users/peterdalloz/Dropbox/PYTHONDROPBOX/,/Users/peterdalloz/Dropbox/PYTHONDROPBOX/.DS_Store
3,getMssql.py,694,1,2015-08-07T17:31:52,2013-09-08T22:37:24,getMssql,.py,f,-rw-r--r--,/Users/peterdalloz/Dropbox/PYTHONDROPBOX/,/Users/peterdalloz/Dropbox/PYTHONDROPBOX/getMssql.py

Interactive command line vs batch mode

The interactive command line is useful for exploring data and developing queries, but once you have a query that works the most efficient way to rerun it is to use a text editor to save it as a text file, and execute it with the %read command, or the -init start up option, e.g.

	CRAB> %read analyzetree.crab

You can run scripts without an interactive command line by using the -batch option at Crab startup. This tells Crab to scan any paths provided (if any), and to run any commands or queries provided (if any), and then quit.

	$ crab -batch -init analyzetree.crab

When running queries in batch, output is returned as comma delimited lists unless you use a command line switch at Crab start up. See the "Launching Crab" documentation.

Batch mode instant SQL

You can use Crab to extend Bash with SQL query aliases.

E.g. the following command scans the current directory, and lists the fullpath of all the files:

	$ crab -maxdepth 1 -batch . 'select fullpath from files where type = "f"'

To make an alias for this:

	$ alias fls="crab -maxdepth 1 -batch . 'select fullpath from files where type = \"f\"'"

Now you can type fls in any directory to get the fullpaths of the files it contains.

To make reusable SQL with parameters, the easiest way is to save the call to Crab in a Bash script file. See next section.

Using Crab in Bash script files

Below is an example Bash script file which takes one or more paths as parameters, scans them and runs a query which returns the fullpaths of all their contents.

	#!/bin/bash
/users/peterdalloz/CrabHome/CrabExe/crab -batch -nocolor -maxdepth 1 $@ "select fullpath from files"

Here $@ is a placeholder for the script arguments, which is how the scan paths are passed to Crab

Set up editing shortcuts


To get the full range of shortcuts for editing on the Crab command line, enable the Terminal app option "Use Option as Meta Key". Here's how: Open the Terminal app, click on


Terminal | Preferences | Profiles


Select the color scheme/profile you use, then click on the radio button marked  Keyboard  and check the box  Use Option as Meta key

My Image

Default editing shortcuts (Emacs compatible)

move cursor word left alt + left
move cursor word right alt + right
move cursor start of line ctrl + a
move cursor end of line ctrl + e
delete word left alt + delete
delete word right alt + d
forward delete fn + delete OR ctrl + d
delete current line to cursor ctrl + u
undo ctrl + _
edit in external editor ctrl + ^
complete keyword tab (after first two characters)
recall previous command (step back through history) ctrl + p
step forward through history ctrl + n
search back through history ctrl + r
search forward through history ctrl + s

Alternative editing shortcuts


OSX shortcuts

The easiest way to get OS X text editor shortcuts such as command-left to move cursor to start of line and command-right for end of line, is to use the free app keyRemap4MacBook, now known as Karabiner.   https://pqrs.org/osx/karabiner/

See the section  
For Applications, which has a set of options labelled Enable at only terminal

My Image


External editor

When editing multi-line SQL it is convenient to use an external editor. The shortcut ctrl+^ at the Crab command line opens the editor specified by your EDITOR environment variable. E.g. to use vim assign this from the Bash shell


export EDITOR="vim"

Put your choice of editor in here, and note that no space is allowed around the = sign
To assign this for each shell session, put it in .bash_profile in your home directory.

Another example: to use Sublime Text as your external editor


export EDITOR="subl -w"


(The -w option makes sublime wait until you've closed the edit window before returning you to Crab.)

Then Ctrl + ^ will open the current Crab command line in a new window in Sublime Text. Edit your command there, adding new lines as convenient, and when you're finished use ctrl-s to save, ctrl-w to close the window. This takes you back to the crab command line with the SQL converted to a single-line of input.

Note that you need to close your editor window to regain control in Crab.



Up-arrow command history search

If you want to search your command history with up-arrow: Add the following two lines to the .inputrc file in your home directory. If you don't have an .inputrc file, then make one.


"\e[A":history-search-backward
"\e[B":history-search-forward



Vi compatibility mode

If you want to edit using vi mode: Add the following two lines to the .inputrc file in your home directory. If you don't have an .inputrc file, then make one.


set editing-mode vi
set keymap vi



Libedit mode

If you prefer not to use rlwrap and readline, Crab will default to the OS X readline equivalent library, libedit. This also makes it easy to run Crab without an alias, for example inside shell scripts. Just give the full path to the Crab binary and run it directly, e.g.


~/Users/peterdalloz/CrabHome/CrabExe/crab ~/myproject

To configure libedit shortcuts and other preferences, make an ~/.editrc file. See the editrc man pages.



© 2017 Etia UK