Although the Linux desktop has been subject to enormous improvements over the past twenty years (with perhaps the most notable change coming by way of the Ubuntu Unity interface), the
1. Create and enter a directory using one command
Creating and subsequently entering a new directory is such a common task it seems that there should be a shortcut for executing both commands in the shell. While it’s not, you can add the following function to your .bashrc file:
Then run source .bashrc to read the changes into memory, and complete both tasks using the mkcd command:
wjgilmore@ubuntu:~$ mkcd articles
2. Return to the previous directory
When you need to move from a deeply embedded directory and want to return to the original directory you could pass the previous path into the cd command, but a little-known cd argument makes this trivial. This sequence demonstrates the behavior:
wjgilmore@ubuntu-laptop:~$ cd -
3. Creating directory bookmarks
Continuing along with the theme of directory interaction, there are some directories that you will inevitably return to time and again. It's possible to create bookmarks that allow you to quickly navigate to those directories by adding their paths to the $CDPATH shell variable (within your .bashrc file):
Once added, you can navigate directly to the books directory from anywhere within the operating system path simply by executing the following command:
$ cd books
4. Deftly edit the command line
How many times have you tediously edited and executed a series of slightly dissimilar commands? Such as when building the PDF version of various book chapters I'm working on from the Markdown source I regularly execute the following command:
$ pandoc -o html/chapter06.html chapters/chapter06.md --template=templates/html.template
In order to also build the chapter04.md source document command line novices would quickly tire of arrowing up to retrieve the previously executed (above) command from history, and then arrowing left until replacing both instances of chapter06.md with chapter04.md. There are several more efficient ways to perform this task. First, consider using Bash's command line editing keyboard shortcuts (two modes are supported: Emacs and vi), which allow you to quickly navigate to the desired location:
Ctrl + a: Go to beginning of line
Ctrl + e: Go to end of line
Alt + f: Go forward one word
Alt + b: Go backward one word
A second and even more efficient approach involves using command line substitution. The following command will replace the 06 found in the previously executed command with 04:
$ pandoc -o html/chapter06.html chapters/chapter06.md
pandoc -o html/chapter04.html chapters/chapter04.md --template=templates/html.template
Incidentally if you're using the GNOME terminal then the meta (Alt) key won't work as described, because GNOME terminal already binds the Alt key to toolbar commands. Alternatively you can use Shift + Alt as the meta key, but this is a bit awkward. Instead, if you don't require the toolbar command shortcuts, disable them by navigating to Edit -> Keyboard Shortcuts... and disable the Enable menu access keys option.
5. Saving a long command for later use
When working through a sequence of system administration operations, it is possible to type a particularly long command and then realize before executing it a step in the sequence has been left out. Rather than deleting the command, you can save it to the history without executing it by appending a hash mark (#) to the beginning of the command:
$ #this is some ridiculously long command that I want to save
After pressing the Enter button, arrow up and you'll see the command has been saved. To execute the command, just remove the hash mark from the beginning of the line before execution.
6. Save typing using command aliases
The ls command's long listing format (ls -l) can be frequently used, but the hyphen makes it a bit unwieldy when typing furiously. You can create command aliases of for longer commands using the alias command within .bashrc. In this example, the command alias dir is substituted for ls -l:
alias dir='ls -l'
7. Saving more typing by ignoring typos
You're in the terminal zone, blazing from one directory to the next while copying, updating and removing files at will. Or you're not, because the fingers are moving faster than the brain or even keyboard response time can handle, causing you to constantly backtrack and correct your typos. Add the following line to your .bashrc file and the shell will automatically fix any typing blunders you make when identifying file or path names.
shopt -s cdspell
8. Opening applications in the background
When cruising around the command line, you may need to do another task such as respond to an email. Of course, it's possible to open GUI applications from the terminal in the same way you'd execute any other command, done simply by invoking their name, in this case, opening Gimp:
But doing so effectively ends your terminal session, because the application will open in the foreground. If you're regularly opening a particular application from the command-line, consider modifying its default invocation within your .bashrc file:
command gimp "$@" &
Reload your .bashrc file (see the source command) and you'll be able to invoke the Gimp application, passing along the names of any image files you'd like to open, with the added bonus of retaining control of the terminal.
9. Do more with less
The more command is useful for quickly perusing the contents of a text file. Once the file is loaded into the page you can use the forward slash (/) to search the file. The problem is that once you've found the desired string it's not possible to navigate up and inspect the contents that appeared prior to this string. The less command doesn't suffer from this disadvantage, allowing you to scroll both up and down within a text file. The less command is invoked in the same manner as more:
$ less sometextfile.txt
10. Clean up your command line history
The history command is easily one of the most powerful tools at your disposal. But there is one timesaver in particular that deserves mention: the $HISTIGNORE shell variable.
Over time your history list will become incredibly long. Take advantage of the $HISTIGNORE ;variable to mute the recording of any commands you deem irrelevant:
$ export $HISTIGNORE="&:cd:exit:ls"
This will cause all duplicate commands, and the cd, exit, and ls commands to be omitted from the history list.
Speed is key to mastering the command line, and these ten tips and tricks should get you started on your command line mastery. If you would like to share any other tips, please contact me via my Web site.
ABOUT THE AUTHOR: Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including Easy PHP Websites with the Zend Framework, Easy PayPal with PHP, and Beginning PHP and MySQL, Fourth Edition. Follow him on Twitter at @wjgilmore.
This was first published in July 2011