Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash: Write to File - DesignLinux

designlinux 0 Comments

One of the most common tasks when writing Bash scripts or working on the Linux command line is reading and writing files.

This article explains how to write text to a file in Bash, using the redirection operators and tee command.

Writing to a File using Redirection Operators #

In Bash, the redirection of output allows you to capture the output from a command and write it to a file.

The general format for redirecting and writing output to a file is as follows:

output > filename 
output >> filename 
  • The > redirection operator writes the output to a given file. If the file exists, it is truncated to zero length. Otherwise, the file is created. Be extra careful when using this operator as you may overwrite an important file.
  • The >> redirection operator appends the output to a given file. The file is created if it does not exist.

You need to have write permissions to the file. Otherwise, you will receive a permission denied error.

Here is a simple example showing how the redirect the output of the echo command to a file:

echo "this is a line" > file.txt

To prevent overwriting existing files, enable the “noclobber” option with the set builtin:

set -o noclobberecho "this is a line" > file.txt
bash: file.txt: cannot overwrite existing file

The >| operator allows you to override the Bash “noclobber” option

set -o noclobberecho "this is a line" >| file.txt

The >> operator append the output to the end of the file, rather than overwriting the file:

echo "this is a line" >> file.txt

Use the printf command if you want to create a complex output:

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

If you want to write multiple lines to a file, use the Here document (Heredoc) redirection.

For example, you can pass the content to the cat command and write it to a file:

cat << EOF > file.txt
The current working directory is: $PWD
You are logged in as $(whoami)
EOF

To append the lines, change > with >> before the file name:

cat << EOF >> file.txt
The current working directory is: $PWD
You are logged in as $(whoami)
EOF

You can write the output of any command to a file:

date +"Year: %Y, Month: %m, Day: %d" > file.txt

The output of the date command will be written to the file.

Writing to a File using the tee Command #

The tee command reads from the standard input and writes to both standard output and one or more files simultaneously.

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

The tee command’s default behavior is to overwrite the specified file, same as the > operator. To append the output to the file, invoke the command with the -a (--append) option:

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

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

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

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

echo "this is a line" | tee file_1.txt file_2.txt file_3.txt

Another advantage of the tee command is that you can use it in conjunction with sudo and write to files owned by other users. To append text to a file that you don’t have write permissions to, prepend sudo before tee:

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

The echo command output is passed as input to the tee, which elevates the sudo permissions and writes the text to the file.

Conclusion #

In Linux, to write text to a file, use the > and >> redirection operators or the tee command.

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

bash terminal

Related

Tags: bash, terminal

How To Migrate Virtualbox VMs Into KVM VMs In Linux

Prev Post

Managing KVM Virtual Machines with Cockpit Web Console in 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
  • 1
  • 607
  • 1,055,379

DesignLinux.com © All rights reserved

Go to mobile version