How to write the output to the Linux file

 

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

How to write the output into the file in Linux

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

How do I save terminal output to a file


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 operatorDescriptionOverwrite current file?
command > output.txtSave terminal output (customary output) to a file named output.txtSure
command >> output.txtAppend terminal output (customary output) to a file named output.txtNo
commandTakes customary enter from output.txt fileN/A
command 0Takes customary enter from output.txt fileN/A
command 1> output.txtPlaces customary output to output.txt fileSure
command 1>> output.txtAppends customary output to output.txtNo
command 2> output.txtPlaces customary error to output.txtSure
command 2>> output.txtAppends customary error to output.txt fileNo
command &> output.txtPlaces each customary error and output to output.txtSure
command > output.txt 2>&1{POSIX} Places each customary error and output to file named output.txtSure
command &>> output.txtAppends each customary error and output to file named output.txtNo
command >> output.txt 2>&1{POSIX} Appends each customary error and output to file referred to as output.txtNo
command | tee output.txtPlaces customary output to output.txt whereas displaying output on displaySure
command | tee -a output.txtAppends customary output to output.txt whereas displaying output on displayNo
command |& tee output.txtPlaces each customary output and error to output.txt whereas displaying output on terminalSure
command 2>&1 | tee output.txt{POSIX} Places each customary output and error to file named output.txt whereas displaying output on terminalSure
command |& tee -a output.txtAppend each customary output and error to file referred to as output.txt whereas displaying output on terminalNo
command 2>&1 | tee -a output.txt{POSIX} Append each customary output and error to file named output.txt whereas displaying output on terminalNo

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:

  1. /dev/stdin (customary enter) – File descriptor Zero is duplicated.
  2. /dev/stdout (customary output) – File descriptor 1 is duplicated.
  3. /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

Posted by: Vivek Gite

The creator is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the most recent tutorials on SysAdmin, Linux/Unix and open supply matters by way of RSS/XML feed or weekly e-mail publication.

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