Skip to main content
⌛ FINAL DAYS of CYBER DEALS! Save up to 60% Now

Five practical guides for managing Linux terminal and commands

December 4, 2020Announcements

Guest Post from Matt Zand and Kevin Downs

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!

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

If you like to learn more about Linux, reading the following articles and tutorials are highly recommended:

About Authors

Matt Zand is a serial entrepreneur and the founder of 3 tech startups: DC Web Makers, Coding Bootcamps and High School Technology Services. He is a leading author of Hands-on Smart Contract Development with Hyperledger Fabric book by O’Reilly Media. He has written more than 100 technical articles and tutorials on blockchain development for Hyperledger, Ethereum and Corda R3 platforms. At DC Web Makers, he leads a team of blockchain experts for consulting and deploying enterprise decentralized applications. As chief architect, he has designed and developed blockchain courses and training programs for Coding Bootcamps. He has a master’s degree in business management from the University of Maryland. Prior to blockchain development and consulting, he worked as senior web and mobile App developer and consultant, angel investor, business advisor for a few startup companies. You can connect with him on LI: https://www.linkedin.com/in/matt-zand-64047871

Kevin Downs is Red Hat Certified System Administrator or RHCSA. At his current job at IBM as Sys Admin, he is in charge of administering hundreds of servers running on different Linux distributions. He is a Lead Linux Instructor at Coding Bootcamps.

Thank you for your interest in Linux Foundation training and certification. We think we can better serve you from our China Training site. To access this site please click below.

感谢您对Linux Foundation培训的关注。为了更好地为您服务,我们将您重定向到中国培训网站。 我们期待帮助您实现在中国区内所有类型的开源培训目标。