- 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
- TechyTalk
- Thoughts on Magento, PHP, Linux and open source
- Linux system programming: Open file, read file and write file
- Configuring your environment
- open(), read() and write() system calls
- Thread: writing to a file from the command-line
- writing to a file from the command-line
- Re: writing to a file from the command-line
- Re: writing to a file from the command-line
- Re: writing to a file from the command-line
- Re: writing to a file from the command-line
- Re: writing to a file from the command-line
- Re: writing to a file from the command-line
- How to Write to a File in Bash
- How to write a file using redirection operators
- How to write a file using Heredoc
- About the author
- Sam U
- write(2) — Linux man page
- Synopsis
- Description
- Return Value
- Errors
- Conforming to
- Notes
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
Источник
TechyTalk
Thoughts on Magento, PHP, Linux and open source
Linux system programming: Open file, read file and write file
This is my first article in what I’m hoping will be a series of articles on system programming for POSIX compliant operating systems with focus on Linux. Actually I’ve touched this topic a while ago when I wrote three articles about library programming on Linux (static libraries, dynamic libraries and dynamic libraries using POSIX API). In this series my goal is to go trough basics of Linux system programming from the easiest topics like open file, read file and file write to a bit more complicated things like Berkeley sockets network programming. So lets get started with environment setup and an example of program that copies source file into destination file using POSIX API system calls to demonstrate open(), read() and write() system calls on Linux operating system.
Configuring your environment
I’ll use my trustworthy Ubuntu Linux operating system but you can actually use any POSIX compliant operating system, the only difference will probably be that you will need to configure your environment differently. What we need to begin with Linux system programming is gcc compiler with related packages and POSIX related man pages. So here’s how to install this packages on Ubuntu based operating system:
Basically that’s all you need to create serious system tools for Linux operating system. Later we will probably need some more libraries but we will install them when necessary.
open(), read() and write() system calls
If you have named this code file sp_linux_copy.c and if you want to name executable file sp_linux_copy to compile this program you would probably use something like this:
Then if your source file is named source_file.txt and if you want to name the destination file destination_file.txt you would run this program like this:
Now lets go trough the code and explain tricky parts. First thing we must do is to include necessary header files. Man page of every system call tells you what header files you need to include to be able to use this system call. Second we will define constant we will use to define size of our buffer in bytes. Smaller buffer size will make our copy process longer but it will save memory. Next we open source and destination file descriptors, source with O_RDONLY to make it read only, destination with O_WRONLY | O_CREAT to make it writable and to create destination file with 0644 file system permission flags. In case of error we use perror() man 3 perror to print relatively user friendly error message.
Now we are ready to start copy process. We run read() and write() inside loop (because source file might be bigger than our buffer) to copy from one file into another. Important to notice is that write() is using number of bytes read from source file returned by read() so it would know how much to write into destination file. If number of bytes read (ret_in) and number of bytes written (ret_out) differ this indicates error so once again we use perror() to print out error description. At the end if all went well we do cleanup by closing both file descriptors and returning 0 (EXIT_SUCCESS) to indicate that program ended without errors.
That’s it for this introductory article on Linux system programming topic. In my next article I will show you few more examples on POSIX input/output and then move on to memory management related system calls.
Источник
Thread: writing to a file from the command-line
Thread Tools
Display
writing to a file from the command-line
I have created a file called myfile.txt. So could someone tell me how to write something into the file from the command-line please
Re: writing to a file from the command-line
Re: writing to a file from the command-line
$cat some text > myfile.txt
$ cat some text >> myfile.txt .. append to file
Re: writing to a file from the command-line
$cat some text > myfile.txt
$ cat some text >> myfile.txt .. append to file
echo «some text»>>myfile.txt
cat «somefile.txt>>myfile.txt»
If you use only a single more than symbol >, it will delete the file and write ONLY what you asked it to. a double >> adds it onto the end without deleting anything.
cat is for files (It reads the file and outputs it, i.e. cat file.txt gives back the contents of file.txt)
Echo is for plain text.
Re: writing to a file from the command-line
Re: writing to a file from the command-line
Shows just how much of a noob i am. So i shall start from the beginning.
I created a text file called myfile.txt by using: touch myfile.txt
I also created a directory by using: mkdir Mytext
I moved myfile.txt into the Mytext directory by using: mv myfile.txt
/Mytext
I typed ls to make sure that myfile was inside the Mytext directory
Now while i was still in the Mytext directory i typed:
cat «Up hill and over dale»>>myfile.txt (clicked on Enter)
And was rewarded with: No such file or diectory
Would someone please tell me where i am going wrong?
Re: writing to a file from the command-line
I used: echo «Up hill and over dale»>>myfile.txt (clicked on enter)
and the used: cat myfile.txt to view the contents
Thanks for the replies everyone
Источник
How to Write to a File in Bash
There are multiple ways to read and write a file in bash. The simplest way is using operators “>” and “>>”.
- “>” operator will overwrite the existing data
- “>>” operator will append data
The general format of using redirection operators is:
Let’s understand the writing to a file procedure with an example:
How to write a file using redirection operators
As discussed above, the simple and straightforward approach of writing to a file is using redirection operators. For instance, if you want to change the text of an already existing file, then firstly create a text file by the name of “testfile.txt” and write anything in it:
Save the text file.
Type the below-mentioned command in the terminal:
Over wring may be risky; therefore, it is good practice to enable “noclobber”. Setting “noclobber” would block any overwriting to any exiting file.
But if you want to bypass “noclobber” then use the”>|” operator instead of “>”:
Or you can simply disable “noclobber”:
But this command will take away protection from all the files.
The above output is indicating that the existing text has been overwritten. Now, let’s use the “>>” operator:
“echo” is not always ideal to use since you cannot format text using it, therefore use “printf” in the place of “echo” to format the text as demonstrated in the following command:
Let’s understand the concept with a bash script example. Open Vim by typing “vim” in the terminal. If you do not have Vim editor on your device, then install it using:
Type the script:
The “cat” command is used to create and edit the files. Save the above script in Vim by switching mode after pressing the “Esc” key and then type “:w myscript.sh”. Open the terminal and run the code:
How to write a file using Heredoc
If you want to write multiple lines, then the easiest method is using “Heredoc”. Here document, also known as “Heredoc,” is a multi-purpose code block. The syntax of Heredoc is:
About the author
Sam U
I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.
Источник
write(2) — Linux man page
Synopsis
Description
The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).)
For a seekable file (i.e., one to which lseek(2) may be applied, for example, a regular file) writing takes place at the current file offset, and the file offset is incremented by the number of bytes actually written. If the file was open(2)ed with O_APPEND, the file offset is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic step.
POSIX requires that a read(2) which can be proved to occur after a write() has returned returns the new data. Note that not all file systems are POSIX conforming.
Return Value
If count is zero and fd refers to a regular file, then write() may return a failure status if one of the errors below is detected. If no errors are detected, 0 will be returned without causing any other effect. If count is zero and fd refers to a file other than a regular file, the results are not specified.
Errors
Other errors may occur, depending on the object connected to fd.
Conforming to
Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written.
Notes
If a write() is interrupted by a signal handler before any bytes are written, then the call fails with the error EINTR; if it is interrupted after at least one byte has been written, the call succeeds, and returns the number of bytes written.
Источник