Advanced Command Line Skills
The course is part of this learning path
This course covers a selection of more advanced command-line skills that you can use to become a more proficient Linux user. This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.
In this course, you will learn about the following features:
- Tab completion
- Repeat as root
- Rerun a Command Starting with a String
- Reuse Arguments
- Strip Out Comments and Blank Lines
- Reuse the Last Item from the Previous Command
- Anyone with intermediate knowledge of Linux who wants to learn more!
This is an advanced-level course so a good level of Linux knowledge is expected.
Using tab completion is one of the easiest ways to increase your efficiency at the command line. Not only can tab completion save you keystrokes, but it can reveal the possible choices in a given situation. Tab completion can be used to auto-complete file names, directories, commands, arguments and more. Let's look at using tab completion to auto-complete commands.
After you start typing a command you can hit the tab key to invoke tab completion. Tab attempts to automatically complete partially type commands. If there are multiple commands that begin with a string that proceeds tab, those commands will be displayed. You can continue to type and press tab again. When there is only one possibility remaining, pressing the tab key will complete the command.
Let's look at an example. If I type host and press tab tab it will reveal the three possible choices for completion. If I want to complete host name, I'll just press n and tab and that command is completed. And I'll just hit enter to run it. Here's an example of a 12 character command that you can run with just three keystrokes. YP tab, and enter. So if we look at y and hit tab a couple of times, you see we have two choices. Yes and YP domain. So if we already know this, we can hit YP tab and save ourselves a lot of typing.
As you can see tab completion is great for expanding commands. The good news is that it also works on files and directories. Let's look at the contents of our directory here. We have a couple of files named file1.text and file2.text. So let's start a command cat and we'll do F and tab, which reveals the common component. And if we hit tab twice, it will reveal all of our possible choices. So here we can do one and hit that, and that will cat the file. It's an empty file. There's nothing there, but this is just an example of how this would work.
Here's an example of how to use it with directories. As you can see, we have directories that begin with D. Desktop, documents and downloads. So if I wanted to change into the desktop directory, I could type CD DE tab, and enter. And that puts me in the desktop directory. Programmable completion is a feature of the bash shell that allows command arguments to be auto completed. Not all command line programs utilize this feature, so your mileage may vary.
For the commands that do use this feature, you can achieve complex command lines with just a few key strokes. We're going to enable auto completion for our session here on this Ubuntu server. We're going to source /etc/bash_completion. And if you wanted to enable this for your environment, you can put this in your bashRC. And as a matter of fact, it's already there for my account. And if you wanted to enable it globally, you can edit /etc/bash.bashrc. And here we go, you would just uncomment these lines to enable bash programmable completion on an Ubuntu server. The process is gonna be very similar on Fedora and other distributions, slightly different but very similar.
We can use auto-completion with the man command to help speed up our way to the proper documentation we're after. So for example, to auto-complete man subdomain.com, we can type man subd and hit the tab key and hit enter. And it pulls up that man page. Here's an example of git. I have the git command installed and I have a git repo in this directory.
So I'm gonna type git space PU and hit tab. And then you can see that the possible arguments to git are pull and push in this situation. So we'll run git pull, hit tab and enter. So to recap, tab completion can be used to complete file names, directory names, command names and even arguments to commands that use programmable completion.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.