Write to file linux command line

What’s the quickest way to add text to a file from the command line?

Occasionally I have a thought that I want to write into a file while I am at the terminal. I would want these notes all in the same file, just listed one after the other. I would also like a date / time tag on each one. Is it possible to do this without having to open the file each time? Can I just enter it into the terminal and have it appended to the file each time with a command or script? I am using GNU BASH.

9 Answers 9

Also, to write multiple lines to a file from the command line, do:

Write yourself a shell script called «n». Put this in it:

#!/bin/sh notefile=/home/me/notefile date >> $notefile emacs $notefile -f end-of-buffer 

I recommend this instead of cat >> notefile because:

  1. One day you’ll be in such a hurry that you’ll fumblefinger the >> and type > instead and blow away your file.
  2. Emacs starts in five one-hundredths of a second on my Mac Mini. It takes a tenth of a second to start on a ten year old Celeron-based system I have sitting around. If you can’t wait that long to start typing, then you’re already a machine and don’t need to take notes. 🙂

If you insist on avoiding a text editor, use a shell function:

n () < date >> /home/me/notefile; cat >> /home/me/notefile; > 

which should work in all shells claiming Bourne shell compatibility.

How does the n function write to the file? Don’t you mean something like a echo «$*» rather than UUOC? How does it incorporate the date/time tag?

Not quite useless. It reads from stdin, which is where the user will be typing their note. I did forget the datestamp, though.

You can use >> to append to a file, or use > to overwrite it.

Lots of neat bells and whistles here!

date >>filename; cat >>filename 

If you have ruby installed on your computer you can use https://github.com/minhajuddin/taskr . This way you get a nice view of your notes with tags and the hours elapsed.

Taskr screenshot

Depending on your needs, syslogd might be another tool to peruse. The command

theuser@tetrad:~$ logger This message is sent to syslog 

will log the message with the facility user.notice. With many Linux systems this will be enough to have a file /var/log/user.log opened and appended to, with others you may need to define a handling for that facility and log level (or, whichever facility you choose — the local0 to local7 facilities are usually free to assign to things like this.

It’s got the benefit of being able (aka configurable) to send notes from client machines to a central logging server, something I like to use for keeping track of administrative action since it preserves timestamp, user and host information automagically, while keeping actions in order.

Читайте также:  Run linux on android device

resulting output in local file:

theuser@tetrad:~$ tail /var/log/user.log Jan 31 07:18:37 tetrad theuser: This message is sent to syslog 

Example for syslog configuration line on Solaris:

local5.notice ifdef(`LOGHOST', /var/log/diary, @loghost) 

Note: The ifdef is preprocessed with m4 , on the machine with the hostname/hostalias «loghost», the messages will be logged to the file /var/log/diary, on all others, they will be sent to the remote syslog service at loghost. To test this kind of configuration, the config file can be sent through m4 for expansion (leave away the -D LOGHOST to see how it would look on a system not called loghost:

theuser@solstice$ /usr/ccs/bin/m4 -D LOGHOST /etc/syslog.conf 

Источник

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:

Читайте также:  Найти ip адрес linux

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.

Читайте также:  Linux socket close connection

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.

Источник

Оцените статью
Adblock
detector