This note is about modifiers you can add to almost all Unix commands.
End of Options
-- can be used to signify end of options of a command. Rarely needed but useful to know.
your-command -p param -a argument -- file.txt
Continues on Next Line
You can use
\ to tell a shell the command continues on the next line.
echo 'hello world!' echo \ 'hello world!'
Run in Background
End command with
& if you want the command to run in background so allows inserting new commands while it is running.
Continue to the next command if first does not cause errors.
emerge --sync && emerge -uDNav world
Continue to the next command only if first fails.
false || echo This is shown. true || echo This is not shown.
Unix has three streams STDIN, STDOUT, STDERR. STDIN is for user input, STDOUT is standard output and STDERR is error messages. When commands take or provide text like input, you can redirect it.
Write output from a command to a file on disk with
>. Redirects STDOUT.
# Save text to demo.txt. echo -e "line1\nline2" > demo.txt # Store all process info to processes.txt. ps -ax > processes.txt
You can redirect errors by
2>. This tells command to pipe output channel 2, which is STDERR.
# Saves curl error to a file. curl fakeurl 2> errs.txt # Write valid responses to different file than errors. 11 curl google.com fakeurl 1> out1.txt 2> out2.txt # Write valid responses and errors to same file. 14 curl google.com fakeurl &> out.txt
Append output from a command to an existing file on disk with
# Store all process info to processes.txt while keeping previous info. ps -ax >> processes.txt
Read a command's input from a disk file, not from user, with
# Load data from file /var/backups/local.nidump into current NetInfo domain. niload -d -r / . < /var/backups/local.nidump
# Getting STDIN from a 10 first lines of a file. head errs.txt
Pass the output of one command to another for further processing with
# Getting STDIN from a pipe to 10 first lines of a file. cat errors.txt | head # Find processes that contain name string 'Finder' ps -ax | grep Finder # List all open files and display them one screen at a time. lsof | more
Write output to a file, and pass it to further commands in the pipeline with
tee. Name comes from T-junction.
ls | tee list.txt
tr 'A-Z' 'a-z' <fnord.txt | tr -cs 'a-z' '\n' | sort | uniq | comm -23 - /usr/share/dict/words
What this does is pass the contents of fnord.txt to the
tr command to translate uppercase letters to lowercase; the output from that is piped to another
tr command that turns everything except lowercase letters into line breaks (effectively putting each word on a separate line); that's piped to
sort which puts the lines (words) in alphabetical order; that's piped to
uniq, which gets rid of duplicate words; that's piped to
comm, which compares the words to the dictionary, and prints whatever's not in the dictionary. (Unfortunately, this tends to include plurals, past tense verbs, words that came into use after 1934).