- Open and write data to text file using Bash?
- 12 Answers 12
- How to Write to a File in Bash
- Writing File in Bash
- Using Directional Operators
- Using tee Command
- Using printf Command
- Using Heredoc
- Conclusion
- About the author
- Aaliyan Javaid
- How to Write Data into File in Linux
- Write Data into File using CLI (Command Line Interface)
- How to Write Data to File Using cat Command
- How to Write Data to File Using echo command
- How to Write Data to File Using the printf Command
- Write data to a file using any nano text editor
- Write Data into File using Graphical User Interface
- Conclusion
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
echo "some data for the file" >> fileName
However, echo doesn’t deal with end of line characters (EOFs) in an ideal way. So, if you’re going to append more than one line, do it with printf :
printf "some data for the file\nAnd a new line" >> fileName
The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.
the operator > redirects output to a file overwriting the file if one exists. The >> will append to an existing file.
If you need to do this with root privileges, do it this way: sudo sh -c ‘echo «some data for the file» >> fileName’
#!/bin/sh FILE="/path/to/file" /bin/cat $FILE text1 text2 # This comment will be inside of the file. The keyword EOM can be any text, but it must start the line and be alone. EOM # This will be also inside of the file, see the space in front of EOM. EOM # No comments and spaces around here, or it will not work. text4 EOM
Newbies would benefit from knowing exactly what to do with this file. How to save it, how to run it, etc.
Please explain rather than simply putting up a piece of code. It would make it so much more helpful to everyone — especially newbies.
You can redirect the output of a command to a file:
If you want to write directly the command is echo ‘text’
If you need root permission: sudo sh -c ‘cat file > /etc/init/gunicorn.conf’ (thanks to lukaserat’s comment above)
An explanation of your code would go a long way toward making it more usable to others. Even callouts of vocabulary like «here document» and «redirection» can point searchers to other resources.
Explanation is never necessary when you already know the answer. To me the above code is not helpful due to lack of details.
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.
#!/bin/bash # Open file descriptor (fd) 3 for read/write on a text file. exec 3<> poem.txt # Let's print some text to fd 3 echo "Roses are red" >&3 echo "Violets are blue" >&3 echo "Poems are cute" >&3 echo "And so are you" >&3 # Close fd 3 exec 3>&-
Then cat the file on terminal
$ cat poem.txt Roses are red Violets are blue Poems are cute And so are you
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.
How to Write to a File in Bash
Reading and writing the files is one of the common tasks while writing bash scripts. For example, saving the output of a command to a file or simply manipulating the files in bash different commands are used. There are a number of ways of writing any file in bash through terminal and if you are finding ways to write any file while scripting then read this guide.
Writing File in Bash
As mentioned above there are multiple ways of writing any file while bash scripting and below are some ways to write a file in bash:
Using Directional Operators
There are generally two directional operators one can use to write a file while bash scripting, one is single angle bracket “>” and the other is double angle bracket “>>”. The primary difference between the two is that “>” overwrites the data that was previously present in the file with the new data, whereas “>>” on the other hand just adds the new data in the file.
To use any of the directional operators below is the respective syntax that one should follow:
For instance, to add data to any file which is blank currently or to overwrite the data in any file then use the above-mentioned syntax like this:
Next, to use “>>” directional operator for adding any data in the file I have used the above-mentioned syntax like this:
Remember that the above-mentioned syntax not only writes the file but will also create a file if there isn’t any.
Using tee Command
Another way to write any file while bash scripting is by using the tee command and below is the given syntax for using this command:
For instance, if I want to add any data to any file the above-mentioned syntax can be used like this:
Remember that the above syntax used is beneficial not only for writing the data but it also can be used in case if anyone wants to overwrite the data that is currently present in the file with the new data.
So, if you want to keep the current data and want to write the file with new set of data then use -a flag with the given syntax like:
If you want to add same data to multiple files, then this command can be of great help, the tee command can be used for such purpose like this:
Another benefit of using this command is that one can edit a file that is owned by the other users with the help of using admin privileges like this:
Using printf Command
Since the above-mentioned ways do not give the liberty to user to add formatting to the data so if you want to add specific formatting to the data then printf can be used like this:
Using Heredoc
Another way to write a file in bash is by using the here document format, it’s not any sort of command but it’s more like a format one can follow to write multiple lines of data, below is the format of heredoc:
Here in the syntax cat is used to read the data and delimiter is a sort of boundary for the data; it could be a string or file name. In normal practice usually END or EOF is used as a delimiter, but it all depends on the user’s preference.
For instance, I have created a .sh file and used the format of heredoc like this:
This is a test file created using heredoc
Now to see if the data is saved properly let us run this file using:
Conclusion
Writing files while bash scripting in a Linux system is a tough job if one does not have sound knowledge of using Linux commands. This guide gives 4 ways to write any file while bash scripting that are: using directional operators, tee command, printf and heredoc.
About the author
Aaliyan Javaid
I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.
How to Write Data into File in Linux
Various Linux operating systems across the world provide several functionalities and writing data to file is one of them. The two most common operations with the file are read and write; in this article, we will discuss different ways to write data into the file on Ubuntu 20.04, which is the most popular and commonly used Linux distribution.
It is obvious that we need to store our data on the computer otherwise it gets lost, so we need to write data to a file, so it remains safe and stored. All operating systems provide ways to write data to a file. In Linux, we can also write data to files through the command line, bash scripting, and graphical user interface. If you are a new Linux user or want to learn how to write to a file in Linux, follow any of the procedures mentioned below according to your requirement
You need to first create the file in which you want to write, or the file should already exist in which you want to write.
Write Data into File using CLI (Command Line Interface)
One of the most common ways to perform functions in Linux is by writing commands in a terminal using the Command Line Interface. In this part, we will discuss how to write data to a file using the CLI using different approaches.
- Write data to a file using the “cat command.”
- Write data to a file using the “echo command.”
- Write data to a file using the “printf command.”
- Write data to file using any ” nano text editor.”
These approaches are discussed in detail below.
How to Write Data to File Using cat Command
The cat is preinstalled in new Ubuntu versions, but if you are using an older version, you will need to install it. It is used to create and write data in a text file efficiently. This will save you time by avoiding the need to open an editor, and this command is also simple. The below-mentioned command will create the “linux.txt” file if it doesn’t exist or open it to edit it if it is already present.
Two redirect symbols >> are used before the filename. A single redirect symbol > can also be used, but only if it will overwrite the content previously written in the file.
Press enter and now write the below-mentioned line in the file linux.txt.
This is linux 1
After writing the content you want to write in the file, press Ctrl+C to save the content in the file.
To check whether the content is inserted into the file “linux.txt” run the below-mentioned command:
$ cat linux.txt
How to Write Data to File Using echo command
The “echo” is like the cat command; however, it has a lot more flexibility. This command is typically used to print text to the terminal, but it can also be used to write to a file or create an empty file. It is a pre-installed command in almost all versions of Ubuntu, but if it is not installed, you need to install it.
The below-mentioned command will write the text into the linux.txt file:
$ echo 'This is linux 2' >> linux.txt
Now to check the insertion of text display the content of file “linux.txt” by below mentioned command:
$ cat linux.txt
How to Write Data to File Using the printf Command
This pre-installed command has the same functionality as that of the echo command except that it follows “C-style” rather than the Shell-Editing style. The below-mentioned command will write the text into the linux.txt file:
printf "This is linux3" >> linux.txt
Now to check the insertion of text display the content of file “linux.txt” by below mentioned command:
$ cat linux.txt
Write data to a file using any nano text editor
This is the slowest and most time-consuming way, although it can be helpful for Linux newcomers. The nano and other command-line text editors can be used to heavily modify a text file. It is preinstalled in new Ubuntu versions but if you are using an older version you need to install it.
The below-mentioned command will open the text file “linux.txt”:
$ nano linux.txt
Now enter the text you want to write in the file, then press “Ctrl+S” to save the file, “Ctrl+X” to exit, and press “Y” to confirm yes.
Write Data into File using Graphical User Interface
Linux also provides a way to insert text into a file using a graphical user interface in the same way we did in Windows.
Firstly, open the file in which you want to enter the text.
Now write the text into the opened file and then click on the “Save” button to save the text and then close the file.
Conclusion
Files can be used to perform various functions, such as creating, writing, editing, deleting, and saving. In this article, the function of writing data to a file is discussed in detail. There are two main approaches to writing data to file; command-line interface and graphical user interface. In CLI, we discussed how to write data through “cat”, “echo,” and “printf” commands, and through the “nano” text editor.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.