Linux system administrators and users often need to process text files. While text editors like nano or vim are common, sometimes you may need to append content to a file directly from the command line . In this case, you can take advantage of the power of Linux built-in tools such as echo and tee commands. This guide will guide you through two ways to append text to a file without using a text editor in Linux , and provide practical examples.
Table of contents
On a newly installed minimizing Linux system without an internet connection, you may need to manually configure the network to access the online repository or install other tools.
Using echo or tee and Here Document , you can append necessary network configuration details to files such as /etc/systemd/network/network-config.network
or /etc/network/interfaces
.
Not only configuring an IP address, but there are many situations where you need to append text from the command line to a file using echo, tee, or similar tools, which may be more advantageous or even necessary:
A few days ago, I downloaded a Fedora 40 Server QEMU image for testing. When I first started the system using the Fedora 40 server QEMU image, the installer prompted me to manually set the IP address.
There is no text editor in the minimized Fedora server image, so I have to use the following method to configure the IP address in Fedora .
OK, don't say much. Let me show you how I append my network configuration directly to a file in /etc/systemd/network/
using the echo and tee commands.
The echo command is a powerful tool for printing text to a terminal, and when used in conjunction with output redirection, it can be used to append content to a file.
Here is how to use this method:
echo -e "\[Match\]\nName=enp3s0\n\n\n\[Network\]\nAddress=192.168.1.100/24\nGateway=192.168.1.1\nDNS=8.8.8.8" | sudo tee -a /etc/systemd/network/20-wired.network > /dev/null
Let's break down this command:
echo -e
allows interpretation of backslash escape characters, allowing you to include newlines ( \n
) in quote text.|
(pipe) symbol redirects the output of echo to the tee command.sudo tee -a /etc/systemd/network/20-wired.network
appends the pipeline content to the specified file ( /etc/systemd/network/20-wired.network
). The sudo
command is used to grant the necessary permissions to write to the file.> /dev/null
redirects tee's standard output to an empty device ( /dev/null
), effectively discarding it and keeping the terminal clean.This method is especially useful when you need to append multiple lines of text, as you can include them all in quoted strings passed to echo.
The above command will add the following content to the /etc/systemd/network/20-wired.network
file. You can verify it by viewing the file content using the cat
command:
<code>$ cat /etc/systemd/network/20-wired.network [Match] Name=enp3s0 [Network] Address=192.168.1.100/24 Gateway=192.168.1.1 DNS=8.8.8.8</code>
Another way to append text to a file is to use the tee command in conjunction with the Here Document. This method is better readability and manageability when dealing with multi-line content.
Here is an example:
sudo tee -a /etc/systemd/network/20-wired.network > /dev/null <p> The use of Here Document makes the code easier to read and facilitates adding multiple lines of text. <code>EOF</code> is an end mark, which can be replaced with other marks, as long as the start and end marks are consistent.</p><p> Both methods are effective for script and command line usage, and the choice between them is usually determined by personal preference or specific use cases.</p><p> The echo method is simpler, but for longer or more complex content, readability may be reduced. The Here Document method is usually easier to read, but requires an extra step to declare the beginning and end of the input.</p><p> Both of these methods allow you <strong>to append content to a file directly from the Linux command line</strong> without a text editor. As a Linux administrator, mastering these command line skills can increase your productivity and increase your productivity.</p>
The above is the detailed content of How to Append Text to a File in Linux (Without Text Editors). For more information, please follow other related articles on the PHP Chinese website!