Ter­mi­nal DirB....................................

Shashank Sharma shows you how Bash nin­jas like him­self cre­ate book­marks to make nav­i­gat­ing di­rec­to­ries quick and easy, us­ing dirb.

Linux Format - - CONTENTS - Our ex­pert is a trial lawyer in Delhi and avid Arch Linux user. He’s al­ways on the hunt for geeky mem­o­ra­bilia. Shashank Sharma

Shashank Sharma cre­ates book­marks to make nav­i­gat­ing di­rec­to­ries quick and easy.

Fea­tures like his­tory ex­pan­sion, tab-com­plete and re­verse lookup make work­ing with Bash easy and fun. Yet no amount of tab-com­ple­tion can help you when you fre­quently have to nav­i­gate be­tween deep-nested di­rec­to­ries. While it wouldn’t be a bother if you had to ac­cess these di­rec­to­ries only once or twice, if you have to con­cur­rently work on mul­ti­ple di­rec­tory trees, then hav­ing sev­eral in­stances of the ter­mi­nal ap­pli­ca­tion open isn’t a prac­ti­cal so­lu­tion, ei­ther.

Dirb en­ables you to cre­ate book­marks to nested di­rec­to­ries that you rou­tinely ac­cess, and then nav­i­gate to them with only a few key strokes, in­stead of typ­ing in the com­plete path ev­ery time. The tool utilises Bash shell func­tions to bring the book­marks func­tion­al­ity, com­monly as­so­ci­ated with browsers, to the com­mand line. Al­though the orig­i­nal pro­gram has since been lost, the tool is still avail­able thanks to its per­mis­sive li­cense, al­beit as a fork on Git.

