Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash: Append to File - DesignLinux

Jul 29 2020
designlinux 0 Comments
Append to File

This tutorial explains how to append text to a file in Bash. There are different ways to append text to a file.

Prerequisite#

Your user must have write permission to a file in which you want to append text. Otherwise, you will receive a permission denied error.

Append Text using Redirection Operator (>>)#

Using Redirection you can take the output from a command and put it as input to another command or file. The >> redirection operator appends the output to a given file.

There are many commands that are used to print text to the standard output, such as echo and printf are being most used. Following is the example to add text to file using redirection operator:

echo "this is a new line" >> filename.txt

In above command you should specify the file name after the redirection operator. You should use the -e option with the echo command to interpret the backslash-escaped characters such as newline \n:

echo -e "this is a first line \nthis is second line" >> filename.txt

If you want to specify the formatting output, you should use printf command.

printf "Hello, I'm %s.\n" $USER >> filename.txt

Append Text using the tee Command#

In Linux, the tee is a command-line utility, which reads from the standard input and writes to both standard output and files at the same time.

By default, the tee command overwrites the specified file. To append the output to the file use tee with the -a (--append) option:

echo "this is a new line" | tee -a file.txt

If you don’t want tee to write to the standard output, redirect it to /dev/null:

echo "this is a new line" | tee -a file.txt >/dev/null

The main benefit of tee command over the redirection operator is, tee allows to you to append text to multiple files at once, and to write to files owned by other users in conjunction with sudo.

To append text to a file that you don’t have write permissions to, you should use sudo before tee as shown below:

echo "this is a new line" | sudo tee -a file.txt

To append text to more than one file, specify the files as arguments to the tee command:

echo "this is a new line" | tee -a file1.txt file2.txt file3.txt

Conclusion#

In Linux, to append text to a file in bash, use the >> redirection operator or the tee command.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: bash, terminal

How to Install Memcached on CentOS 8

Prev Post

How to Install Nagios on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 256
  • 614,628

DesignLinux.com © All rights reserved

Go to mobile version