Learn how to work in Linux terminal!
Five practical tips for managing Linux terminal and commands
How it works
In this article we will take a brief look at some time saving tricks you can use when interacting with the terminal. As a system administrator you will spend most, if not all, of your time in a terminal. Knowing tricks like these can save you a lot of time and make you a more efficient system administrator. These skills aren’t just useful for only administrators though. All of these shortcuts can be used by anyone who interacts with a command line interface. Check these out and enjoy!
One-to-One Live Linux Classes
Coding Bootcamps school offers One-to-One Live Linux Classes for Beginners.
1- Keys for Command Line Editing
If you are familiar with Linux, then we are guessing you know how to run commands by typing them in at the shell prompt. The text you type at a shell prompt is called the command line (it’s also called the input line). The following table describes the keystrokes used for typing command lines.
Keystroke(s) | Function |
Up | Move back one command in the history list (see section 3) |
Down | Opposite of Up arrow (moving forward one command) |
Left | Move the cursor back one character. |
Right | Move the cursor forward one character. |
Esc f | Move the cursor forward one word. |
Esc b | Move the cursor back one word. |
Ctrl-A | Move the cursor to the start of line. |
Ctrl-E | Move the cursor to the end of line. |
Ctrl-D | Delete current character. Beginners usually confuse this with Backspace |
Backspace | Delete previous character. |
Esc d | Delete current word. |
Ctrl-K | Kill or cut all text on the input line, from the character the cursor is underneath to the end of the line |
Ctrl-U | Delete from beginning of line. |
Esc k | Delete to end of line. |
Ctrl-Y | Fetch last item deleted. |
Esc . | Insert last word of previous command. |
Ctrl-L or Ctrl-I | Clear the screen, placing the current line at the top of the screen. Alternatively, you can use “clear” command. |
Tab | Try to complete the current word, interpreting it as a filename, username, variable name, hostname, or command based on the context. |
Esc ? | List or suggest the possible completions |
In addition to keystrokes covered in the above table, often you may run into situations when you have to suspend a currently executed command, so to do so you just type Ctrl-C or Curl-Z to cancel its execution. These keystroke commands are very handy, for instance, when a program is taking too long to execute and you want to try something else.
The following sections describe some important features of command line editing, such as quoting special characters and strings, re-running commands, running multiple commands, and command history. Here is a good article if you like to learn more about how Linux OS works.
2- Passing Special Characters to Commands
Some characters are reserved and have special meaning to the shell on their own. To avoid these characters being interpreted you must quote it by enclosing the entire argument in single quotes (‘’). For example,
$ echo 'All sorts of things are ignored in single quotes, like $ & * ; |.'
All sorts of things are ignored in single quotes, like $ & * ; |.you can also escape a single character being interpreted by adding a backslash character before it:
$ echo \$date $ date
Normally, this would echo back the date variable. Instead the backslash character told the terminal to ignore $ as a special character and not to interpret it.
When the argument you want to pass has one or more single quote characters in it, enclose it in double quotes, like so:
$ echo "This is how we can use single ' within double quotes"
This is how we can use single ‘ within double quotes
Double quotes works like single quotes except it will still allow the shell to interpret some characters such as dollar signs, backquotes, backslashes and variables:
$ echo "The current Oracle SID is $ORACLE_SID"
The current Oracle SID is test
Below are two ways for working quite the opposite of double and single quotes:
- `command`
- $(command)
Back quotes actually force the interpretation of characters and commands they enclose:
$ today=`date '+%A, %B %d, %Y'` $ echo $today Wednesday, November 25, 2020
Likewise, $() actually forces the interpretation of characters and commands they enclose:
$ today=$(date '+%A, %B %d, %Y') $ echo $today Wednesday, November 25, 2020
Special backslash escape sequences for certain characters are commonly included in a string, as shown in the following examples:
Example 1– Break a line with \ character
echo "This will print as two lines."
The output:
# This will print
# as two lines.
echo "This will print \ as one line."
The output
# This will print as one line.
Example 2– Break a line with –e as escape character and \n as a new line
echo -e "this is in \n two lines" this is in two lines
Example 3– Escape double quotes with \ character
echo "Hello"
# Hello
echo "\"Hello\", he said."
# “Hello”, he said.
Example 4– Escape variable
echo "\$variable01"
# results in $variable01
Example 5– Escape \ character
echo "\\"
# Results in \
3- Repeating the Last Command You Typed
In the Linux terminal, you can easily reuse the previous commands. You can do so by just simply typing the Up arrow key to select the last executed commands in your Shell, followed by hitting Enter to execute them. The Up arrow method works fine in all Linux distributions or terminals. Besides Up arrow, you can have a recourse to other methods. Here we cover two more methods:
Method 1- Exclamation Mark
You can run any last executed command, by just typing double exclamation marks, and hit ENTER:
$ !!
Method 2- Using reverse-search
You can use the bash reverse-incremental search feature, C-r, to search, in reverse, through your command history. You’ll find this useful if you remember typing a command line with ‘foo’ in it recently, and you wish to repeat the command without having to retype it. Type C-r followed by the text foo, and the last command you typed containing ‘foo’ appears on the input line.
An incremental search builds the search string in character increments as you type. Typing the string ‘toy’ will first search for (and display) the last input line containing a ‘t’, then ‘to’, and finally ‘toy’, as you type the individual characters of the search string. Typing C-r again retrieves the next previous command line that has a match for the search string.
Perhaps the easiest, and most widely used method to recall commands is simply inputting the up arrow to your terminal. This will show the last command you typed in. Each time you hit the up arrow it will recall the previous command. You can do this all the way back to your first command inputted for your session.
4- Running a List of Commands
To run more than one command on the input line, type each command in the order you want them to run, separating each command from the next with a semicolon (‘;’). Bear in mind, when multiple commands are executed in one command line, it runs them simultaneously and failure of one command has no effect on the execution of other commands.
Another approach in running multiple commands at once is to separate commands with && instead of semicolon. In this case, Linux executes commands sequentially from left to right, meaning a command starts only after its previous command (if exists) executed successfully. As a result, when using this method, if a command fails, all of its succeeding commands will not be executed.
You’ll sometimes find this useful when you want to run several non-interactive commands in sequence.
To run the who command three times, type:
$ who; who; who Or $ who&& who&& who
Another method for running multiple commands at once is to use pipes. Unlike the previous method (using ; ) where commands are independently executed, under pipes the output of one command (from left to right) becomes input of the next command, and it goes on till it processes the last command on the right side. Then, it outputs the final result. In other words, using pipes you can connect or join multiple commands together. Below is an example of simple pipe.
$ ls -l | head -4
It list files and folders in the currently directly, then using pipe, it filters and outputs only top 4 items from the list.
5- Command History
You can also view the entire command history for your current session. By typing history into the command line you will be able to see all commands that were run in your current session. You can then run a certain command based on its number simply by typing in !<number>. Below is an example:
$ history 1 ls 2 echo “hello” 3 pwd 4 cd 5 ls -la $ !2 hello $
Summary
This is just a brief overview of the many tricks and methods out there for saving some time when using the command line. We have only scratched the surface. As you continue to use the terminal you will find your own methods to become more efficient. It is important to hone your craft. You want to be as efficient as possible. New utilities and commands are constantly being created, so be sure to do your own research on what’s out there. After all, Linux is open source!
Resources- Self-Paced Linux Courses
If you like to learn more about Linux, taking the following courses is highly recommended:
- Introduction to Linux OS
- Intro to Linux Bash Scripting
- Advance Linux Bash Scripting
- Linux Management for System Admins
- How to manage Red Hat or CentOS Linux servers- Intermediate level
Resources- Free Courses
Here is the list of our 9 free self-paced courses that are highly recommended:
- IT Career Roadmap Explained
- Web Design with Bootstrap
- User Experience Best Practices
- Intro to Search Engine Optimization
- Web Design with WordPress
- Introduction to Drupal CMS
- Intro to Joomla CMS
- Intro to Cybersecurity
- Introduction to Cloud Technology
Resources- Live Linux Courses
If you like to learn more about Linux, take the following live Linux classes is highly recommended:
- Linux shell and bash scripting via hybrid training
- Hands-on Linux programming for system administration via hybrid training
- Red Hat Certified System Administrator Live Training Class
- Live training class for obtaining Red Hat Certified Engineer certification
- Essential Linux Commands in 6 Hours by Hands-on Exercises
- Linux Bash & Shell Scripts in 6 Hour by Hands-on Exercises
- Linux and bash scripting- Private tutoring sessions
Resources- Tutorials
If you like to learn more about Linux, reading the following articles and tutorials is highly recommended:
- Comprehensive Review of Linux File System Architecture and Management
- Comprehensive Review of How Linux File and Directory System Works
- Comprehensive list of all Linux OS distributions
- Comprehensive list of all special purpose Linux distributions
- Linux System Admin Guide- Best Practices for Making and Managing Backup Operations
- Linux System Admin Guide- Overview of Linux Virtual Memory and Disk Buffer Cache
- Linux System Admin Guide- Best Practices for Monitoring Linux Systems
- Linux System Admin Guide- Best Practices for Performing Linux Boots and Shutdowns