Bài giảng Quản trị Linux - Chủ đề 6: I/O Redirection

pdf 27 trang phuongnguyen 3470
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:

  • pdfbai_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

  1. Đặng Thanh Bình I/O Redirection
  2. Contents • Simple redirections • Advanced redirection features • Filters • Summary
  3. SIMPLE REDIRECTIONS
  4. 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.
  5. The Redirection Operators • Output redirection with > and | – Sends the standard output of one command to another command as standard input.
  6. The Redirection Operators • Output redirection with > and | – Truncating
  7. The Redirection Operators • Output redirection with > and | – Create a new empty file with the given name
  8. 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
  9. The Redirection Operators • Input redirection using the < operator – Sending a file to somebody – Similar to
  10. 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
  11. The Redirection Operators • Combining redirections – Redirect the output to a file
  12. The Redirection Operators • The >> operator – Instead of overwriting file data, you can also append text to an existing file using >>
  13. ADVANCED REDIRECTION FEATURES
  14. 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
  15. 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)
  16. 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.
  17. 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
  18. 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
  19. 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
  20. Examples • Writing to output and files simultaneously
  21. FILTERS
  22. 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.
  23. 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
  24. Filtering Output • sort arranges lines in alphabetical order • Directory content is sorted smallest files first, biggest files last
  25. 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
  26. 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
  27. 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