DEV Community

Cover image for Shell, I/O Redirections and filters
Haile Melaku
Haile Melaku

Posted on

Shell, I/O Redirections and filters

Today we are going to touch on some more Linux shell topics such as i/o stream, filters and pips.


#I/O Redirection

I/O redirection is the redirecting of input and output of many commands to files, devices, and even to the input of other commands.

Streams

Input and output in the Linux environment is distributed across three streams:

  • standard input (stdin) (0)

  • standard output (stdout) (1)

  • standard error (stderr) (2)

Standard Input

The standard input moves data form user or keyboard to program.
To do this we use the notation for the input standard, < and << have different functionality one has:

Overwrite

  • < - standard input

Append

  • << - standard input

Image description

Standard Output

The standard output is the output generated by a program, it will output text directly to the terminal or a file.
To do this we use the notation for the output standard, > and >> have different functionality one has:

Overwrite

  • > - standard output

Append

  • >> - standard output

Image description

Standard Error

The standard error is s errors generated by a program that has failed in some way.
To do this we use the notation for the error standard, 2> and 2>> have different functionality one has:

Overwrite

  • 2> - standard error

Append

  • 2>> - standard error

Image description

Image description

Tip: for more info read i/o redirect


#Pipelines

Pipes are used to redirect a stream from one program to another and it connects multiple commands together.

What that mean is pips work like an assembly line in factory once one program runs and outputs then that output is moved through the pip to the second command.

We declare a pip using this command
|

Image description

What this command do is it takes the output of ls -la then display it using less.


#Filters

Filters are mostly used with pips.Filters take standard input and perform an operation upon it and send the results to standard output.

Here are some of the common programs that can act as filters:

  • find - returns files with filenames that match the argument passed to find.

  • grep - returns text that matches the string pattern passed to grep.

  • tr - finds-and-replaces one string with another.

  • wc - counts characters, lines, and words.


#Commands

Now that we seen these concepts we need to see some common Linux commands and programs we will be using in this topic.


echo

we use the command echo display a line of text and to write into a file using i/o streams.

echo [SHORT-OPTION]... [STRING]...

Image description

We use with the stream

Image description

We need special characters to string char

Image description

Tip: for more info use the command man echo


cat

we use the command cat to concatenate files and print on the standard output.

cat [OPTION]... [FILE]...


head

we use the command head to output the first part of files.

head [OPTION]... [FILE]...


tail

we use the command tail to output the last part of files.

tail [OPTION]... [FILE]...


find

we use the command find to search for files in a directory hierarchy.

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]


wc

we use the command to print newline, word, and byte counts for each file.

wc [OPTION]... [FILE]...


sort

we use the command to sort lines of text files.

sort [OPTION]... [FILE]...


uniq

we use the command to report or omit repeated lines.

uniq [OPTION]... [INPUT [OUTPUT]]


grep

we use the command to print lines that match patterns.

grep [OPTION...] PATTERNS [FILE...]


tr

we use the command to translate or delete characters.

tr [OPTION]... SET1 [SET2]


rev

we use the command to reverse lines characterwise.

rev [option] [file...]


cut

we use the command to remove sections from each line of files.

cut OPTION... [FILE]...

Top comments (0)