Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Append Text to End of File in Linux - DesignLinux

May 29 2020
designlinux 0 Comments

While working with configuration files in Linux, sometimes you need to append text such as configuration parameters to an existing file. To append simply means to add text to the end or bottom of a file.

In this short article, you will learn different ways to append text to the end of a file in Linux.

Append Text Using >> Operator

The >> operator redirects output to a file, if the file doesn’t exist, it is created but if it exists, the output will be appended at the end of the file.

For example, you can use the echo command to append the text to the end of the file as shown.

# echo "/mnt/pg_master/wal_archives     10.20.20.5(rw,sync,no_root_squash)" >> /etc/exports

Alternatively, you can use the printf command (do not forget to use \n character to add the next line).

# printf "/mnt/pg_master/wal_archives     10.20.20.5(rw,sync,no_root_squash)\n" >> /etc/exports

You can also use the cat command to concatenate text from one or more files and append it to another file.

In the following example, the additional file system shares to be appended in the /etc/exports configuration file are added in a text file called shares.txt.

# cat /etc/exports
# cat shares.txt
# cat shares.txt >>  /etc/exports
# cat /etc/exports
Append Files to /etc/exports

Append Files to /etc/exports

Besides, you can also use the following here document to append the configuration text to the end of the file as shown.

# cat /etc/exports
# cat >>/etc/exports<s<EOF
> /backups 10.20.20.0/24(rw,sync)
> /mnt/nfs_all 10.20.20.5(rw,sync)
> EOF
# cat /etc/exports
Append Text Using here Document

Append Text Using here Document

Attention: Do not mistake the > redirection operator for >>; using > with an existing file will delete the contents of that file and then overwrites it. This may result in data loss.

Append Text Using tee Command

The tee command copies text from standard input and pastes/writes it to standard output and files. You can use its -a flag to append text to the end of a file as shown.

# echo "/mnt/pg_master/wal_archives     10.20.20.5(rw,sync,no_root_squash)" | tee -a /etc/exports
OR
# cat shares.txt | tee -a /etc/exports
Append Text Using Tee Command

Append Text Using Tee Command

You can also use a here document with the tee command.

# cat <<EOF | tee -a /etc/exports
>/backups 10.20.20.0/24(rw,sync)
>/mnt/nfs_all 10.20.20.5(rw,sync)
EOF
Append Text Using Here and Tee Command

Append Text Using Here and Tee Command

You might also like to read these related articles.

  1. How to Run Commands from Standard Input Using Tee and Xargs in Linux
  2. Learn The Basics of How Linux I/O (Input/Output) Redirection Works
  3. How to Save Command Output to a File in Linux
  4. How to Count Word Occurrences in a Text File

That’s it! You have learned how to append text to the end of a file in Linux. If you have questions or thoughts to share, reach us via the feedback form below.

Sharing is Caring…
Share on FacebookShare on TwitterShare on LinkedinShare on Reddit

Related

Tags: Linux Tricks

How to Install OwnCloud on CentOS 8

Prev Post

WireGuard – A Fast, Modern and Secure VPN Tunnel for Linux

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