Đặng Thanh Bình

I/O Redirection

Contents

• Simple redirections • Advanced redirection features • Filters • Summary

SIMPLE REDIRECTIONS

Standard Input and Standard Output

• The keyboard is your standard input (stdin) device, and the screen or a particular terminal window is the standard output (stdout) device. • These default settings don't necessarily have to

be applied

• The standard output, for example, on a heavily monitored server in a large environment may be a printer.

The Redirection Operators

– Sends the standard output of one command to

another command as standard input.

• Output redirection with > and |

The Redirection Operators

– Truncating

• Output redirection with > and |

The Redirection Operators

– Create a new empty file with the given name

• Output redirection with > and |

The Redirection Operators

– To find a word within some text, display all

lines matching "pattern1", and exclude lines also matching "pattern2" from being displayed

– To display output of a directory listing one page at a

time

– To find a file in a directory

• Output redirection with > and |

The Redirection Operators

– Sending a file to somebody

– Similar to

• Input redirection using the < operator

The Redirection Operators

– The file text.txt is first checked for spelling mistakes,

and the output is redirected to an error log file

– List all commands that you can issue to examine

another file when using less

• Combining redirections

The Redirection Operators

– Redirect the output to a file

• Combining redirections

The Redirection Operators

– Instead of overwriting file data, you can also append

text to an existing file using >>

• The >> operator

ADVANCED REDIRECTION FEATURES

Use Of File Descriptors

• There are three types of I/O, which each have

their own identifier, called a file descriptor: – Standard input: 0 – Standard output: 1 – Standard error: 2

Use Of File Descriptors

• If the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0)

• If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1)

Use Of File Descriptors

• Direct both standard output and standard error

to the file dirlist

• The ampersand & serves as an indication that the number that follows is not a file name, but rather a location that the data stream is pointed to.

Use Of File Descriptors

• The > sign should not be separated by spaces

from the number of the file descriptor – If it would be separated, we would be pointing the

output to a file again

Examples

• Analyzing errors

– Constructs like these are often used by programmers, so that output is displayed in one terminal window, and errors in another

• Separating standard output from standard error

Examples

– tee command: copy input to standard output and one

or more output files in one move

– Using the -a option to tee results in appending input

to the file(s)

– This command is useful if you want to both see and

save output

– The > and >> operators do not allow to perform both

actions simultaneously

• Writing to output and files simultaneously

Examples

• Writing to output and files simultaneously

FILTERS

Introduction

• When a program performs operations on input and writes the result to the standard output, it is called a filter.

• One of the most common uses of filters is to

restructure output.

More about grep

• Scan the output line per line, searching for

matching patterns

• All lines containing the pattern will be printed to

standard output

• This behavior can be reversed using the -v option. • Recursive grep that searches all subdirectories of

encountered directories using the -r option

Filtering Output

• sort arranges lines in alphabetical order

• Directory content is sorted smallest files first,

biggest files last

More about sort

– Sort by column 3:

• Use -k for sorting according to column (kolumn)

sort -k3 filename.txt • By default sort will take space/tabs as field separators. But in /etc/passwd file the field separator is : so we have to mention this one when sorting a file using –t option

sort -t: -k6 /etc/passwd

More about sort

– Sort will not understand numbers by default, we have to use -n to make sure sort command understand it.

– Ex: sort /etc/passwd file according to UID

• Sort according to number, use -n option

sort -n -t: -k3 /etc/passwd – Sort will put 10 before 3 when it find this values, by

default it will sort only first numerical char

• Sort the file and reverse the order sort -r filename.txt

More about sort

• Sort the file and display only unique values

sort -u filename • Sort a file according to some requirements and

save it to a different file

sort -o temp.txt filename.txt • Sort accourding to human readable numbers

(e.g., 2K 1G)

sort -h filename.txt

• Sort according to month of the year.

sort -M filename.txt