linux 命令shell编程I/O 重定向

原文:http://linuxcommand.org/lts0060.php#standard_output

本文系翻译,不知道有没有人翻译过,就算有也没事,反正没人看我博客,哈哈



I/O Redirection

In this lesson, we will explore a powerful feature used by many command line programs called input/output redirection. As we have seen, many commands such as ls print their output on the display. This does not have to be the case, however. By using some special notation we can redirect the output of many commands to files, devices, and even to the input of other commands.


这节课,我们学习输入输出重定向这个神奇的东东。看看,像ls这样的命令,他能打印文件的列表,实际上这也不是个例,很多命令都可以将文件啦,设备啦甚至输入的命令给重定向到输出上面来。

Standard Output

Standard Output

Standard Output

Most command line programs that display their results do so by sending their results to a facility called standard output. By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used like this:

像下面一样,把一个命令结果重定向到文件里面

[me@linuxbox me]$ ls > file_list.txt

In this example, the ls command is executed and the results are written in a file named file_list.txt. Since the output of ls was redirected to the file, no results appear on the display.

Each time the command above is repeated, file_list.txt is overwritten (from the beginning) with the output of the command ls. If you want the new results to be appended to the file instead, use ">>" like this:

[me@linuxbox me]$ ls >> file_list.txt

When the results are appended, the new results are added to the end of the file, thus making the file longer each time the command is repeated. If the file does not exist when you attempt to append the redirected output, the file will be created.

Standard Input

Standard Input

Many commands can accept input from a facility called standard input. By default, standard input gets its contents from the keyboard, but like standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the "<" character is used like this:

[me@linuxbox me]$ sort < file_list.txt

In the above example we used the sort command to process the contents of file_list.txt. The results are output on the display since the standard output is not redirected in this example. We could redirect standard output to another file like this:

[me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt

As you can see, a command can have both its input and output redirected. Be aware that the order of the redirection does not matter. The only requirement is that the redirection operators (the "<" and ">") must appear after the other options and arguments in the command.

Pipes

Pipes

By far, the most useful and powerful thing you can do with I/O redirection is to connect multiple commands together with what are calledpipes. With pipes, the standard output of one command is fed into the standard input of another. Here is my absolute favorite:

[me@linuxbox me]$ ls -l | less

In this example, the output of the ls command is fed into less. By using this "| less" trick, you can make any command have scrolling output. I use this technique all the time.

By connecting commands together, you can acomplish amazing feats. Here are some examples you'll want to try:

Examples of commands used together with pipes
CommandWhat it does

ls -lt | head

Displays the 10 newest files in the current directory.

du | sort -nr

Displays a list of directories and how much space they consume, sorted from the largest to the smallest.

find . -type f -print | wc -l

Displays the total number of files in the current working directory and all of its subdirectories.

Filters

Filters

One class of programs you can use with pipes is called filters. Filters take standard input and perform an operation upon it and send the results to standard output. In this way, they can be used to process information in powerful ways. Here are some of the common programs that can act as filters:

Common filter commands
ProgramWhat it does

sort

Sorts standard input then outputs the sorted result on standard output.

uniq

Given a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique).

grep

Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.

fmt

Reads text from standard input, then outputs formatted text on standard output.

pr

Takes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing.

head

Outputs the first few lines of its input. Useful for getting the header of a file.

tail

Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.

tr

Translates characters. Can be used to perform tasks such as upper/lowercase conversions or changing line termination characters from one type to another (for example, converting DOS text files into Unix style text files).

sed

Stream editor. Can perform more sophisticated text translations than tr.

awk

An entire programming language designed for constructing filters. Extremely powerful.


原文:http://linuxcommand.org/lts0060.php#standard_output
本文系翻译,不知道有没有人翻译过,就算有也没事,反正没人看我博客,哈哈




I/O Redirection


In this lesson, we will explore a powerful feature used by many command line programs called input/output redirection. As we have seen, 
many commands such as ls print their output on the display. This does not have to be the case, however. By using some special notation
 we can redirect the output of many commands to files, devices, and even to the input of other commands.


这节课,我们学习输入输出重定向这个神奇的东东。看看,像ls这样的命令,他能打印文件的列表,实际上这也不是个例,很多命令都可以将文件啦,设备
啦甚至输入的命令给重定向到输出上面来。
Standard Output


Standard Output
Standard Output




Most command line programs that display their results do so by sending their results to a facility called standard output.
 By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used like this:
像下面一样,把一个命令结果重定向到文件里面,默认, 输出结果直接在屏幕显示。下面这个例子我们用">"表示重定向,它的意思就是把ls的结果输出给文件,那么
屏幕就不会显示咯。每一次执行这命令都会重复这样的操作,把你的输出重定向到文件里面,这时都是从文件的开头重新读取。
[me@linuxbox me]$ ls > file_list.txt
In this example, the ls command is executed and the results are written in a file named file_list.txt. Since the output of ls was redirected to
 the file, no results appear on the display.
