Terminal DirB....................................
Shashank Sharma shows you how Bash ninjas like himself create bookmarks to make navigating directories quick and easy, using dirb.
Shashank Sharma creates bookmarks to make navigating directories quick and easy.
Features like history expansion, tab-complete and reverse lookup make working with Bash easy and fun. Yet no amount of tab-completion can help you when you frequently have to navigate between deep-nested directories. While it wouldn’t be a bother if you had to access these directories only once or twice, if you have to concurrently work on multiple directory trees, then having several instances of the terminal application open isn’t a practical solution, either.
Dirb enables you to create bookmarks to nested directories that you routinely access, and then navigate to them with only a few key strokes, instead of typing in the complete path every time. The tool utilises Bash shell functions to bring the bookmarks functionality, commonly associated with browsers, to the command line. Although the original program has since been lost, the tool is still available thanks to its permissive license, albeit 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 Download button on the project’s Git page at https://github.com/icyfork/dirb). Now extract the contents of the downloaded zip file, which contains the dirb.sh file. Next, enable dirb to work with Bash by informing the Bashrc file of the exact path to the downloaded dirb.sh script. You can do this by adding source /path/to/dirb.sh to your ~/.bashrc file using your usual text editor.
Dirb ships with a handful of commands to help you minimise the keystrokes when navigating directories: s......... save a directory bookmark g........ go to a saved directory bookmark r......... remove a saved directory bookmark d........ display bookmarked directory path sl....... print a list of directory bookmarks
The g command works the same as cd . In fact, the g command has been written to replace the in-built cd . This is because users shouldn’t have to use both cd and g to explore directories. While you can’t use the cd command to navigate to a saved bookmark, you can use g in place of cd . For example, the command g /etc works just like cd /etc .
Creating bookmarks
You can use any key to create a bookmark, except the special function keys such as F1-F12, Ctrl, Alt, Shift, and so on. As a rule, if pressing the key prints something on the screen, it can be used as a bookmark.
If you want to create a bookmark for the /run/media/ linuxlala/Stuffsies/Vbox-Machines/ directory, switch to the directory on the terminal and then run the sV command. You can now use the gV command to switch to this directory irrespective of your current path.
However, there are only so many bookmarks you can create using a combination of letters and numbers. Thankfully, dirb, much like the command line, is case sensitive and so the letters v and V can be used to bookmark different directories. However, taking this approach may lead to confusion in the long term.
You can also combine letters and numbers to create even more bookmarks. For instance, if the letter d is used to create a bookmark for the ~/Downloads directory, you can use dc to bookmark the ~/Documents folder.
Because g has been designed to completely replace the cd command, a tricky situation may arise when you attempt to navigate to a directory that has the same name as a saved bookmark. For instance, below there’s a bookmark d for ~/ Downloads and also a directory named d in ~/. 1. $ mkdir d 2. $ cd Downloads/ 3. $ s d 4. $ cd .. 5. $ g d 6. $ pwd 7. /home/linuxlala/Downloads 8. $ cd .. 9. $ g ./d 10. $ pwd 11. /home/linuxlala/d
The lines above have been numbered to help you follow along the example. In line 5, the command gd is used to
navigate to the ~/Downloads directory. If, however, you had used the cd d command then you’d have been taken to the
~/d directory. Because there’s a saved bookmark with the same name as a directory, you must use the g ./d as shown in line 9 in the previous example. You can similarly use the full stop (.) with the g command when you want to switch to a sub-directory that has the same name as a previously saved bookmark.
You also don’t need to switch to a directory before creating a bookmark for it. The s command accepts the bookmark name and path as arguments with the
s <bookmark name> <path> syntax as demonstrated in the following example: $ s gitp /run/media/linuxlala/Stuffsies/git-projects/ $ g gitp $ pwd /run/media/linuxlala/Stuffsies/git-projects With dirb, you can also create a bookmark for a relative path instead of specifying the path to an exact directory. One of the most common directory changes one must do when working with the terminal is the cd ../../ command. The following example demonstrates the creation and working of a bookmark for this operation: $ cd /home/linuxlala/Documents/project/files $ g 2up $ pwd /home/linuxlala/Documents Irrespective of your current path, when you now run the
g 2up command, you’ll move two directories up, relative to your current path. Because the 2up bookmark isn’t anchored to any specific directory, you can run the g 2up command from within any directory with the same result – moving two directories up. Depending on your usage, you can similarly create bookmarks for frequent relative path movements that you make during your Bash excursions.
Removing bookmarks
Use the sl command for a list of all the saved bookmarks: $ sl 2up gitp stuffsies d v V e
You can also run the sl -l command, which displays the time and date when the bookmarks were last used: $ sl -l Mar 6 18:38 2up Feb 12 18:47 gitp Feb 12 18:46 stuffsies Feb 12 18:05 d Feb 11 18:33 v
Feb 11 18:33 V Feb 11 17:52 e
Naturally, your bookmark list will grow with time and so you can use regular expressions with the sl command to narrow down your search for bookmarks: $ sl -l “s*” Mar 6 19:55 sysd Mar 6 19:55 sec Mar 6 19:55 st Mar 6 18:46 stuffsies
The above example lists all the bookmarks beginning with s. You must remember to enclose the regular expression in quotes. The timestamps are updated only when the dirb commands s and g are used to access a bookmarked directory. So if you were to access the directories with the cd command, the timestamp wouldn’t be updated.
Chances are that you might not remember the path of a saved bookmark if you haven’t accessed in a long time. When this happens, you can run the d <bookmark name> command to view the path of the saved bookmark: $dV /run/media/linuxlala/Stuffsies/VBox-Machines
To maintain good housekeeping, you should regularly prune the bookmarks and remove ones that are no longer needed. The r command, designed to remove bookmarks that aren’t required, accepts a bookmark name as the argument. The command r 2up will remove the 2up bookmark we created. You can now recycle the freed-up bookmark for some other directory or path.
Despite being simplistic, dirb is a highly useful tool that can be an asset for home users and sysadmins alike. What’s more, it doesn’t require any time to setup or master and makes working with Bash even more convenient. LXF