Linux append string to file

How To Use Cat Command To Append Data To a File on Linux/Unix

I am a new Unix user. I have Debian Linux installed. I need to append text to a file called daily.log. How do I use the cat command to append data to a file?

You can use the cat command to append data or text to a file. The cat command can also append binary data. The main purpose of the cat command is to display data on screen (stdout) or concatenate files under Linux or Unix like operating systems. To append a single line you can use the echo command or printf command command. Let us see how to use the cat command to append data and update files without losing its content.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements cat on Linux or Unix
Est. reading time 2 mintues

How To Use Cat Command To Append Data To a File on Linux/Unix

Redirection symbol is as follows for appending data to a file:

Syntax

Examples

Create a text file called foo.txt, type:

To save the changes press CTRL-d i.e. press and hold CTRL and press d. Create another text file called bar.txt as follows:

Display both files on the screen, enter:

To append a contains of bar.txt to to foo.txt, enter:

To append a ‘Use unix or die’ text to foo.txt file, enter:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Fig.01: Using the cat and echo command to append a text to a file.

Append text to a file when using sudo command

We can use the echo command or printf command to append data to a file called sales.txt in the current directory:

Want to append to a file? Try:
cat filename | sudo tee -a foo_file.txt
In this example append data using the following syntax:
sudo sh -c ‘echo «192.168.1.253 wireless-router» >> /etc/hosts’
Verify it:
cat /etc/hosts

Summing up

We explained various Linux and Unix commands that one could use to append data to a file. Although I tested all examples on Bash running on macOS and Linux desktop, these examples should work with other shells, too, such as:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

when i execute two commands like echo $val(nn) > t.txt and awk -f throughput.awk wpan.tr >t.txt
i want the file t.txt to have two columns side by side leaving a tab or space in between. but i m getting output like this

pls help in this regard…thanks

I’m not sure I understood your query. To merge corresponding or subsequent lines of files try paste command.

Источник

Linux append text to end of file

You need to use the >> to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.

How to redirect the output of the command or data to end of file

The procedure is as follows

  1. Append text to end of file using echo command:
    echo ‘text here’ >> filename
  2. Append command output to end of file:
    command-name >> filename

How to add lines to end of file in Linux

The >> is called as appending redirected output. Create the file if does not exists. For example, append some networking command to net.eth0.config.sh script:
echo ‘I=eth0’ >> net.eth0.config.sh
echo ‘ip link set $I up’ >> net.eth0.config.sh
echo ‘ip addr add 10.98.222.5/255.255.255.0 dev $I’ >> net.eth0.config.sh
echo ‘ip route add default via 10.98.222.1’ >> net.eth0.config.sh

You can also add data to other config files. Another option is to run command and append output to a file. Run data command at the terminal and append output to output.txt:
date >> output.txt
Execute ls command and append data to files.txt:
ls >> files.txt
To see files.txt use cat command:
cat files.txt
more files.txt
less files.txt

How to append standard output and standard error

The following sytax allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file name. The format for appending standard output and standard error is:
echo ‘text’ &>>filename
command &>>filename
find . type d -name «*.projects» &>> list.txt
This is semantically equivalent to
echo ‘text’ >>fileNameHere 2>&1
command >>fileNameHere 2>&1
date >>data.txt 2>&1

For more info read redirection topic.

Append text when using sudo

Try the tee command:
echo ‘text’ | sudo tee -a my_file.txt
echo ‘104.20.186.5 www.cyberciti.biz’ | sudo tee -a /etc/hosts
Of coruse we can use following syntax to append text to end of file in Linux
sudo sh -c ‘echo my_text >> file1’
sudo — bash -c ‘echo «some data» >> /my/path/to/filename.txt’
The -c option passed to the bash/sh to run command using sudo.
See “how to append text to a file when using sudo command on Linux or Unix” for more info.

Conclusion – Append text to end of file on Unix

To append a new line to a text on Unix or Linux, try:

Источник

How to append text to the end of a file in Linux

If you want to know how to append text to the end of a file in Linux, then you are in the right place. Folks, here we will discuss what are the various ways to append text to a file in Linux.

Table of Contents