Each time the command above is repeated, file_list.txt is overwritten (from the beginning) with the output of the command ls. If you want the new
 results to be appended to the file instead, use ">>" like this:
那么我们想读取文件末尾怎么办?下面一样,">>",表示文件追加的意思,就是说每一次重定向到文件,都是从末尾追加的,每一次执行文件,文件大小都会增加,当然
,如果没有这个文件那么,就会创建一个文件。
[me@linuxbox me]$ ls >> file_list.txt
When the results are appended, the new results are added to the end of the file, thus making the file longer each time the command is repeated. If
 the file does not exist when you attempt to append the redirected output, the file will be created.
Standard Input


Standard Input
标准输入,默认输入就是键盘啦。但是可以重定向改变啦,我们用"<"改变输入。
Many commands can accept input from a facility called standard input. By default, standard input gets its contents from the keyboard, but like
 standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the "<" character is used like this:
[me@linuxbox me]$ sort < file_list.txt
这个例子我们用'sort'来处理输入,这里就是文件作为输入
In the above example we used the sort command to process the contents of file_list.txt. The results are output on the display since the standard output 
is not redirected in this example. We could redirect standard output to another file like this:
[me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt
一个命令可以既有输入重定向也有输出重定向。它们的顺序只要遵循一个原则就是和对应的命令符合。
As you can see, a command can have both its input and output redirected. Be aware that the order of the redirection does not matter. The only requirement
 is that the redirection operators (the "<" and ">") must appear after the other options and arguments in the command.
Pipes
管道
Pipes
把很多命令利用重定向连接是不是很酷炫吊炸天呢,这便是管道了。
By far, the most useful and powerful thing you can do with I/O redirection is to connect multiple commands together with what are calledpipes. With pipes, 
the standard output of one command is fed into the standard input of another. Here is my absolute favorite:
[me@linuxbox me]$ ls -l | less
In this example, the output of the ls command is fed into less. By using this "| less" trick, you can make any command have scrolling output. I use this
 technique all the time.
就是这样,把输出结果利用less实现滚动效果,方便浏览,我经常使用它。
By connecting commands together, you can acomplish amazing feats. Here are some examples you'll want to try:
你还可以试一下下面的。
Examples of commands used together with pipes
Command What it does
ls -lt | head
Displays the 10 newest files in the current directory.
当前最新的10个文件
du | sort -nr
统计目录(或文件)所占磁盘空间的大小,由大到小排序
Displays a list of directories and how much space they consume, sorted from the largest to the smallest.
find . -type f -print | wc -l
Displays the total number of files in the current working directory and all of its subdirectories.
查询所有文件然后统计行数
Filters
过滤器
Filters
One class of programs you can use with pipes is called filters. Filters take standard input and perform an operation upon it and send the results to standard 
output. In this way, they can be used to process information in powerful ways. Here are some of the common programs that can act as filters:
管道中一种编程就是过滤器,把输入的文件提取有效部分转变为输出就可以了。
Common filter commands
Program What it does
sort 排序
Sorts standard input then outputs the sorted result on standard output.
uniq 剔除重复
Given a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique).
grep 检索
Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.
fmt 格式化
Reads text from standard input, then outputs formatted text on standard output.
pr 分页
Takes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing.
head  头
Outputs the first few lines of its input. Useful for getting the header of a file.
tail 末尾的
Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.
tr 转化,大小写啊,dos转unix字符啊
Translates characters. Can be used to perform tasks such as upper/lowercase conversions or changing line termination characters from one type to another 
(for example, converting DOS text files into Unix style text files).
sed 


Stream editor. Can perform more sophisticated text translations than tr.
awk
An entire programming language designed for constructing filters. Extremely powerful.


Performing tasks with pipes


Printing from the command line. Linux provides a program called lpr that accepts standard input and sends it to the printer. It is often used with pipes and 
filters. Here are a couple of examples:


cat poorly_formatted_report.txt | fmt | pr | lpr


cat unsorted_list_with_dupes.txt | sort | uniq | pr | lpr


In the first example, we use cat to read the file and output it to standard output, which is piped into the standard input of fmt. fmt formats the text into
 neat paragraphs and outputs it to standard output, which is piped into the standard input of pr. prsplits the text neatly into pages and outputs it to standard
 output, which is piped into the standard input of lpr. lpr takes its standard input and sends it to the printer.






把文件读取结果给fmt,格式化整洁的格式又重定向给pr分页好再显示第一页










The second example starts with an unsorted list of data with duplicate entries. First, cat sends the list into sort which sorts it and feeds it into uniq which
 removes any duplicates. Next pr and lpr are used to paginate and print the list.


把文件排序好,除重,分页,显示第一页

Viewing the contents of tar files Often you will see software distributed as a gzipped tar file. This is a traditional Unix style tape archive file (created with
 tar) that has been compressed with gzip. You can recognize these files by their traditional file extensions, ".tar.gz" or ".tgz". You can use the following command
to view the directory of such a file on a Linux system:
tar tzvf name_of_file.tar.gz | less

经常看到tar压缩的文件,解压然后看看里面有什么






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值