Bài giảng Quản trị Linux - Chủ đề 6: I/O Redirection
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Quản trị Linux - Chủ đề 6: I/O Redirection", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Tài liệu đính kèm:
- bai_giang_quan_tri_linux_chu_de_6_io_redirection.pdf
Nội dung text: Bài giảng Quản trị Linux - Chủ đề 6: I/O Redirection
- Đặ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 • Output redirection with > and | – Sends the standard output of one command to another command as standard input.
- The Redirection Operators • Output redirection with > and | – Truncating
- The Redirection Operators • Output redirection with > and | – Create a new empty file with the given name
- The 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
- The Redirection Operators • Input redirection using the < operator – Sending a file to somebody – Similar to
- The Redirection Operators • Combining redirections – 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
- The Redirection Operators • Combining redirections – Redirect the output to a file
- The Redirection Operators • The >> operator – Instead of overwriting file data, you can also append text to an existing file using >>
- 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 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 • 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
- Examples • 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 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
- 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 • Use -k for sorting according to column (kolumn) – Sort by column 3: 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 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 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