|
LinuxI/O Redirection & Pipes / Filenames with Spaces, Apostrophes, etc. / Desktop Configuration
I/O REDIRECTION & PIPES STDIN refers to input (e.g. keyboard). STDOUT is the console output. STDERR are error messages. These also have number states attached: (0) < redirect STDIN (1) > redirect STDOUT (2) 2> redirect STDERR Additionally, double < , > e.g. >> << can append instead of overwriting to the output and can be used to redirect STDERR to STDOUT or vice-versa. Example: $ ls > list.txt prints output of ls command to text file. $ command 2> errors.txt print output of STDERR of 'command' to errors.txt *To redirect both STDOUT and STDERR use >& Pipes ------- Pipes work by storing data in an internal buffer. Each process reads and writes from and to it's own standard input/output. Combined with pipes, the output of one command can be the input of another: $ command1 < file.in | command2 > file.out Example: Read text file and output 10 most frequently used words: $ tr '[A-Z]' '[a-z]' | tr -cs '[a-z]' '[012*]' | sort | uniq -c | sort -nr | sed 10q When redirecting STDIN (e.g. $ cat < test.txt), the command receiving the stream of data does not necessarily know where it comes from or even the filename, only that it is receiving the data. /dev/null can be used in place of output redirection. More Examples -------------------- $ ls -tl | tail prints listing of ten oldest files in a directory $ sed 's/original_string/new_string/' < input_file > output_file string substitution $ diff <(ls somedir) <(ls anotherdir) compare contents of two directories $ ls *.txt | wc -l > count.txt count number of .txt files and save result to count.txt $ du | sort -nr print largest to smallest directories
|