Common Scenarios for appending the text to a file

  • appending the logs to a log file, generally done by servers.
  • saving the output of a command to a file. For example, saving the output of a script to a file for debugging purposes later on.
  • copying the content of a file to another file. For example, creating a backup file before making any change to a file.
  • concatenating a group of files content to another file. For example, merging a bunch of CSV files to create a new CSV file.

Linux Commands to append text to a File

We use redirection operator (>>) to append data to an existing text file. However, we need to provide the data to be appended to the file. Some of the common commands that are used along with >> operator are cat, echo, print, etc.

Let’s look at some examples to append text to a file in Linux.

1. Append text to the end of a file using redirection operators

Adding the text is possible with the “>” and “>>” operators. These are the output redirection operators. Redirection is sending the output to any file.

There are two types of redirection operator i.e. Input redirection and Output Redirection. “ ” is called Output redirection.

For your understanding, here’s what input redirection looks like.

Here, the input from the file i.e. linux.txt is sent to the tr command through input redirection operator. It will print the characters in uppercase as shown above in the image.

Talking about the Output redirection operator, it is used to send the output of the command to a file. It can be done using any command either cat or echo. Let’s understand it by the following example,

Using “>” operator would send the output of the cat command to the linux.txt file. Further, I have used cat command without “>” operator because here we not going to append text to the end of a file but are just displaying the content of the file.

We will consider the linux.txt file for further examples.

Similarly, “>>” redirection operator is used to append text to the end of a file whereas “>” operator will remove the content in the existing file. I hope the difference is cleared between both of them. Don’t worry, we will discuss this in more detail.

Let’s dive into the ways on how can we append the text in the file using various commands.

2. Using cat command with redirection operator (>>)

The “>>” operator is used to append text to the end of a file that already has content. We’ll use the cat command and append that to our linux.txt file.

Consider we want to append text to the end of a file i.e. linux.txt as shown above. Let’s have a look at the command below:

As shown above, the input text is sent to the file i.e. linux.txt. WE use the cat command once again with the append operator to print the text as shown in the example above.

You can see that the text has been appended at the bottom of the file. Remember while adding the text using “>>” operator through cat command, you’ll be working within the editor mode. To save the text and finish appending, you can press Ctrl+D.

3. Linux echo command with >>

The echo command is used to print the text, print the value of the variable and so on. If you want to have detail knowledge, you can check the tutorial on the echo command in Linux. While using the echo command, you need to add the text within quotations. The output of the echo command will be redirected through the”>>” operator to the file. Let’s have a look at the below command.

You can see that the text “I hope you understood the concept.” is appended at the bottom of the file. You can print the content of the file using cat command. Here also, the text is sent as output to the file through “>>” operator.

4. The tee command with -a option

The tee command is used to read the standard input and writes it to the standard output as well as files too. Using tee command with -a option will not overwrite the content but will append it to the file. Let’s understand through an example.

The text in the linux.txt file is redirected to tee command through “|” operator and is appended to the file linuxfordevices.txt. You can see that all the data is transferred to the new file.

5. Append using”printf” in Linux

You might be thinking why we are using printf in Linux as it is used in C language to print the text. We know that printf is used to display the standard output. Folks, we can also use printf command to append the text using “>>” operator in Linux. Just add the text within quotations and send it to the file as output. Let’s have a look at the command below:

6. Append the text using awk command

The awk command is used to perform the specific action on the text as directed by the program statement. Using the BEGIN keyword with awk command specifies that awk will execute the action as denoted in begin once before any input lines are read.

Therefore, we will provide the command enclosed within the BEGIN as shown below in the example. The command to append the text is provided in the BEGIN section, hence awk command will execute it once it reads the input lines. Let’s have a look at the command below:

You can see that the text “Feel free to reach us” has been appended to the bottom of the linux.txt file as shown above in the image. Use the cat command to print the text of the file.

7. Save the list of files to another file using >> operator

We can also save the list of the files in any directory using ls command with “>>” operator. We have to just use ls command followed by the “>>” operator and the file name where we need to save the output. Let’s have a look at the command below:

Conclusion

In this tutorial, we learned how to use the “>>” operator to append text to the end of a file. We also learned what is the difference between input redirection and output redirection, how to save the list of files and date to any file and so on. I hope that all your concerns are cleared. If face any issues, do let us know in the comment section.

Источник

Читайте также:  Vmware mac windows server
Оцените статью