5.6 I/O Redirection
I/O Redirection
I/O Redirection - Input/Output redirection is an important feature used by command line programs.
By default commands print their output on display. By using I/O redirection feature output can be transferred to files, devices, as input to another command.
Terminal = Keyboard + Display / Monitor
Display = used to see the command output and error messages
Keyboard = used to input commands
Shell associates three files with the terminal. These files are streams of characters.
- Standard Input - input stream representing input, connected to keyboard
- Standard Output - output stream representing output, connected to display/screen
- Standard Error - output stream representing error messages, connected to display
Each file is represented by a number called file descriptor.
A File is opened using its pathname but subsequent operations are performed using file descriptor.
Kernel maintains the table of file descriptors. Above three files have following file descriptor -
0 - Standard Input
1 - Standard Output
2 - Standard Error
Standard Input - It can represent three input sources.
- The keyboard, default input source
- A file using redirection with < symbol
- Output of another program using pipe
e.g. $wc
$wc < sample.txt
$ls | wc
Standard Error - Wrong command or invalid command input then error message is generated and sent to standard error stream
Standard Error cannot be redirected as standard output using > and >> but we need to use 2> symbol.
Standard Output - All commands write to standard output stream to display something.
There are three possible destinations of this standard output stream -
- Display, default output stream
- A File using the redirection symbol > and >>
- As input to another program using pipe ( | )
e.g. $wc sample.txt
$wc sample.txt > outputFile
$who | wc -l
$cat *.c > all_c_progs.txt
Filters - when a command/program performs operations on input and writes the result to the standard output it is called a filter.
e.g. $bc < expressions.txt > results.txt
/dev/null - If you want to execute a command and don't want to see its output on display then you can redirect the output to a special file /dev/null.
It is a special file that can accept any stream without growing in size. It's size is always zero.
/dev/tty - This file indicates one's terminal.
In a shell script if you want to see the output on display you can redirect the output explicitly to /dev/tty.
Most commands send their results to standard output. By default Standard output directs its content to display.
">" character is used to redirect the standard output to a file.
$ ls > file1.txt
after executing the above command the output of the command is saved / written into a file named file1.txt and not displayed on display. ">" overwrites the existing content of the file.
">>" character is used to redirect the standard output to file but here results will be appended to the existing file if file already exists.
$ls > file2.txt
Standard Input -
कई commands standard input के माध्यम से input ग्रहण करते है | By default standard input की-बोर्ड से डाटा ग्रहण करता है |
इसे कीबोर्ड के जगह किसी फाइल से '<' operator की सहायता से redirect किया जा सकता है -
$sort < file1.txt
Pipelines
Unix में कई commands को pipeline '|' की सहायता से आपस में जोड़ा जा सकता है |
जब दो commands को pipeline से जोड़ा जाता है तो एक कमांड का standard output दुसरे command के standard input में भेजा जाता है |
$ls -l | less
$ls -lt | head
displays the ten newest files in the current directory
$du | sort -nr
displays the list of directories and space they consume, sorted from largest to small. du - disk usage
$find . -type f -print | wc -l
Displays the total number of files in the current working directory and all its sub-directories.
Filters
Filter एक प्रकार का command है जिसे pipeline के साथ प्रयोग किया जाता है |
Filter Commands -
sort - यह standard input से डाटा input लेता है तथा उसे sort (क्रम में जमाना) करने के बाद results (sorted data) को standard output को भेजता है |
uniq - यह command standard input से प्राप्त डाटा में से duplicate lines को हटाता है |
grep - यह कमांड standard input से प्राप्त डाटा में से दिए गए pattern को search करता है तथा उन्ही lines को standard output पर भेजता है जिनमें की वह pattern मिलता है |
fmt - यह कमांड standard input से डाटा ग्रहण करने के बाद उसमें आवश्यक बदलाव कर (format करना) standard output पर भेजता है |
pr - यह कमांड standard input से डाटा ग्रहण करने के बाद data को pages में split करता है ताकि उसे प्रिंटिंग के लिए भेजा जा सके | (page break, header, footer)
head - यह कमांड प्राप्त input की पहली कुछ lines को output में दर्शाता है | यह file header को देखने के लिए उपयोग में आता है |
tail - यह कमांड प्राप्त input की अंतिम कुछ lines को output में दर्शाता है | यह log file में नयी entry को देखने के लिए उपयोग में आता है |
tr - यह characters (अक्षरों) को translate करता है | lower case / upper case / DOS file - Unix file
sed - stream editor - इसका उपयोग text translation में किया जाता है |
awk - यह एक प्रकार की संपूर्ण भाषा है जिसकी सहायता से filters को बनाया जा सकता है |