翻译完成

This commit is contained in:
MjSeven 2020-12-07 00:06:03 +08:00
parent 02e0eae5ad
commit 6d5a5db085
2 changed files with 112 additions and 114 deletions

View File

@ -1,114 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (Mjseven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Save the Output of a Command to a File in Linux Terminal [Beginners Tip])
[#]: via: (https://itsfoss.com/save-command-output-to-file-linux/)
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
How to Save the Output of a Command to a File in Linux Terminal [Beginners Tip]
======
When you run a command or script in the Linux terminal, it prints the output on the screen for your immediate viewing.
There will be times when you need to save the output to a file for future references. Now, [you can surely copy and paste in Linux terminal][1] but there are better ways to save the output of a shell script or command in Linux command line. Let me show them to you.
### Method 1: Use redirection to save command output to file in Linux
You can [use redirection in Linux for this purpose][2]. With redirection operator, instead of showing the output on the screen, it goes to the provided file.
* The > redirects the command output to a file replacing any existing content on the file.
* The >> redirects adds the command output at the end of the existing content (if any) of the file.
Use the STDOUT redirection operator > for redirecting the output to a file like this:
```
command > file.txt
```
If the file.txt doesnt exist, it will be created automatically. If you use the > redirect again with the same file, the file content is replaced by the new output.
The example below demonstrates it better. It first saves the output of _ls -l_ command. And then later, it replaces the content of the file with the output of _ls *.c_ command.
![Redirecting command output to file][3]
If you dont want to lose the content of the existing file while saving the output of a script or command, use the redirection operation in append mode with >>.
```
command >> file.txt
```
This example demonstrates it better:
![Redirecting command output to file in append mode][4]
Even here if the file doesnt exist, it is created automatically.
Bonus Tip: Save Linux command output as well as error to a file
If your Linux command returns an error, it doesnt get saved in the file. You can save both the command output and command error in the same file using 2>&1 like this:
**command > file.txt 2>&1**
Basically, 0 stands for standard input, 1 for standard output and 2 for standard error. Here, you are redirecting (>) standard error (2) to same address (&) as standard output (1).
### Method 2: Use tee command to display the output and save it to a file as well
By the way, did you notice that when you send the command output to a file, you cannot see it anymore on the display? The [tee command in Linux][5] solves this problem for you.
Like a tee pipe that sends water stream into two directions, the tee command send the output to the display as well as to a file (or as input to another command). You can use it like this:
```
command | tee file.txt
```
Again, the file will be created automatically, if it doesnt exist already.
You may also use the tee command in append mode with option -a in this manner:
```
command | tee -a file.txt
```
Let me demonstrate it with some easy to follow examples:
![][6]
I have used simple Linux commands in my examples. But rest assured, you can use these methods to save the output of bash scripts as well.
### Note: Avoid pipe pitfall while saving command output to file
You probably are familiar with pipe redirection. You may use it to combine Linux commands but you cannot pipe the output to a file. It will result in error that filename command not found:
![][7]
This is because pipe redirects the output of one command to input of another command. And in this case, you give it a file name while it was expecting a command.
If you are new to Linux command line, I hope this quick tutorial added to your Linux knowledge a bit. [I/O redirection][8] is an essential concept that one should be aware of.
As always, questions and suggestions are always welcome.
--------------------------------------------------------------------------------
via: https://itsfoss.com/save-command-output-to-file-linux/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/abhishek/
[b]: https://github.com/lujun9972
[1]: https://itsfoss.com/copy-paste-linux-terminal/
[2]: https://linuxhandbook.com/redirection-linux/
[3]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/11/redirecting_command_output_to_file_in_linux.png?resize=741%2C456&ssl=1
[4]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/11/redirecting_command_output_to_file.png?resize=741%2C494&ssl=1
[5]: https://linuxhandbook.com/tee-command/
[6]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/display-and-save-linux-command-output.png?resize=741%2C494&ssl=1
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/pipe-output-to-file-linux.png?resize=687%2C266&ssl=1
[8]: https://tldp.org/LDP/abs/html/io-redirection.html#FTN.AEN17894

View File

@ -0,0 +1,112 @@
[#]: collector: (lujun9972)
[#]: translator: (Mjseven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Save the Output of a Command to a File in Linux Terminal [Beginners Tip])
[#]: via: (https://itsfoss.com/save-command-output-to-file-linux/)
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
如何将 Linux 终端中命令的输出保存到文件中[新手技巧]
======
当你在 Linux 终端中运行命令或脚本时,它会在终端中打印输出方便你立即查看。
有时你需要将输出保存到文件中以备将来参考。现在,[你当然可以在 Linux 终端中复制和粘贴][1],但是有更好的方法可以在 Linux 命令行中保存 shell 脚本或命令的输出,让我演示给你看。
### 方法 1使用重定向将命令输出保存到文件中
你可以[在 Linux 中使用重定向来达成目的][2]。使用重定向操作符,它会将输出保存到文件中而不是在屏幕上显示。
* > 会将命令输出重定向到文件,它会替换文件中的所有内容。
* >> 会将命令输出添加到文件现有内容的末尾。
使用 STDOUT 重定向运算符 > 将输出重定向到文件:
```
command > file.txt
```
如果 file.txt 不存在,它会自动创建。如果你使用 > 再次重定向到相同的文件,文件内容将被替换为新的输出。
下面的示例将更好地演示它。它首先会保存 _ls -l_ 命令的输出,然后,它将用 _ls *.c_ 命令的输出替换文件的内容。
![将命令输出重定向到文件][3]
如果你不想在保存脚本或命令的输出时丢失现有文件的内容,可以使用 >>
```
command >> file.txt
```
下面这个例子更好地展示了它:
![将命令输出重定向到文件][4]
即使文件不存在,它也会自动创建。
温馨提示:将 Linux 命令输出和错误保存到一个文件中。
如果Linux 命令返回错误,那么错误不会保存在文件中。你可以使用 2>&1 将命令的输出和错误保存到同一个文件中,如下所示:
**command > file.txt 2>&1**
通常0 代表标准输入1 代表标准输出2 代表标准错误。在这里,你要将标准错误(2) 重定向(&)到与标准输出(1) 相同的地址。
### 方法 2使用 tee 命令显示输出并将其保存到文件中
顺便说一句,你是否注意到,当你将命令输出发送到一个文件时,你再也无法在终端上看到它了?[Linux 的 tee 命令][5]解决了这个问题。
类似于将水流发送到两个方向的三通管tee 命令将输出发送到终端以及文件(或作为另一个命令的输入)。你可以像这样使用它:
```
command | tee file.txt
```
同样,如果该文件不存在,它将自动创建。
你还可以使用 tee 命令在附加模式下使用选项 -a
```
command | tee -a file.txt
```
让我用一些简单的例子来演示:
![][6]
我在例子中使用了简单的 Linux 命令。但是请放心,你也可以使用这些方法来保存 bash 脚本的输出。
### 注意:将命令输出保存到文件时,避免管道陷阱
你可能对管道重定向很熟悉,可以使用它来组合 Linux 命令,但不能将输出通过管道传输到文件,它显示找不到 output.txt 命令:
![][7]
这是因为管道将一个命令的输出重定向到另一个命令的输入。在本例中,你向它传递一个了一个文件名而它期望一个命令。
如果你是一个 Linux 命令行新手,我希望这个快速教程对你的 Linux 知识有所帮助。[I/O 重定向][8]是一个需要注意的基本概念。
一如既往,欢迎提出问题和建议。
--------------------------------------------------------------------------------
via: https://itsfoss.com/save-command-output-to-file-linux/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/abhishek/
[b]: https://github.com/lujun9972
[1]: https://itsfoss.com/copy-paste-linux-terminal/
[2]: https://linuxhandbook.com/redirection-linux/
[3]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/11/redirecting_command_output_to_file_in_linux.png?resize=741%2C456&ssl=1
[4]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/11/redirecting_command_output_to_file.png?resize=741%2C494&ssl=1
[5]: https://linuxhandbook.com/tee-command/
[6]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/display-and-save-linux-command-output.png?resize=741%2C494&ssl=1
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/pipe-output-to-file-linux.png?resize=687%2C266&ssl=1
[8]: https://tldp.org/LDP/abs/html/io-redirection.html#FTN.AEN17894