In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to save the output of commands in the Linux terminal to a file. I hope you will gain something after reading this article. Let's discuss it together.
Method 1: use redirection to save command output to a file
You can use redirection in Linux to achieve your goal. Using the redirect operator, it saves the output to a file instead of displaying it on the screen.
Redirects command output to a file, which replaces everything in the file.
> > the command output is added to the end of the existing contents of the file.
Use the standard output redirection operator > to redirect the output to a file:
Command > file.txt
If file.txt does not exist, it is created automatically. If you use > to redirect to the same file again, the file contents will be replaced with the new output.
The following example will demonstrate it better. It first saves the output of the ls-l command, and then it replaces the contents of the file with the output of the ls * .c command.
Redirect command output to a file
If you do not want to lose the contents of an existing file when saving the output of a script or command, you can use > >:
Command > > file.txt
The following example shows it better:
Redirect command output to a file
Even if the file does not exist, it is automatically created.
Warm Tip: save Linux command output and errors to a file.
If the Linux command returns an error, the error is not saved in the file. You can use 2 > & 1 to save the output of the command and the error to the same file, as follows:
Command > file.txt 2 > & 1
Typically, 0 represents standard input, 1 represents standard output, and 2 represents standard error. Here, you will redirect the standard error (2) to the same address as the standard output (1).
Method 2: use the tee command to display the output and save it to a file
By the way, have you noticed that when you send command output to a file, you can no longer see it on the terminal? Linux's tee command solves this problem.
Similar to sending water to a tee in both directions, the tee command sends the output to the terminal and to the file (or as input to another command). You can use it like this:
Command | tee file.txt
Similarly, if the file does not exist, it will be created automatically.
You can also use the tee command-an option to enter additional mode:
Command | tee-a file.txt
Let me demonstrate with some simple examples:
I used the simple Linux command in the example. But rest assured, you can also use these methods to save the output of bash scripts.
Note: avoid pipe traps when saving command output to a file
You may be familiar with pipe redirection, and you can use it to combine Linux commands, but you cannot pipe the output to a file, it shows that the output.txt command cannot be found:
This is because the pipeline redirects the output of one command to the input of another command.
After reading this article, I believe you have a certain understanding of "how to save the output of commands in the Linux terminal to a file". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.