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:
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.
-displays the 10 newest files in the current directory.
-this command will sort the directories from the largest to the smallest.
-Displays the total number of files in the current working directory and all of its sub-directories.
- 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)