Linux administration

Use Unix Streams, Pipes and Redirects

Nguyen Hai Chau
Vietnam National University

Standard Files

  • Processes are connected to three standard files

  • Many programs open other files as well

Standard Input

  • Programs can read data from their standard input file
  • Abbreviated to stdin
  • By default, this reads from the keyboard
  • Characters typed into an interactive program (e.g., a text editor) go to stdin

Standard Output

  • Programs can write data to their standard output file
  • Abbreviated to stdout
  • Used for a program's normal output
  • By default this is printed on the terminal

Standard Error

  • Programs can write data to their standard error output
  • Standard error is similar to standard output, but used for error and warning messages
  • Abbreviated to stderr
  • Useful to separate program output from any program errors
  • By default this is written to your terminal
    • So it gets 'mixed in' with the standard output

Pipes

  • A pipe channels the output of one program to the input of another
    • Allows programs to be chained together
    • Programs in the chain run concurrently
  • Use the vertical bar: |
    • Sometimes known as the 'pipe' character
  • Programs don't need to do anything special to use pipes
    • They read from stdin and write to stdout as normal
  • For example, pipe the output of echo into the program rev (which reverses each line of its input):
$ echo Happy Birthday! | rev
!yadhtriB yppaH

Connecting Programs to Files

  • Redirection connects a program to a named file
  • The < symbol indicates the file to read input from:
$ wc < thesis.txt
  • The file specified becomes the program's standard input
    • The > symbol indicates the file to write output to:
$ who > users.txt
  • The program's standard output goes into the file
  • If the file already exists, it is overwritten
    • Both can be used at the same time:
$ filter < input-file > output-file

Appending to Files

  • Use >> to append to a file:
$ date >> log.txt
  • Appends the standard output of the program to the end of an existing file
  • If the file doesn't already exist, it is created

Redirecting Multiple Files

  • Open files have numbers, called file descriptors
  • These can be used with redirection
  • The three standard files always have the same numbers:
Name File descriptor
Standard input 0
Standard output 1
Standard error 2

Redirection with File Descriptors

  • Redirection normally works with stdin and stdout
  • Specify different files by putting the file descriptor number before the redirection symbol:
    • To redirect the standard error to a file:
$ program 2> file
  • To combine standard error with standard output:
$ program > file 2>&1
  • To save both output streams:
$ program > `stdout`.txt 2> `stderr`.txt
  • The descriptors 3–9 can be connected to normal files, and are mainly used in shell scripts

Running Programs with xargs

  • xargs reads pieces of text and runs another program with them as its arguments
    • Usually its input is a list of filenames to give to a file processing program
  • Syntax: xargs command [initial args]
  • Use -l n to use n items each time the command is run
    • The default is 1
  • xargs is very often used with input piped from find
  • Example: if there are too many files in a directory to delete in one go, use xargs to delete them ten at a time:
$ find /tmp/rubbish/ | xargs -l10 rm -f

tee

  • The tee program makes a 'T-junction' in a pipeline

  • It copies data from stdin to stdout, and also to a file
  • Like > and | combined
  • For example, to save details of everyone's logins, and save Bob's logins in a separate file:
$ last | tee everyone.txt | grep bob > bob.txt

Exercise 1

  • a. Try the example on the 'Pipes' (6) slide, using rev to reverse some text.
  • b. Try replacing the echo command with some other commands which produce output (e.g., whoami).
  • c. What happens when you replace rev with cat? You might like to try running cat with no arguments and entering some text.

Exercise 2

  • a. Run the command ls --color in a directory with a few files and directories. Some Linux distributions have ls set up to always use the --color option in normal circumstances, but in this case we will give it explicitly.
  • b. Try running the same command, but pipe the output into another program (e.g., cat or less). You should spot two differences in the output. ls detects whether its output is going straight to a terminal (to be viewed by a human directly) or into a pipe (to be read by another program).