首页 > 编程知识 正文

bash中命令的基本格式,bash命令使用方法

时间:2023-05-05 07:07:36 阅读:235717 作者:1368

bash 命令输出到文件

When you run a command at the bash prompt, it normally prints the output of that command directly to the terminal so you can read it immediately. But bash also allows you to “redirect” the output of any command, saving it to a text file so you can review the output later.

当您在bash提示符下运行命令时,通常会将该命令的输出直接打印到终端,以便您可以立即读取。 但是bash还允许您“重定向”任何命令的输出,将其保存到文本文件中,以便以后可以查看输出。

This works in bash on any operating system, from Linux and macOS to Windows 10’s Ubuntu-based bash environment.

从Linux和macOS到Windows 10的基于Ubuntu的bash环境 ,它都可以在bash上的任何操作系统上运行。

选项一:仅将输出重定向到文件 (Option One: Redirect Output to a File Only)

To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to.

要使用bash重定向,请运行命令,指定>或>>运算符,然后提供要将输出重定向到的文件的路径。

 > redirects the output of a command to a file, replacing the existing contents of the file.

>将命令的输出重定向到文件,替换文件的现有内容。

 >> redirects the output of a command to a file, appending the output to the existing contents of the file.

>>将命令的输出重定向到文件,并将输出附加到文件的现有内容。

Technically, this redirects “stdout”—the standard output, which is the screen—to a file.

从技术上讲,这会将“ stdout”(标准输出,即屏幕)重定向到文件。

Here’s a simple example. The ls command lists files and folders in the current directory. So. when you run the following command, ls will list files and folders in the current directory. But it won’t print them to the screen—it will save them to the file you specify.

这是一个简单的例子。 ls命令列出当前目录中的文件和文件夹。 所以。 当您运行以下命令时, ls将列出当前目录中的文件和文件夹。 但是它不会将它们打印到屏幕上,而是会将它们保存到您指定的文件中。

ls > /path/to/file

You don’t have to specify the path to an existing file. Specify any valid path and bash will create a file at that location.

您不必指定现有文件的路径。 指定任何有效路径,bash将在该位置创建一个文件。

If you view the contents of the file, you’ll see the ls command’s output. For example, the cat command prints the contents of a file to the terminal:

如果查看文件的内容,将看到ls命令的输出。 例如, cat命令将文件内容打印到终端:

cat /path/to/file

Remember, the >  operator replaces the existing contents of the file with the output of the command. If you want to save the output of multiple commands to a single file, you’d use the >> operator instead. For example, the following command will append system information to the file you specify:

请记住, >运算符将文件的现有内容替换为命令的输出。 如果要将多个命令的输出保存到单个文件中,请使用>>运算符。 例如,以下命令会将系统信息附加到您指定的文件中:

uname -a >> /path/to/file

If the file doesn’t already exist, bash will create the file. Otherwise, bash will leave the existing contents of the file alone and append the output to the end of the file.

如果该文件尚不存在,则bash将创建该文件。 否则,bash将保留文件的现有内容,并将输出附加到文件末尾。

When you view the contents of the file, you’ll see the results of your second command were appended to the end of the file:

当您查看文件的内容时,您会看到第二个命令的结果被附加到文件的末尾:

You can repeat this process as many times as you like to keep appending output to the end of the file.

您可以根据需要重复多次此过程,以保持将输出追加到文件末尾。

选项二:正常打印输出并将其重定向到文件 (Option Two: Print Output Normally and Redirect It to a File)

You might not like redirecting output with the > or >> operators, as you won’t see the output of the command in the terminal. That’s what the tee command is for. The tee command prints the input it receives to the screen and saves it to a file at the same time.

您可能不喜欢使用>或>>运算符重定向输出,因为您不会在终端中看到命令的输出。 这就是tee命令的用途。 tee命令将接收到的输入打印到屏幕上, 并同时将其保存到文件中。

To pipe the output of a command to tee, printing it to your screen and saving it to a file, use the following syntax:

要将命令的输出传递给tee ,将其打印到屏幕上并将其保存到文件中,请使用以下语法:

command | tee /path/to/file

This will replace anything in the file with the output of the command, just like the > operator.

就像>运算符一样,这​​将用命令的输出替换文件中的任何内容。

To pipe the output of a command to tee , printing to to your screen and saving it to a file, but appending it to the end of the file:

要将命令的输出传递给tee ,将其打印到屏幕上并将其保存到文件中,但将其附加到文件末尾:

command | tee -a /path/to/file

This will append the output to the end of the file, just like the >> operator.

就像>>运算符一样,这​​会将输出附加到文件末尾。

The bash shell includes some additional, advanced operators that perform similar functions. They’ll be particularly useful if you’re writing bash scripts. Consult the I/O Redirection chapter in the Advanced Bash-Scripting Guide for more detailed information.

bash shell包含一些执行类似功能的其他高级运算符。 如果您正在编写bash脚本,它们将特别有用。 有关更多详细信息,请查阅《高级Bash脚本指南》中的“ I / O重定向”一章。

翻译自: https://www.howtogeek.com/299219/how-to-save-the-output-of-a-command-to-a-file-in-bash-aka-the-linux-and-macos-terminal/

bash 命令输出到文件

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。