Best Linux Redirection Commands and Keyboard Tricks

This article will talk about the coolest feature of the command line, which is called I/O redirection. The I/O stands for input/output. With this facility, you may redirect the input and output of commands and connect multiple commands to make a powerful command known as pipelines.

The output of a program is of two types. First, you have the result of the program which contains the data program produces. Secondly, there are status and error messages that instruct you how the program is getting along. If you look at the command Is, you will see that it delivers results and error messages on your screen.

This program sends the output to another file called the standard error. Both standard error and output are connected to the screen, and they are saved into a disk file. Many programs take input from a facility known as standard input stdin attached to a keyboard.

I/O redirection allows you to change where the output goes and where the input flows in from. Usually, output goes to the screen, and the input flows in from the keyboard in Linux; I/O has the power to change that.

I/O Redirection in Linux Shell

I/O redirection helps you in redefining the direction of the output. To redirect the standard output to a file instead of the screen, you have to use the redirection operator that is followed by the file’s name. It is useful more often to store the output of a specific command inside a file. You may tell the shell to direct the Is command’s output to a file named ls-output.txt instead of the screen.

[gha@localhost —1$ is -1 /usr/bin > ls-pt.txt

I have created a lengthy listing of /usr/bin directory and then sent the results to the file ls-pt.txt.

Keyboard Tricks

Linux is an operating system for people who love to type. The command line does not allow a mouse to operate. Linux commands can be exhaustive. Therefore, you should learn a bunch of keyboard tricks.

Text Modification

The following list shows how you can modify text in the Linux command line. The terms killing and yanking refer to cuffing and pasting, respectively. The keyboard trick:

  • CTRL-D will help you delete the character at the present position of the cursor.
  • CTRL-T will help you transpose the character at the present location of the location with the one that is preceding it.
  • Alt-U will help you convert into uppercase the characters from the cursor’s present position to the word’s ending point.
  • Alt-L will help you convert into lowercase the characters from the cursor’s present position to the ending point of the word.
  • Alt-T will help you transpose the words at the cursor’s present position with the preceding one.
  • CTRL-K will help you kill text from the cursor’s present position to the ending point of a line.
  • CTRL-U will help you kill text from the cursor’s present position to the starting point.
  • ALT-D will help you kill text from the cursor’s present position to the end of a current word.
  • ALT-Backspace will help you kill text from the cursor’s present position to the current word’s starting point. If the cursor is presently at the start of a word, it will also kill the previous word.
  • CTRL-Y will help you yank text from the kill-ring and paste it at the cursor’s present position.

Completion Commands

Another way by which the shell will help you is through completion. This occurs when you hit the Tab key while you are typing the command. When you are halfway through a command, you can ENTER tab to complete the command. The important thing to remember is that you should not hit the Enter key.

  • The keyboard trick Alt-$ will help you display the list of all possible completions. You may also do this by pressing the Tab key twice. This is much easier to do.
  • The keyboard trick Alt-* will help you insert the possible completions. This is highly useful when you intend to use more than one match.

Searching History

Bash maintains a history of the commands you type in it. The list of commands is kept in the home directory in a file named .bash_history. The history facility is highly useful for cutting down on the amount of typing you need to do especially when you combine it with command-line editing. Here is the syntax of the history command.

[gha@localhost —1$ history I less

Bash, by default, stores the last 500 commands that you have entered. Suppose you want to find the commands that you used to list /usr/bin. Here is the way to do that.

[gha@localhost —1$ history I grep /usr/bin

There is a list of keystrokes that you may use when you are navigating through the history command. You can use the keyboard shortcut:

  • CTRL-P to move to the latest history entry. This performs the same action as the up arrow.
  • CTRL-N to move to the next history entry. This performs the same action as the down arrow.
  • ALT-> to move to the bottom of the history list that is the current command line.
  • ALT-< to move to the starting point of the list of history.
  • CTRL-R to reverse the incremental search. This option incrementally searches from the current command line up the list of history.
  • CTRL-0 to execute the present hem in the history list and then move on to the next one. This is handy if you are looking forward to re-execute a sequence of commands in the list.
  • ALT-N to forward the search. This is non-incremental.
  • ALT-P to reverse the search. This also is non-incremental. Coupled with this keyboard shortcut, you can type the search string and then press ENTER before you have performed the search.

History Expansion

The shell offers a special type of expansion for different items in the history list by using the! character. We already have seen how a number may follow the exclamation point to insert a particular entry from the history list. There are many other expansion features. History expansion mechanism has many available elements, but this subject is too arcane. You can type the sequence:

  • !! to repeat the latest command. However, it may be easier to press the up arrow and then hit ENTER.
  • !?string to repeat the latest history list item that contains a string.
  • !string to repeat the latest history list item with a string.
  • !number to repeat the latest history list item.