User Tools

Site Tools


bash:redirection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
bash:redirection [2019/12/14 19:31] peterbash:redirection [2021/01/13 21:51] (current) – [BASH - Redirection] peter
Line 1: Line 1:
 ====== BASH - Redirection ====== ====== BASH - Redirection ======
 +
 +In Linux/Unix, everything is a file.  Regular file, Directories, and even Devices are files.
 +
 +Every File has an associated number called File Descriptor (FD).
 +
 +  * Your screen has a File Descriptor.  When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor.
 +  * Your printer has a File Descriptor.  If the output is sent to File Descriptor of the printer, the program output would be printed.
 +
 +----
 +
 +===== Standard File Descriptors =====
 +
 +Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error.  
 +
 +These files are always present whenever a program is run.
 +
 +
 +^File^File Descriptor^
 +|Standard Input STDIN|0|
 +|Standard Output STDOUT|1|
 +|Standard Error STDERR|2|
 +
 +
 +The keyboard is the standard input device while your screen is the standard output device.
 +
 +However, files can have their input and output redirected, using:
 +
 +  * ">" is the output redirection operator.
 +  * ">>" appends output to an existing file
 +  * "<" is the input redirection operator
 +  * ">&" re-directs output of one file to another.
 +  * Errors can be re-directed using its corresponding File Descriptor 2.
 +
 +----
  
 ===== Output Redirection ===== ===== Output Redirection =====
Line 37: Line 71:
 ===== Input redirection ===== ===== Input redirection =====
  
-The '<' symbol is used for input(STDIN) redirection.+The '<' symbol is used for input (STDIN) redirection.
  
 <code bash> <code bash>
Line 47: Line 81:
 ---- ----
  
 +===== Error Redirection =====
 +
 +Error re-direction is one of the very popular features of Unix/Linux.
 +
 +Frequent UNIX users will reckon that many commands give you massive amounts of errors.
 +
 +  * For instance, while searching for files, one typically gets permission denied errors. These errors usually do not help the person searching for a particular file.
 +  * While executing shell scripts, you often do NOT want error messages cluttering up the normal program output.
 +
 +The solution is to re-direct the error messages to a file.
 +
 +==== Example 1 ====
 +
 +<code bash>
 +myprogram 2>errorsfile
 +</code>
 +
 +Executes a program names myprogram.
 +
 +The file descriptor for standard error is 2.
 +
 +Using "2>" we re-direct the error output to a file named "errorfile"
 +
 +Thus, program output is not cluttered with errors.
 +
 +----
 +
 +==== Example 2 ====
 +
 +<code bash>
 +find . -name 'my*' 2>error.log
 +</code>
 +
 +Using the "find" command, we are searching the "." current directory for a file with "name" starting with "my".
 +
 +----
 +
 +=== Example 3 ===
 +
 +A more complex example,
 +
 +Server Administrators frequently, list directories and store both error and standard output into a file, which can be processed later.
 +
 +<code bash>
 +ls Documents ABC> dirlist 2>&1
 +</code>
 +
 +Here,
 +
 +  *  which writes the output from one file to the input of another file. 2>&1 means that STDERR redirects to the target of STDOUT (which is the file dirlist)
 +  *  We are redirecting error output to standard output which in turn is being re-directed to file dirlist. Hence, both the output is written to file dirlist
  
bash/redirection.1576351872.txt.gz · Last modified: 2020/07/15 10:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki