Most programs need input to complete their tasks. This input could be in the form of mouse clicks on a graphical user interface that tell a program how to behave, or it could be what a user types in at the system prompt in response to questions posed by the program, and it could also be information contained in a file. Similarly, the output of a program can be sent to the screen, or to another file, or a number of other places.

On unix, commands that normally read input or write output on the terminal can also be executed with this input and/or output done to a file. This is called input (or output) redirection. This topic grounds you in the basic Linux techniques for standard output redirecting and pipes.

Output redirection with >

The "greater than" (>) character sends the output of the command to a file instead of printing it to the screen.

Example 1. Using the ll command, we will re-direct its output to the file list1 instead of printing it to the screen:
$ ll ~/homework > list1

One thing to note here is the file list1 which may not exist at the time when the command was executed, in this case the shell will create the file if it does not exist. If it exists, its previous contents will be overwritten!

Example 2. Check the content of the list1 file with the cat command:
$ cat lis1

Example 3. Creating a text file with cat and typing 3 rows of text:
$ cat > list2
    word1
    word2
    word3
Ctrl+d

The cat command keeps reading the input until end of file. Use the Ctrl+d combination to signal end of file. This is the same key combination to exit from the bash shell.

Since the cat command is short for concatenate, you can use cat to concatenate several files together for display or redirect the output to a file.

Example 4. Concatenating two files with cat:
$ cat list1 list2

Exampel 5. Concatenating two files with cat and redirecting the output to another file instead of printing it to the screen:
$ cat list1 list2 > list3
or, in our case, more simply
$ cat list* > list3
$ cat list3


Output redirection with >>

This type of redirection is similar with previus, but the >> redirection will append the output to the end of the file instead of overwriting it. Similar to the previous redirection, the >> redirection creates the file if not present, otherwise appends the output to it.

Example 6. Redirect the output of the ll command to the file list4:
$ ll ~/ >> list4
$ cat list4

Example 7. Append the content of the file list2 to the file list4:
$ cat list2 >> list4
$ cat list4

Example 8. Append to the list4 file a particular text:
$ echo "Type here your text" >> list4
$ cat list4

Example 9. Append a text with new lines:
$ echo -e "/n Type your text here \n Type another text" >> list4
$ cat list4


Creating pipes

So far so good. Now lets take a look at another mechanism - sending data from one command to another. It's called the pipe mechanism and the operator it uses is |. Commands separated by '|' characters are connected together by the shell and the standard output of each is run into the standard input of the next. The leftmost command in a pipeline will normally take its standard input from the terminal and the rightmost will place its standard output on the terminal.

Example 10. View the content of the /etc directory with less:
$ ll /etc | less

Example 11. List only the first 3 files from the ll command:
$ ll ~/ | head -3

Example 12. Display only the first 15 lines of the file list4:
$ cat list4 | head -15

Example 13. View the 5th line of the file list4:
$ cat list4 | head -5 | tail -1

Example 14. View the lines 11-20 of the file list4:
$ cat list4 | head -20 | tail -10

Example 15. List all .cpp files from the user home directory that have been modified in March and redirect the output to the file list5:
$ ll ~/*.cpp |grep "Mar" > list5
$ cat list5

Example 16. Delete all files list*:
$ ls ~/ | grep list | xargs rm -f
or
$ ls ~/list* | xargs rm -f$ logout


Summary:
>       Save output to a file.
>>    Append output to a file.
|        Send the output from one program as input to another program.

Zuletzt geändert: Dienstag, 2. Mai 2023, 17:12