Save data to file linux

Can linux cat command be used for writing text to file?

This doesn’t work for me, but also doesn’t throw any errors. Specifically interested in a cat -based solution (not vim/vi/emacs, etc.). All examples online show cat used in conjunction with file inputs, not raw text.

14 Answers 14

echo "Some text here." > myfile.txt 

If you need to use double quotes in your text, encompass the whole thing in single quotes. This is useful for .json and the likes, e.g. echo ‘<"info1": "123456">‘ > info.json

Sounds like you’re looking for a Here document

cat > outfile.txt some text >to save >EOF 

The > characters represent the default value of $PS2 ; they show up automatically, and are not meant to be typed. If you have a different value for $PS2 , that will show up instead.

cat > outfile.txt >Enter text >to save press ctrl-d 

I use the following code to write raw text to files, to update my CPU-settings. Hope this helps out! Script:

#!/bin/sh cat > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor  

This writes the text "performance" to the two files mentioned in the script above. This example overwrite old data in files.

This code is saved as a file (cpu_update.sh) and to make it executable run:

After that, you can run the script with:

IF you do not want to overwrite the old data in the file, switch out

cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor  
cat >> /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor  

This will append your text to the end of the file without removing what other data already is in the file.

Источник

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

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.

Источник

Читайте также:  Linux test if process is running
Оцените статью
Adblock
detector