13 - Linux I/O Redirection

What is I/O Redirection ?

Is the manner we can manipulate the INPUT/OUPUT of a command.Every correct command will have an output so with redirection we can send the output to file/files or even to the input of another command.

Standard Output

Most command will have their outputs displayed on the terminal prompt.So to redirect the output content to a file we will use the " > " (bigger then).

Syntax:

    bash:>"command" > "file"
  

Example:

We can see that by doing the "ls" command we get a list of the files that are in the working directory.To get this output stored into a files we will use the "> " after the "ls"(list) command followed by the names of the file that will receive the data.

Note : if you repeat the command above the "redirect" file will be overwritten.

To avoid overwriting the file but still make a redirect using him we will use the " >> " command:

We can see in this example that by using the " >> " command the second output redirection was just added to the existing data in the "redirect" file.

Standard Inputs

By default standard input get their content from the keyboard , but as well it can be redirected. To redirect a standard input we will use " < " command.

Example:

So, we can see that by using the content of the file "redirect" we create input for our "sort" command.We see that the order of the content will be altered after applying the "sort" command over the "redirect"file.

Now let's see how we will redirect the output of the sorted input content into a new file called "sorted_redirect"

Well this is a bit more complex, but you need to understand this so we can continue with out tutorials.

A better description for this is this drawing:

An image can express 1000 word.

Play with the redirect commands and try to use them in all possible ways.Redirect is of great help in daily work.

Linux Pipes " | "

One of the most used tools in Linux when working with redirecting I/O.With the help of pipes the standard output of one command can become the input for another command and so on depending on the complexity of the command.

Examples of the use of Pipes " | ":

bash:> ls -l " | " less

-this command will create a scrolling output.

        bash:>ls -lt | head
      

-displays the 10 newest files in the current directory.

        bash:>du | sort -nr
       

-this command will sort the directories from the largest to the smallest.

        bash:>find . -type f -print | wc -l
      

-Displays the total number of files in the current working directory and all of its sub-directories.

        bash:>ls -l | grep "pattern"
      

- will dispay all the output lines that have "pattern" inside .

Filters in Linux

Filter are the programs that can be used with pipes to process the content information.

Some of the most used Filters are :

(i will list my favorites, and we will give examples of them in the future tutorials)

  • 1- grep -Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.
  • 2- awk -An entire programming language designed for constructing filters. Extremely powerful.
  • 3- tail -Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.
  • 4- head -Outputs the first few lines of its input. Useful for getting the header of a file.
  • 5- sort -Sorts standard input then outputs the sorted result on standard output.