Serverspace Black Friday
VB
July 8, 2022
Updated June 7, 2023

An Input-Output redirections in Linux

Linux

Description

Linux is “built” around the philosophy “one tool for one task”. Due to this, mainstream Linux distros has much and much utilities, each of them has just a few functions but this functions are working great.

These utilities can send it’s “job” results from one process to another via input-output redirections mechanism. It could be useful in developing, services support etc.

All processes in Linux are working across 3 streams, standard input (stdin), standard output (stdout) and standard error (stderr).  These streams are having system-reserved numbers. Stdin stream has number “0”, stream “1” is used for standard output, stream “2” is uses for an errors showing.

“Input” stream receives data from the computer keyboard or from another script output. Some examples will be described further.

By default, standard output stream sends data to the system terminal too.

pic.1

Standard error stream also sends its data to the system terminal, but one difference with previous point – standard output stream shows result if command executed successfully. In case something wrong – data will be send to the standard error output.

pic.2

Streaming redirections

Described in previous paragraph system streams could redirects work results to each other, to variables, files etc. To 'forward" system streams bracket symbols are reserved.

Double brackets is "add" stream data to the end of “target” object’s exist content.

pic.3

Single bracket will "replace" target object's content with “source” stream's data.

pic.4

Pipes

“Pipe” symbol is reserved in Linux for "sending" stream from one utility to the file or another program. It seems like “program BEFORE pipe is working, then sends results to the input of utility AFTER pipe”. E.g. command history | grep touch will show user's command history to the console and “forward” it further. Then utility “grep” will filter this flow and show commands which contains word “touch” only.

pic.5

Filter utilities

Filters are the class of system utilities which usually uses “together” with pipe. The most popular filters are:

  • find – uses to find files which filenames are contains search argument
  • grep – shows text expressions which contains data sent to the utility as argument
  • tee – forwards “incoming” input to one or more destinations (other streams, files etc)
  • tr – utility for findind and replacing string content to another data
  • wc – counts how much times incoming data is containing into “target” object

Final conclusions

In summary, there are possible to say that system streams “management” is very useful skill. It would be fine for software development, server tech support or any automation scripts building.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.