Day 13 of my ALX Software Engineering Journey: Input / Output Redirection

The output of commands can be redirected to files, devices and input of commands. The standard output in shell is the display. The output of commands is sent to the standard output.

Symbols and Commands used in Input/Output Redirection

The symbol to redirect a standard file output is '>'.

In the above image, the list of folders in the directory is listed with the ls command.

The output of the ls command is then redirected into file.txt with the command ls > file.txt


To append a standard file output we use the symbol '>>'. For example ls >> file.txt is used to append the output of ls to a file file.txt

file.txt already contains the output of the ls command, I'll create a new file and append the output of ls to file.txt. The output of ls should contain the new file new_file.


The output of an undirected command can be redirected to a file e.g: ls<file.txt>filelist.txt

The above command when typed, sends the output of ls<file.txt to the display but redirects it to filelist.txt.

Hence input and output can be redirected.


Pipelines

These are symbols that aid input and output redirection. With pipelines the output of a command can be redirected into the input of another command

E.g:ls | less

Above the output of ls is passed to less

Pipelines permit command combination.

du

Is used to estimate the file space used up by files or directories.


Filters

When used with pipelines, they take the standard input and perform operations and send the result to the standard output.

Some commands that work as filters include:

Sort:

sorts lines of text files as a filter, sorts an input and sends the sorted result to the standard output.

sort, when typed without an option, filters text based on capital letters, when the capital letters are to be ignored and data is to be arranged in alphabetical order the -f option is used.

sort -f [name of file]

Uniq

Removes duplicate lines in a file. It removes duplicate lines of data when a sorted stream of data is passed to it.

In the above image, the duplicate lines were removed when the result of the cat command was piped to the uniq command.

Grep

Prints lines that match patterns. It can be used to search for a particular text in a file. It's that powerful.

The above image illustrates the use of grep to find a file betty in the bin directory. The content of the bin directory is first listed with the ls command then the result is piped into the grep command. Grep examines a line of data that contains a specified pattern of characters.

Fmt

Simple optimal text formatted. As a filter, it reads text from a standard input and outputs formatted texts to a standard output.

Pr

Converts text files for printing.

As a filter, it takes text input and splits data into pages with page breaks, header and footer in preparation for printing.

Head

Outputs the first part of a file.

Below is the content of file using the cat command:

Using the head command the first 10 lines of file will be displayed

It outputs the first 10 lines of the file.

Tail

Outputs the last 10 lines of a file.

It filtered the last 10 lines as output.

Tr

Translates or deletes characters.

In the image below the tr command translates all lowercase letters to uppercase.

sed

Stream editor for filtering and transforming text.

awk

A programming language designed for constructing filters. It's a very important tool for selecting data from files.

In the above photo, the awk command was used to print the second column in the filelist.txt.

Special characters

These are characters that carry out a special instruction or have an alternative meaning, they are also known as metacharacters.

" ": is used as a tab, new line, vertical tab, form feed carriage return or space.

$: Expansion: introduces various types of expansions.

\: is used to prevent the next character from being interpreted as a special character.

‘_’: prevent the text inside from being split into multiple words or arguments.

#: is used to add commentary that extends to the end of the line.

'=': assigns value to a variable.

Summary
Input and output redirection remains a solid concept that requires mastery. With continuous practice comes mastery.

Did you find this article valuable?

Support Chisom Dillibe Okorie by becoming a sponsor. Any amount is appreciated!