CentOS Debian Manjaro Mint openSUSE Red Hat Ubuntu

How to Write Data into File in Linux

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.

$ 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

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.

Step 1:

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.

Similar Posts