Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Append Lines to a File in Linux
Appending lines to a file is a fundamental Linux operation that allows you to add new content to existing files without overwriting their original contents. This is particularly useful for log files, configuration updates, and data collection tasks where preserving existing information is crucial.
Using echo Command
The echo command is the simplest method to append text to a file. It displays text and can redirect output using the append redirection operator >>.
echo "New line of data" >> filename.txt
The >> operator appends the text to the end of the file. If the file doesn't exist, echo will create it automatically.
Using cat Command
The cat command can append multiple lines using a here document (heredoc) syntax, which is useful for adding several lines at once.
cat <<EOF >> filename.txt First new line Second new line Third new line EOF
The <<EOF syntax creates a here document where you can input multiple lines until you type EOF (End of File marker).
Using tee Command
The tee command writes output to both the terminal and a file simultaneously. The -a flag enables append mode.
echo "New line of data" | tee -a filename.txt
This method is particularly useful when you want to see the output on screen while also saving it to a file.
Using printf Command
The printf command offers more formatting control compared to echo, making it ideal for structured data.
printf "New line of data<br>" >> filename.txt printf "Formatted: %s - %d<br>" "text" 123 >> filename.txt
The creates a newline character. printf allows format specifiers like %s for strings and %d for integers.
Using sed Command
The sed (stream editor) command can append lines using the a command with the -i flag for in-place editing.
sed -i '$a New line of data' filename.txt
The $a tells sed to append after the last line ($). The -i flag modifies the file directly.
Using File Redirection
Any command's output can be appended to a file using the >> redirection operator.
ls -la >> directory_listing.txt date >> logfile.txt
This method works with any command that produces output, making it versatile for logging and data collection.
Comparison of Methods
| Command | Best For | Advantages | Limitations |
|---|---|---|---|
| echo | Simple text | Easy syntax, creates file if missing | Limited formatting |
| cat | Multiple lines | Here document support | Interactive input required |
| tee | Viewing + saving | Displays output on screen | Requires piping |
| printf | Formatted data | Advanced formatting options | More complex syntax |
| sed | Text manipulation | In-place editing, powerful options | Steeper learning curve |
Practical Examples
Creating a Log Entry
echo "$(date): User logged in" >> system.log
Adding Configuration Settings
cat <<EOF >> config.txt server=192.168.1.1 port=8080 timeout=30 EOF
Conclusion
Linux provides multiple methods to append lines to files, each suited for different scenarios. The echo command works best for simple text, while cat with here documents excels for multiple lines. Choose the method that best fits your specific requirements and complexity needs.
