Write Data into File in Linux - Step by step guide ?

Different 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. 

Here at LinuxAPT, we shall look into how to write to a file in Linux.

However, 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.


How to 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".


1. 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:

$ cat >> linux.txt

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


2. 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


3. 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


4. 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.


5. 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.


[Need help in fixing Linux system issues ? We can help you. ]

This article covers how to write data through "cat", "echo", and "printf" commands, and through the "nano" text editor.

Related Posts