Bài giảng Quản trị Linux: I/O redirection gồm có những nội dung chính sau: Simple redirections, advanced redirection features, filters, summary. Mời các bạn cùng tham khảo để biết thêm các nội dung chi tiết.
Trang 1Đặng Thanh Bình
I/O Redirection
Trang 3SIMPLE REDIRECTIONS
Trang 4Standard Input and Standard Output
• The keyboard is your standard input (stdin)device, and the screen or a particular terminalwindow is the standard output (stdout) device
• These default settings don't necessarily have to
be applied
• The standard output, for example, on a heavilymonitored server in a large environment may be
a printer
Trang 5The Redirection Operators
• Output redirection with > and |
– Sends the standard output of one command to another command as standard input.
Trang 6The Redirection Operators
• Output redirection with > and |
– Truncating
Trang 7The Redirection Operators
• Output redirection with > and |
– Create a new empty file with the given name
Trang 8The Redirection Operators
• Output redirection with > and |
– 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
Trang 9The Redirection Operators
• Input redirection using the < operator
– Sending a file to somebody
– Similar to
Trang 10The Redirection Operators
Trang 11The Redirection Operators
• Combining redirections
– Redirect the output to a file
Trang 12The Redirection Operators
• The >> operator
– Instead of overwriting file data, you can also append text to an existing file using >>
Trang 13ADVANCED REDIRECTION FEATURES
Trang 14Use Of File Descriptors
• There are three types of I/O, which each havetheir own identifier, called a file descriptor:
– Standard input: 0
– Standard output: 1
– Standard error: 2
Trang 15Use Of File Descriptors
• If the file descriptor number is omitted, and thefirst character of the redirection operator is <, theredirection refers to the standard input (filedescriptor 0)
• If the first character of the redirection operator is
>, the redirection refers to the standard output(file descriptor 1)
Trang 16Use 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
Trang 17Use Of File Descriptors
• The > sign should not be separated by spacesfrom the number of the file descriptor
– If it would be separated, we would be pointing the output to a file again
Trang 18• Analyzing errors
• Separating standard output from standard error
– Constructs like these are often used by programmers,
so that output is displayed in one terminal window, and errors in another
Trang 19• Writing to output and files simultaneously
– 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
Trang 20• Writing to output and files simultaneously
Trang 21FILTERS
Trang 22• When a program performs operations on inputand writes the result to the standard output, it iscalled a filter.
• One of the most common uses of filters is torestructure output
Trang 23More about grep
• Scan the output line per line, searching formatching patterns
• All lines containing the pattern will be printed tostandard output
• This behavior can be reversed using the -v option
• Recursive grep that searches all subdirectories ofencountered directories using the -r option
Trang 24Filtering Output
• sort arranges lines in alphabetical order
• Directory content is sorted smallest files first,biggest files last
Trang 25More about sort
• Use -k for sorting according to column (kolumn)
– Sort by column 3:
sort -k3 filename.txt
• By default sort will take space/tabs as fieldseparators But in /etc/passwd file the fieldseparator is : so we have to mention this onewhen sorting a file using –t option
sort -t: -k6 /etc/passwd
Trang 26More about sort
• Sort according to number, use -n option
– 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
Trang 27More about sort
• Sort the file and display only unique values
sort -u filename
• Sort a file according to some requirements andsave 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