Linux 101: Append String/Data To A File

rpm dependencies, linux, ubuntu

Appending data to a file is one of the most basic yet useful tasks in Linux, especially for system admins and developers. Whether you’re working on scripts, logs, or just need to add some text to an existing file, Linux offers several straightforward ways to do this.

Here, we’ll walk through five methods you can use to append text to files in Linux. All the examples below should work on most Linux distributions like Ubuntu, CentOS, Fedora, and more.

1. Using the echo Command

The echo command is probably the simplest way to add a line of text to the end of a file. Let’s say you want to add “Hello, World!” to a file called example.txt:

echo "Hello, World!" >> example.txt

The >> operator tells the system to append the text to the file rather than overwriting it. If the file doesn’t exist, it will create it for you.

2. Using cat to Append Multiple Lines

While cat is commonly used to display file contents, you can also use it to append text interactively. For example, if you want to append several lines to example.txt:

cat >> example.txt

After running this command, you can start typing whatever you want to add to the file. When you’re done, hit Ctrl+D to save and exit. This is especially useful for adding longer sections of text manually.

3. Appending with tee

The tee command is a bit different—it reads from standard input and writes to both the terminal and a file. To use it for appending, you’ll need to add the -a option:

echo "New line with tee" | tee -a example.txt

This will display the text on your screen while also appending it to example.txt. It’s handy when you want to see the output while writing it to a file.

See also  How to Create Thousands/Millions Files in Linux

4. Using printf for More Control

If you need more formatting control, printf is a good alternative to echo. Here’s how to use it to append text:

printf "Formatted text\n" >> example.txt

Unlike echo, printf doesn’t automatically add a newline (\n), so make sure to include it where necessary. This method is ideal if you want to append formatted text or work with variables in scripts.

5. Appending Command Output with Redirection

Sometimes, you might want to append the output of a command to a file. For instance, if you want to add the result of ls -l to example.txt:

ls -l >> example.txt

This appends the directory listing in long format to the file. You can use this method with most Linux commands to capture their output in a file.

Appending Blank Lines

If you ever need to append a blank line, you can do this easily with echo or printf:

echo "" >> example.txt

Or, to be more explicit with the newline:

echo -e "\n" >> example.txt

Both methods will add a blank line at the end of the file, which can be useful for separating content visually.

Adding Text to the Beginning of a File

Adding text to the beginning of a file is a bit more complex than appending to the end, but it’s still doable. One way to do it is by using the sed command:

sed -i '1s/^/Initial Text\n/' example.txt

This command edits the file in place (-i), inserting “Initial Text” at the very beginning. It’s a powerful option when you need to prepend text, but use it carefully as it directly modifies the file.

See also  Linux Tips: Using 'tail' to Display File Updates in Realtime

Concatenating Strings in Bash Scripts

If you’re working in a Bash script and want to concatenate strings, it’s very straightforward. Here’s a quick example:

str1="Hello, "
str2="World!"
combined="$str1$str2"
echo $combined

This will output “Hello, World!” to the terminal. You can also use curly braces for clarity when dealing with longer variables:

combined="${str1}${str2}"

Wrapping Up

Linux gives you several ways to append data to files, whether you’re adding text, output from a command, or even formatted strings. Learning these methods will help you handle files more efficiently and make your day-to-day work easier. Once you’re familiar with these tools, you’ll find yourself using them regularly to manage logs, scripts, and more.

Whether you’re just starting with Linux or have been using it for years, mastering file manipulation commands like these is key to making the most of the system.

20 thoughts on “Linux 101: Append String/Data To A File”

  1. Hi,

    I am having two text file (Let’s assume sample1.txt & sample2.txt). sample1.txt contains only one line “xyz” while sample2.txt contains many lines like
    “abc
    def
    ghi
    jkl
    mno”

    Now I need to create a new file which should contain the contents of both the file like below

    “xyz abc
    xyz def
    xyz ghi
    xyz jkl
    xyz mno”

    Is there any way in Unix to achieve the same ?

    Any type of help will be much appreciated.

    Thanks,
    Prashant

  2. Thank you very much! I was looking for how to set a new line in a txt file, and just add one echo per line in the loop resolve it. Thank you!

  3. In response to Prashant:
    —————————–
    user@host0 ~]$ for x in `cat sample2.txt`
    > do
    > echo “`cat sample1.txt` $x” >> sample3.txt
    > done
    user@host0 ~]$ cat sample3.txt
    xyz abc
    xyz def
    xyz ghi
    xyz jkl
    xyz mno

  4. Hasan,

    Use this to find all the files called test.txt under the current tree and to append a line:

    for f in `find . -name test.txt`; do echo “test” >> $f; done;

  5. Hi everyone,

    I am looking to append a string to a particular string as a reference in sample.txt or sample.xml, Can Someone shed some light on it

    sample.xml

    to
    appended sample.xml with “valid” as reference string

    Regards

Leave a Comment