Input/output redirections and pipes


Today is the day I’m diving into the docs getting ready for the RHCE exam (exam on April 20) I’m adding a RHCE category to the site. I’ll put all my study notes in there.

Feel free to drop a comment if you have any useful tip or trick !

Let’s begin with the basics..

Standard input (stdin) : 0 Standard output (stdout) : 1 Error output (stderr) : 2

: output redirection
append output | : pass the output to the next utility || : execute the next command if the previous failed && : execute the next command only if the previous has succeded

Examples :

  1. ls -l test.txt > /tmp/output.txt

If test.txt exists, the output will be written in /tmp/output.txt If test.txt doesn’t exist, the error message will be output to the screen while nothing will be written to /tmp/output.txt (but the file will be created if not existing)

  1. ls -l test.txt 2> /tmp/error_output.txt > /tmp/output.txt Now, the error message would be output to /tmp/error_output.txt if the file doesn’t exist

  2. ls -l test.txt 2> /tmp/error_output.txt >> /tmp/output.txt The error output would still go in error_output.txt while the standard output would be APPENDED to output.txt

  3. cat file1 > file2 Will copy the content of file1 to file2

  4. cat > file3 Hello world ^D

Will write “Hello world” to file3

  1. wc count_test.txt This would count the number of lines, words and bytes in test.txt (“wc < test.txt”) and output the result to count_test.txt

Several ways of counting lines etc. : $ wc test.txt 7 2 17 test.txt $ wc < test.txt 7 2 17 $ cat test.txt | wc 7 2 17

ls -l test10 2>&1 | mail -s "output in a mail" Any output in a mail

Thanks for reading this post!

Did you find an issue in this article?

- click on the following Github link
- log into Github with your account
- click on the line number containing the error
- click on the "..." button
- choose "Reference in new issue"
- add a title and your comment
- click "Submit new issue"

Your feedback is much appreciated! πŸ€œπŸΌπŸ€›πŸΌ

You can also drop me a line below!