Commands about I/O Redirection
the commands that will show in this article:
- cat:concatenate files
- sort:sort lines of text
- uniq:report or omit repeated lines
- grep:print lines matching a pattern
- wc:print new line,word,and byte counts for each file
- head:output the first part of a file
- tail:output the last part of a file
- tee:read from standard input and write to standard output and files
eveything is a file
programs such as ls actually send their results to a special file called standard output and it’s status messages to standard error.(often it is the shell screen…)
Redirecting standard output
ls -l /usr/bin > ls-output.txt
the standard output will be made to ls-output.txt.If we want to assign the output/error,we can add the number slogan,0-input,1-output,2-error.
ls -l /usr/bin 1 > ls-output.txt
ls -l /usr/err 2 > ls-error.txt
the symbol of > will overwrite the log when standard-output/standard-error,if you want to append the log after the previous,please use >>
ls -l /usr/bin 1 >> ls-output.txt
ls -l /usr/err 2 >> ls-error.txt
redirecting standard output and error to one file
ls -l /usr/bin > ls-output.txt 2>&1
also,you can do it like this,it also set the output and error to the sanme file:
ls -l /usr/bin &>> ls-output.txt
desposing of unwanted output
sometimes we don’t need the output/error and just want to throw it,you can use:
ls -l /usr/bin 2> /dev/null
/dev/null is a system device called a bit bucket which accepts input and does nothing
some related commands
cat:reads one or more files and copies them to standard output:
cat [file...]
cat ls-output.txt
often used to display short files.also can use like this,cat many files to one standard output:
cat movie.mpeg.0* > output.txt
if you want to create a file,except touch,you also can use cat:
cat > newFile.txt
using the symbol <,we can change the source of standard input:
cat < standardInput.txt
then the standard input change to the standardInput.txt file.
pipelines
the ability of commands to read data from standard-input to output is utilized by a shell feature called pipelines,the symbol is |,the previous output can be the input of the latter,like:
ls -l /usr/bin | less
filters:filters take input,change it somehow and then output it.example:
ls /bin /usr/bin | sort | less
uniq:report/omit repeated lines,often used with sort,
ls /bin /usr/bin | sort | uniq | less
this example will remove any duplicates from the output of sort,if you want to see the list of duplicates instead,add -d option:
ls /bin /usr/bin | sort | uniq -d | less
wc:means word count,is to print line,word and byte counts:
wc output.txt
ls /bin /usr/bin | sort | uniq | wc -l
grep:print lines matching a pattern,used to find text patterns within files.like:
grep pattern [file...]
ls /bin /usr/bin | sort | uniq | grep zip
you also can add some options,-i means ignore case when performing the searching,-v tells grep to only sprint lines that do not match the pattern.
head/tail:print First/Last part of Files.default print 10 lines,you also can use -n to chose the number of lines printed:
head -n 5 output.txt
tail -n 20 output.txt
ls /usr/bin | tail -n 5
tail can use the option of -f to view files in real-time,it is useful to watch the progress of log files as they are being writen,like:
tail -f /var/log/message
tee:copy information from stdin and output to stdout and some other files.It is useful for capturing a pipeline’s contents at an intermediate stage of processing,like:
ls /usr/bin | tee ls.txt | grep -i zip