Redirection in Linux using <,> and »
In Linux there are three data streams from which we get and send data, they are
- Standard input which is keyboard (STDIN)
- Standard output which is display (STDOUT)
- Standard Error which is displayed on STDOUT only
We can use redirection operators to redirect standard output to files. Similary we can also change the standard input to file so the input will be take from the files
Lets see redirections with examples
We use ls to display files which are by default displayed on the screen, using > redirection operator we then redirect the output to file files_in_videmo
. So insted of displaying it on screen its transfered to file.
Lets see now another example when we take input from the file and not from keyboard.
wc command is best example where we can give input from the file
Above command displays the line numbers from the files_in_videmo file
You can use » operator to append the text in output redirection. > will override file
Lets now see how to redirect the standard error from display screen to the file
In many cases we get error messages on the screen and they are annoying so we can redirect them to the file or log or many to black hole where they are iqnored and absorbed.
In above example we get error /de file or director cannot access, we use file descriptors number 2 to redirect error to the erromsg file and then wehen we display we get error message is got displayed in the file.
Please note when ther is no error, file wont have anything and it will be overwritten each time you run the command as shown below
- 0 file descriptor is used for input
- 1 is used for output
- 2 is file descriptor for the error
We can also use these numbers to redirect as shown below
Pipe is Linux
Pipe represented using |
symbol is used to give the output of one command to give as input to next command. Its one of the very powefull feature in the Linux command line
lets see with example
Using pipe we passed the output of ls to grep which will search for the file names which contains file in it and displays on the screen
Lets se more advanced example using tee command
Here we pass out put of ls command to grep for pattern search and then redirect output to to standard output and also to file.
tee command is used to pass output to standard display and to file at once.
Interesting examples of piping and redirection
Find ip addresses of starting with 192
Above command will count the number of files in a given directory, to count recursively use -R option
Sorting files with size
In above example
- du will estimate file space usage
- sort will sort out the output of du command
- head will only show top 20 largest file in ~/videmo/