How do I write the output into the file in Linux bash shell? Are you able to clarify I/O redirection for each bash and POSIX shell to jot down knowledge to the file below Unix or Linux?
Whenever you kind a command on the shell immediate, it shows output on display or terminal. Nonetheless, bash lets you redirect and write the output into the file in Linux or Unix-like programs. That is helpful for processing or saves the terminal output to a file for different functions.
ADVERTISEMENTS
How do I save terminal output to a file?
A command can obtain enter from a file and ship output to a file.
Writing the output into the file
The syntax is
command > filename
For instance, ship output of the ls command to file named foo.txt
$ ls > foo.txt
View foo.txt utilizing the cat command:
$ cat foo.txt
Please notice that whenever you kind ‘ls > foo.txt’, shell redirects the output of the ls command to a file named foo.txt, changing the prevailing contents of the file. In different phrases, the contents of the file shall be overwritten.
Appending the output or knowledge to the file
The syntax is
command >> filename
For instance the next will append knowledge:
echo “————–” >> /tmp/knowledge.txt echo “Area info” >> /tmp/knowledge.txt host www.cyberciti.biz >> /tmp/knowledge.txt echo “————–” >> /tmp/knowledge.txt |
Confirm it:
cat /tmp/knowledge.txt
The best way to save the output of a command to a file in bash utilizing tee command
The tee command learn from customary enter and write to plain output and information. The syntax is as follows for writing knowledge into the file:
command | tee file.txt
Wish to append knowledge? Attempt
command | tee -a output.txt
Examples
Show output of the date command on display and save to the file named /tmp/output.txt. If the output.txt already exists, it will get overwritten:
$ date | tee /tmp/output.txt
$ cat /tmp/output.txt
Identical as above however append to the given information, don’t overwrite file:
$ pwd | tee -a /tmp/take a look at.txt
$ echo “At this time is $(date)” | tee -a /tmp/take a look at.txt
$ hostnamectl | tee -a /tmp/take a look at.txt
$ cat /tmp/take a look at.txt
The above instructions will append the output to the tip of the file, similar to the shell >> operator as defined earlier.
I/O redirection abstract for bash and POSIX shell
Shell operator | Description | Overwrite current file? |
---|---|---|
command > output.txt | Save terminal output (customary output) to a file named output.txt | Sure |
command >> output.txt | Append terminal output (customary output) to a file named output.txt | No |
command | Takes customary enter from output.txt file | N/A |
command 0 | Takes customary enter from output.txt file | N/A |
command 1> output.txt | Places customary output to output.txt file | Sure |
command 1>> output.txt | Appends customary output to output.txt | No |
command 2> output.txt | Places customary error to output.txt | Sure |
command 2>> output.txt | Appends customary error to output.txt file | No |
command &> output.txt | Places each customary error and output to output.txt | Sure |
command > output.txt 2>&1 | {POSIX} Places each customary error and output to file named output.txt | Sure |
command &>> output.txt | Appends each customary error and output to file named output.txt | No |
command >> output.txt 2>&1 | {POSIX} Appends each customary error and output to file referred to as output.txt | No |
command | tee output.txt | Places customary output to output.txt whereas displaying output on display | Sure |
command | tee -a output.txt | Appends customary output to output.txt whereas displaying output on display | No |
command |& tee output.txt | Places each customary output and error to output.txt whereas displaying output on terminal | Sure |
command 2>&1 | tee output.txt | {POSIX} Places each customary output and error to file named output.txt whereas displaying output on terminal | Sure |
command |& tee -a output.txt | Append each customary output and error to file referred to as output.txt whereas displaying output on terminal | No |
command 2>&1 | tee -a output.txt | {POSIX} Append each customary output and error to file named output.txt whereas displaying output on terminal | No |
Conclusion
You discovered the way to write the output to the file in Linux or Unix-like system when utilizing bash or POSIX shell. We’ve:
- /dev/stdin (customary enter) – File descriptor Zero is duplicated.
- /dev/stdout (customary output) – File descriptor 1 is duplicated.
- /dev/stderr (customary error) – File descriptor 2 is duplicated.
See I/O redirection documentation for extra info. We are able to learn bash man web page as follows utilizing the person command:
man bash
ADVERTISEMENTS
save terminal output to file linux,linux redirect output to file and screen,how to save command line output in text file linux,bash script output to file,shell script redirect output to file,shell script output to log file,save terminal history to file,how to append output to a file in linux