Ubuntu Tutorial

File Descriptor : in linux, "everything is a file" and file has a number called file descriptor. file descriptor allow system to access file easily. see following table.

Integer value Name
0 Standard input
1 Standard output
2 Standard error

Redirection : redirection allow to change standard input or output of command. following are redirection types.

Input Redirection : In input redirection input(standard input) is passed to command or program using < symbol. see following example, in cat < abc.txt command shell open file and write content to the standard input of another process(here cat command). ie we are passing abc.txt as input to cat command.

image
input redirection

Output Redirection : In output redirection output(except standard error) of command or file or program is redirected or passed as input to file or program instead of printing on terminal. to do this > symbol is used. see following example 2, find command will find file that has extension .js. output of find command shows some standard error as ": Permission denied". then we execute find command with > f.txt. this will store output except standard errors in f.txt and print standard errors on terminal. at the end we print f.txt file on terminal.

Note : when we store output in file if file is already created it will override that file. so select file name correctly.

if you want to append output to file instead of creating file use >> instead of >
example find . -name "*.js" >> output.txt

image
output redirection

Error Redirection : in error redirection error is redirected from terminal. following are ways to redirect error.

  • using 2 > file : this will redirect or move error to given file and print remaining output on terminal. see following example 3, find command will find file that has extension .js. output of find command shows some standard error as ": Permission denied". then we execute find command with 2>output1.txt. here 2 is standard error(file descriptor). 2>output1.txt will send only standard error of output to output1.txt file and print remaining output(excluding standard errors) on terminal. at the end we print output1.txt file that contains standard errors of find command.

    image
    error redirection using 2>file

  • using 2>1 : this will remove error from output and print remaining output on terminal. see following example 4, we list all js extension files but we get errors also. in 2>1 2 is standard error and 1 is standard output. 2>1 will redirect standard error from standard output. and also print output without standard errors. in next command we store output of 2>1 in output2.txt. at the end we print output2.txt file.

    image
    error redirection using 2>1

  • using 2>&1 : this will redirect standard output with standard error of command or program to file or program. see following example 5, we list all js extension files but we get errors. then we store output in output3.txt. while storing output in file 2>&1 will allow standard error to get store in file. and at the end we print the output3.txt file.

    image
    error redirection using 2>&1