Learn The Basics of How Linux I/O (Input/Output) Redirection Works
Aaron Kili
For instance to append the output of top command above in the top.log file especially within a script (or on the command line), enter the line below: $ top -bn 5 »top.log Note: Using the file descriptor number, the output redirect command above is the same as: $ top -bn 5 1>top.log How To Redirect Standard Error to File in Linux To redirect standard error of a command, you need to explicitly specify the file descriptor number, 2 for the shell to understand what you are trying to do. The first is a relatively old method which works as follows: $ ls -l /root/ >ls-error.log 2>&1 The command above means the shell will first send the output of the ls command to the file ls-error.log (using >ls-error.log ), and then writes all error messages to the file descriptor 2 (standard output) which has been redirected to the file ls-error.log (using 2>&1 ). The second and direct method is: $ ls -l /root/ &>ls-error.log You can as well append standard output and standard error to a single file like so: $ ls -l /root/ &»ls-error.log How To Redirect Standard Input to File Most if not all commands get their input from standard input, and by default standard input is attached to the keyboard. To redirect standard input from a file other than the keyboard, use the “<” operator as below: $ cat <domains.list How To Redirect Standard Input/Output to File You can perform standard input, standard output redirection at the same time using sort command as below: $ sort
Tags:
- Redirection (computing)
- Standard streams
- Pipeline (Unix)
- Computing
- Digital technology
- Information Age
- Utility software
- Information technology
- Information technology management
- Software development
- Computer science
- Operating system technology
- Software
- System software
- Technology
- Computer architecture
- Computer engineering
- Computers
- Unix
- Software engineering