Before getting into tips and tricks, I want to remind you of the importance of the command line, whether in Linux, Unix, Windows or macOS. Server administrators, system troubleshooters and general Linux users often find that working at the command line is better than working in the graphical user interface (GUI). These tips work across Linux distros, whether you're using Red Hat Enterprise Linux, Fedora, Ubuntu, Debian or another open source Linux distribution.
Why is the command line interface so much better?
- Fast: Frequently, troubleshooters can display settings more quickly by typing in a single command than they could by browsing through four or five layers of a graphical interface. A great example is showing an IP address. Just open the Linux Terminal and type ip addr to see the current IP address.
- Low overhead: Linux servers often do not use an interactive graphical interface. Displaying a GUI consumes a lot of resources better used to provide the server's services. For example, a busy webserver benefits from being able to dedicate CPU time and memory to serving web pages rather than rendering a beautiful 3D desktop that no one looks at anyway.
- Scripting: Automation is a current buzzword in IT, but it's not a new concept. Administrators have been writing commands into text files and executing the files to automate processes for decades. Such shell scripting is essential in efficient, repeatable configuration management, and it's virtually impossible through a GUI.
So, what's the best way to start with the Linux command line? Here is a list of tips and tricks to help you learn your way around Bash, the Linux shell.
What Is the Linux CLI?
The default Linux command line interface (CLI) is the Bash shell (the "Bourne Again SHell"). Bash is user-friendly and predictable, with many powerful features, whether you're logged in as a standard or root user. It's also the environment you'll use when remotely connected via secure shell (SSH).
The command syntax defines how you interact with the system. There are two primary ways of telling the computer what to do using Bash. I'll use some basic commands to demonstrate these syntaxes. I used the mkdir command to create a directory named projects in my current working directory (in this case, my home directory). I populated it with some new files.
Syntax 1
The more traditional way of executing commands in Bash uses a command -option argument syntax.
- Command: Specifies what you want the system to do
- Option: Modifier to the command (note the preceding dash)
- Argument: What you want the command to act on
For example, to display the contents of the projects directory in the file system, type the following executable:
$ ls projects
Figure 1: The ls projects command displays the contents of the projects file
In this example, the command is ls, and the argument (what you want to be displayed) is projects. There are no options.
You can modify this command with options like -a or -l.
$ ls -a projects
Figure 2: The ls -a projects command displays all contents, including hidden files
The -a option modifies the functionality of ls so it displays all files (including hidden files).
Another example is the -l option, which lists the file contents in long format (displaying permissions):
$ ls -l projects
Figure 3: The ls -l projects command displays the contents in long format, showing permissions
You could even combine the two options:
$ ls -la projects
Figure 4: The ls -la projects command displays all contents in long format
These options display all files in long format in the projects directory.
Syntax 2
The other approach to issuing Bash commands uses a subcommand component. You might think of subcommands as a variation of options.
The syntax is command subcommand argument.
- Command: Specifies what you want the system to do
- Subcommand: Modifier or additional function for the command
- Argument: What you want the command to act on
A great example of this structure is the ip command. To display the system's Internet Protocol (IP) configuration, type:
$ ip addr
Figure 5: The ip addr command displays IP address information
The addr subcommand provides further detail on the ip command. The result displays all IP addresses on all interfaces. To see the IP address for a specific interface, add another option and argument, such as show eth0.
$ ip addr show eth0
Figure 6: The ip addr show eth0 displays IP address information for the eth0 interface
Many newer Linux commands use this structure.
Read the Directions
One of the most intimidating aspects of the Linux command prompt is remembering all these different commands with their options. You don't really have to recall the options. The Linux manual pages ("man pages") system is quick and easy to reference. It displays the available options for every command.
For example, the date command is pretty straightforward. Unmodified, it simply displays the current time and date. However, you can modify its output a lot, but you must use many odd options.
Type the following command to see the date man page:
$ man date
This page gives you plenty of information, including examples, to modify date as needed.
Use Tab Completion
Tab completion is a powerful part of Bash, and multiple ways of using it to your advantage exist. The most basic use is to autocomplete commands or file names, but it can also display a group of files or subcommands.
Autocomplete Commands and Names
Tab completion is probably my favorite Bash feature. Once you type in enough of a command, directory name, or file name to be unique, press the Tab key, and Bash will do the rest. Suppose you have a text file named 2023_sales_report_by_region.csv. Not a fun file name to type. Perhaps it's nestled in a directory with regional sales reports from other years, making "2023" a unique string. To view the contents of a file, type the cat command followed by 2023, and press Tab:
Figure 7: Pressing tab autocompletes this long file name
Bash takes care of the rest, saving you from typing the remaining 26 characters. Even better, it also prevents any typos you might have generated while typing the name in manually. Tab completion is also a great shortcut when using the cd command to change directories.
What if it's Not Unique?
What if you're experimenting with this trick, and it appears to fail? For example, you type 2023<TAB> and nothing appears. Or perhaps only part of the filename appears. In that case, what you typed either doesn't exist in that directory or isn't unique. In other words, you may have two or more text files that begin with "2023" and Bash doesn't know which one you want. It will autocomplete as much of the file name as it can.
You have options:
- Continue typing the file name manually until it is unique.
- Press the Tab key twice, which displays all possible options so you can select the one you want.
Display the Available Subcommands
Here's a great trick with tab completion and the command subcommand syntax. Type in the primary command, press Space, then press the Tab key twice. Bash displays all available subcommands for that primary command!
Try it with this example:
$ ip <TAB><TAB>
Don't forget the space after ip, or it won't work.
Figure 9: Type the ip command, a space, and then press Tab to see the available subcommands
Know Your History
Remember when I said above that Bash is user-friendly? It records your recent commands in a text file. More importantly, it allows you to reference that file and repeat commands.
First, type history. Bash displays the recent commands. If you experimented with the examples above, you should be able to identify the commands you used.
Figure 10: The history command displays recent commands
Observe the final command in the list, whatever it is. Now, press the Up arrow key one time. Did that same command appear? Yes, it did!
Type history again and observe the third most recent command. Press the Up arrow key three times, and you should see that command. Use the Up and Down arrow keys to navigate through this list. Press Enter when Bash displays the command you want to repeat.
Notice Bash lists the output from the history command on numbered lines. You can repeat a command by typing an exclamation point and the line number. For example, to repeat the command on line 42—perhaps the hostname command—type !42.
Note: Linux administrators refer to the exclamation point as bang. If I were teaching you this trick verbally, I would have said, "Type bang forty-two."
The next time you enter a command and forget to precede it with sudo, repeat it by typing sudo !!. The two bang characters represent the most recent command.
One great way of using history is editing mistyped commands. Suppose you manually typed the following command:
$ cat 2023_sales_rport_by_region.csv
Oh no! Now you see that you misspelled "report" in the middle of the command. Of course, you could retype the whole thing, but that's tedious, and you risk a different typo. Instead, press the Up arrow key to display the mistyped command, use the Left and Right arrow keys to move the cursor to the typo, and add the missing letter. Press Enter.
Figure 11: Use history to repeat a mistyped command, then edit the command instead of retyping it
You can use history to fix a long command rather than manually retyping it.
Practice for Efficiency and Accuracy
Linux power users benefit from the command line, and it's an essential tool for Linux system administration. Ensure you're comfortable with the two syntax styles and accessing man pages. Make using tab completion second nature for efficiency and accuracy. Finally, get in the habit of using history to repeat (and modify) recent commands.
There are many more tips and tricks to get the most out of Bash on your Linux system, but this tutorial will get you started.
Learn the skills you need with CompTIA CertMaster Learn. Sign up today for a free trial today!