- How to write the output into the file in Linux
- How do I save terminal output to a file?
- Writing the output into the file
- Appending the output or data to the file
- How to save the output of a command to a file in bash using tee command
- Examples
- I/O redirection summary for bash and POSIX shell
- Conclusion
- How do I save terminal output to a file?
- 9 Answers 9
- Overview:
- What’s the quickest way to add text to a file from the command line?
- 9 Answers 9
- Open and write data to text file using Bash?
- 12 Answers 12
- Redirecting Output:
- Appending Redirected Output
- Here Documents
- Here Strings
How to write the output into the file in Linux
How do I save terminal output to a file?
A command can receive input from a file and send output to a file.
Writing the output into the file
The syntax is
command > filename
For example, send output of the ls command to file named foo.txt
$ ls > foo.txt
View foo.txt using the cat command:
$ cat foo.txt
Please note that when you type ‘ls > foo.txt’, shell redirects the output of the ls command to a file named foo.txt, replacing the existing contents of the file. In other words, the contents of the file will be overwritten.
Appending the output or data to the file
The syntax is
command >> filename
For example the following will append data:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Verify it:
cat /tmp/data.txt
How to save the output of a command to a file in bash using tee command
The tee command read from standard input and write to standard output and files. The syntax is as follows for writing data into the file:
command | tee file.txt
Want to append data? Try
command | tee -a output.txt
Examples
Display output of the date command on screen and save to the file named /tmp/output.txt. If the output.txt already exists, it gets overwritten:
$ date | tee /tmp/output.txt
$ cat /tmp/output.txt
Same as above but append to the given files, do not overwrite file:
$ pwd | tee -a /tmp/test.txt
$ echo «Today is $(date)» | tee -a /tmp/test.txt
$ hostnamectl | tee -a /tmp/test.txt
$ cat /tmp/test.txt
The above commands will append the output to the end of the file, just like the shell >> operator as explained earlier.
I/O redirection summary for bash and POSIX shell
Shell operator | Description | Overwrite existing file? |
---|---|---|
command > output.txt | Save terminal output (standard output) to a file named output.txt | Yes |
command >> output.txt | Append terminal output (standard output) to a file named output.txt | No |
command | Takes standard input from output.txt file | N/A |
command 0 | Takes standard input from output.txt file | N/A |
command 1> output.txt | Puts standard output to output.txt file | Yes |
command 1>> output.txt | Appends standard output to output.txt | No |
command 2> output.txt | Puts standard error to output.txt | Yes |
command 2>> output.txt | Appends standard error to output.txt file | No |
command &> output.txt | Puts both standard error and output to output.txt | Yes |
command > output.txt 2>&1 | <POSIX> Puts both standard error and output to file named output.txt | Yes |
command &>> output.txt | Appends both standard error and output to file named output.txt | No |
command >> output.txt 2>&1 | <POSIX> Appends both standard error and output to file called output.txt | No |
command | tee output.txt | Puts standard output to output.txt while displaying output on screen | Yes |
command | tee -a output.txt | Appends standard output to output.txt while displaying output on screen | No |
command |& tee output.txt | Puts both standard output and error to output.txt while displaying output on terminal | Yes |
command 2>&1 | tee output.txt | <POSIX> Puts both standard output and error to file named output.txt while displaying output on terminal | Yes |
command |& tee -a output.txt | Append both standard output and error to file called output.txt while displaying output on terminal | No |
command 2>&1 | tee -a output.txt | <POSIX> Append both standard output and error to file named output.txt while displaying output on terminal | No |
Conclusion
You learned how to write the output to the file in Linux or Unix-like system when using bash or POSIX shell. We have:
- /dev/stdin (standard input) — File descriptor 0 is duplicated.
- /dev/stdout (standard output) — File descriptor 1 is duplicated.
- /dev/stderr (standard error) — File descriptor 2 is duplicated.
See I/O redirection documentation for more information. We can read bash man page as follows using the man command:
man bash
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How do I save terminal output to a file?
How do I save the output of a command to a file?
Is there a way without using any software? I would like to know how.
9 Answers 9
Yes it is possible, just redirect the output (AKA stdout ) to a file:
Or if you want to append data:
If you want stderr as well use this:
or this to append:
if you want to have both stderr and output displayed on the console and in a file use this:
(If you want the output only, drop the 2 above)
To write the output of a command to a file, there are basically 10 commonly used ways.
Overview:
Please note that the n.e. in the syntax column means «not existing».
There is a way, but it’s too complicated to fit into the column. You can find a helpful link in the List section about it.
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
command 2> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.
command 2>> output.txt
The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.
Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..
command | tee output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.
command | tee -a output.txt
The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at «How to pipe stderr, and not stdout?» on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.
command |& tee output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.
command |& tee -a output.txt
Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.
Источник
What’s the quickest way to add text to a file from the command line?
Occasionally I have a thought that I want to write into a file while I am at the terminal. I would want these notes all in the same file, just listed one after the other. I would also like a date / time tag on each one.
Is it possible to do this without having to open the file each time? Can I just enter it into the terminal and have it appended to the file each time with a command or script?
I am using GNU BASH.
9 Answers 9
Write yourself a shell script called «n». Put this in it:
I recommend this instead of cat >> notefile because:
- One day you’ll be in such a hurry that you’ll fumblefinger the >> and type > instead and blow away your file.
- Emacs starts in five one-hundredths of a second on my Mac Mini. It takes a tenth of a second to start on a ten year old Celeron-based system I have sitting around. If you can’t wait that long to start typing, then you’re already a machine and don’t need to take notes. 🙂
If you insist on avoiding a text editor, use a shell function:
which should work in all shells claiming Bourne shell compatibility.
Also, to write multiple lines to a file from the command line, do:
You can use >> to append to a file, or use > to overwrite it.
Lots of neat bells and whistles here!
If you have ruby installed on your computer you can use https://github.com/minhajuddin/taskr . This way you get a nice view of your notes with tags and the hours elapsed.
Depending on your needs, syslogd might be another tool to peruse. The command
will log the message with the facility user.notice. With many Linux systems this will be enough to have a file /var/log/user.log opened and appended to, with others you may need to define a handling for that facility and log level (or, whichever facility you choose — the local0 to local7 facilities are usually free to assign to things like this.
It’s got the benefit of being able (aka configurable) to send notes from client machines to a central logging server, something I like to use for keeping track of administrative action since it preserves timestamp, user and host information automagically, while keeping actions in order.
resulting output in local file:
Example for syslog configuration line on Solaris:
Note: The ifdef is preprocessed with m4 , on the machine with the hostname/hostalias «loghost», the messages will be logged to the file /var/log/diary, on all others, they will be sent to the remote syslog service at loghost. To test this kind of configuration, the config file can be sent through m4 for expansion (leave away the -D LOGHOST to see how it would look on a system not called loghost:
Источник
Open and write data to text file using Bash?
How can I write data to a text file automatically by shell scripting in Linux?
I was able to open the file. However, I don’t know how to write data to it.
12 Answers 12
The short answer:
However, echo doesn’t deal with end of line characters (EOFs) in an ideal way. So, if you’re gonna append more than one line, do it with printf :
The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.
You can redirect the output of a command to a file:
or append to it
If you want to write directly the command is echo ‘text’
I know this is a damn old question, but as the OP is about scripting, and for the fact that google brought me here, opening file descriptors for reading and writing at the same time should also be mentioned.
Then cat the file on terminal
This example causes file poem.txt to be open for reading and writing on file descriptor 3. It also shows that *nix boxes know more fd’s then just stdin, stdout and stderr (fd 0,1,2). It actually holds a lot. Usually the max number of file descriptors the kernel can allocate can be found in /proc/sys/file-max or /proc/sys/fs/file-max but using any fd above 9 is dangerous as it could conflict with fd’s used by the shell internally. So don’t bother and only use fd’s 0-9. If you need more the 9 file descriptors in a bash script you should use a different language anyways 🙂
Anyhow, fd’s can be used in a lot of interesting ways.
I like this answer:
but would suggest cat >> FILE.txt if you want just add something to the end of the file without wiping out what is already exists
Moving my comment as an answer, as requested by @lycono
If you need to do this with root privileges, do it this way:
For environments where here documents are unavailable ( Makefile , Dockerfile , etc) you can often use printf for a reasonably legible and efficient solution.
I thought there were a few perfectly fine answers, but no concise summary of all possibilities; thus:
The core principal behind most answers here is redirection. Two are important redirection operators for writing to files:
Redirecting Output:
echo ‘text to completely overwrite contents of myfile’ > myfile
Appending Redirected Output
echo ‘text to add to end of myfile’ >> myfile
Here Documents
Others mentioned, rather than from a fixed input source like echo ‘text’ , you could also interactively write to files via a «Here Document», which are also detailed in the link to the bash manual above. Those answers, e.g.
cat > FILE.txt or cat >> FILE.txt
make use of the same redirection operators, but add another layer via «Here Documents». In the above syntax, you write to the FILE.txt via the output of cat . The writing only takes place after the interactive input is given some specific string, in this case ‘EOF’, but this could be any string, e.g.:
cat > FILE.txt or cat >> FILE.txt
would work just as well. Here Documents also look for various delimiters and other interesting parsing characters, so have a look at the docs for further info on that.
Here Strings
A bit convoluted, and more of an exercise in understanding both redirection and Here Documents syntax, but you could combine Here Document style syntax with standard redirect operators to become a Here String:
Источник