First, clone the dirb.sh file from the project’s Git page on to your disk (click the green Clone or Down­load but­ton on the project’s Git page at https://github.com/icy­fork/dirb). Now ex­tract the con­tents of the down­loaded zip file, which con­tains the dirb.sh file. Next, en­able dirb to work with Bash by in­form­ing the Bashrc file of the ex­act path to the down­loaded dirb.sh script. You can do this by adding source /path/to/dirb.sh to your ~/.bashrc file us­ing your usual text ed­i­tor.

Dirb ships with a hand­ful of com­mands to help you min­imise the key­strokes when nav­i­gat­ing di­rec­to­ries: s......... save a di­rec­tory book­mark g........ go to a saved di­rec­tory book­mark r......... re­move a saved di­rec­tory book­mark d........ dis­play book­marked di­rec­tory path sl....... print a list of di­rec­tory book­marks

The g com­mand works the same as cd . In fact, the g com­mand has been writ­ten to re­place the in-built cd . This is be­cause users shouldn’t have to use both cd and g to ex­plore di­rec­to­ries. While you can’t use the cd com­mand to nav­i­gate to a saved book­mark, you can use g in place of cd . For ex­am­ple, the com­mand g /etc works just like cd /etc .

Cre­at­ing book­marks

You can use any key to cre­ate a book­mark, ex­cept the spe­cial func­tion keys such as F1-F12, Ctrl, Alt, Shift, and so on. As a rule, if press­ing the key prints some­thing on the screen, it can be used as a book­mark.

If you want to cre­ate a book­mark for the /run/me­dia/ lin­uxlala/Stuff­sies/Vbox-Ma­chines/ di­rec­tory, switch to the di­rec­tory on the ter­mi­nal and then run the sV com­mand. You can now use the gV com­mand to switch to this di­rec­tory ir­re­spec­tive of your cur­rent path.

How­ever, there are only so many book­marks you can cre­ate us­ing a com­bi­na­tion of let­ters and num­bers. Thank­fully, dirb, much like the com­mand line, is case sen­si­tive and so the let­ters v and V can be used to book­mark dif­fer­ent di­rec­to­ries. How­ever, tak­ing this ap­proach may lead to con­fu­sion in the long term.

You can also com­bine let­ters and num­bers to cre­ate even more book­marks. For in­stance, if the let­ter d is used to cre­ate a book­mark for the ~/Down­loads di­rec­tory, you can use dc to book­mark the ~/Doc­u­ments folder.

Be­cause g has been de­signed to com­pletely re­place the cd com­mand, a tricky sit­u­a­tion may arise when you at­tempt to nav­i­gate to a di­rec­tory that has the same name as a saved book­mark. For in­stance, be­low there’s a book­mark d for ~/ Down­loads and also a di­rec­tory named d in ~/. 1. $ mkdir d 2. $ cd Down­loads/ 3. $ s d 4. $ cd .. 5. $ g d 6. $ pwd 7. /home/lin­uxlala/Down­loads 8. $ cd .. 9. $ g ./d 10. $ pwd 11. /home/lin­uxlala/d

The lines above have been num­bered to help you fol­low along the ex­am­ple. In line 5, the com­mand gd is used to

nav­i­gate to the ~/Down­loads di­rec­tory. If, how­ever, you had used the cd d com­mand then you’d have been taken to the

~/d di­rec­tory. Be­cause there’s a saved book­mark with the same name as a di­rec­tory, you must use the g ./d as shown in line 9 in the pre­vi­ous ex­am­ple. You can sim­i­larly use the full stop (.) with the g com­mand when you want to switch to a sub-di­rec­tory that has the same name as a pre­vi­ously saved book­mark.

You also don’t need to switch to a di­rec­tory be­fore cre­at­ing a book­mark for it. The s com­mand ac­cepts the book­mark name and path as ar­gu­ments with the

s <book­mark name> <path> syn­tax as demon­strated in the fol­low­ing ex­am­ple: $ s gitp /run/me­dia/lin­uxlala/Stuff­sies/git-projects/ $ g gitp $ pwd /run/me­dia/lin­uxlala/Stuff­sies/git-projects With dirb, you can also cre­ate a book­mark for a rel­a­tive path in­stead of spec­i­fy­ing the path to an ex­act di­rec­tory. One of the most com­mon di­rec­tory changes one must do when work­ing with the ter­mi­nal is the cd ../../ com­mand. The fol­low­ing ex­am­ple demon­strates the cre­ation and work­ing of a book­mark for this op­er­a­tion: $ cd /home/lin­uxlala/Doc­u­ments/project/files $ g 2up $ pwd /home/lin­uxlala/Doc­u­ments Ir­re­spec­tive of your cur­rent path, when you now run the

g 2up com­mand, you’ll move two di­rec­to­ries up, rel­a­tive to your cur­rent path. Be­cause the 2up book­mark isn’t an­chored to any spe­cific di­rec­tory, you can run the g 2up com­mand from within any di­rec­tory with the same re­sult – mov­ing two di­rec­to­ries up. Depend­ing on your us­age, you can sim­i­larly cre­ate book­marks for fre­quent rel­a­tive path move­ments that you make dur­ing your Bash ex­cur­sions.

Re­mov­ing book­marks

Use the sl com­mand for a list of all the saved book­marks: $ sl 2up gitp stuff­sies d v V e

You can also run the sl -l com­mand, which dis­plays the time and date when the book­marks were last used: $ sl -l Mar 6 18:38 2up Feb 12 18:47 gitp Feb 12 18:46 stuff­sies Feb 12 18:05 d Feb 11 18:33 v

Feb 11 18:33 V Feb 11 17:52 e

Nat­u­rally, your book­mark list will grow with time and so you can use reg­u­lar ex­pres­sions with the sl com­mand to nar­row down your search for book­marks: $ sl -l “s*” Mar 6 19:55 sysd Mar 6 19:55 sec Mar 6 19:55 st Mar 6 18:46 stuff­sies

The above ex­am­ple lists all the book­marks be­gin­ning with s. You must re­mem­ber to en­close the reg­u­lar ex­pres­sion in quotes. The time­stamps are up­dated only when the dirb com­mands s and g are used to ac­cess a book­marked di­rec­tory. So if you were to ac­cess the di­rec­to­ries with the cd com­mand, the time­stamp wouldn’t be up­dated.

Chances are that you might not re­mem­ber the path of a saved book­mark if you haven’t ac­cessed in a long time. When this hap­pens, you can run the d <book­mark name> com­mand to view the path of the saved book­mark: $dV /run/me­dia/lin­uxlala/Stuff­sies/VBox-Ma­chines

To main­tain good house­keep­ing, you should reg­u­larly prune the book­marks and re­move ones that are no longer needed. The r com­mand, de­signed to re­move book­marks that aren’t re­quired, ac­cepts a book­mark name as the ar­gu­ment. The com­mand r 2up will re­move the 2up book­mark we cre­ated. You can now re­cy­cle the freed-up book­mark for some other di­rec­tory or path.

De­spite be­ing sim­plis­tic, dirb is a highly use­ful tool that can be an as­set for home users and sysad­mins alike. What’s more, it doesn’t re­quire any time to setup or mas­ter and makes work­ing with Bash even more con­ve­nient. LXF

You’ll learn the use­ful­ness of the sl com­mand once you get started with book­mark­ing your most-used di­rec­to­ries.

If noth­ing else the DirB script can help you mas­ter the art of vari­ables and the if/else loops in Bash script­ing. En­hance your Ter­mi­nal-fu Sub­scribe now at

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.