Redirection
Based on Digital Ocean's intro to Linux I/O Redirection.
Streams are numbered:
- stdin (0) < or <<
- stdout (1) > or >>
- stderr (2) 2> or 2>>
- both stdout and stderr &>
Redirection makes both streams point to the same place.
<<, >> and 2>> append, <, > and 2> overwrite.
Targetting other streams.
If you want to target one stream to another instead of a file, refer to it using &
and the number of the stream.
e.g. 2>&1
redirects stderr to stdout, and >&2
redirects stout to stderr.
If using a file redirect as well, these stream redirects must come first, e.g. 2> err.log >&2
.
I'm not sure what the name of this bit is.
Not clear if you can redirect things to &0
, or why you'd want to.
Special files
/dev/null is discard
Process Substitution
This creates a pipe.
## Command inside the <() goes to stdin wc <(cat /usr/share/dict/linux.words) ## Command inside the >() gets a stream which would have been written to a file otherwise. >()
Pipes
a | b
sends output from a to input of b.
Tee
The tee
command sends its output to a file as well as the output stream.
Named Pipes
Use the mkinfo
command to create named pipes.
They make nice temporary files, but remember to rm them after.
Process substitution is usually better.
xargs
Takes some standard input and turns it into command arguments. xargs behaves a little like backquote or the $(some-command)
expansion.
echo some inputs | xargs some-program
xargs breaks input on spaces or newlines. You can use the xargs -0
on null characters instead, or xargs --delimiter=some-char
to specific a character. xargs doesn't support multibyte (for example, unicode) characters.
You can also read your inputs from a file directly:
xargs -a input-file some-program
The xargs -p
flag puts you into interactive mode, so you can choose whether or not to execute each command.
xargs placeholder substitution
The xargs -I placeholder
argument lets you select where in a command your inputs go. Input is no longer broken on blank spaces with this flag.
echo input text | xargs -I PLACEHOLDER echo oy PLACEHOLDER oy PLACEHOLDER savaloy
Parallel xargs
echo some inputs | xargs -P 10 some-program
This is a nice, easy way to run some commands in parallel, but xargs doesn't handle shared access to resources. This means that your standard output stream is likely to get out of order.
The usual way to handle this is to have each command output to a different file using placeholder substitution, and then collect them up at the end.