Linux

Bash Write to File

Bash Write to File

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 

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 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.

WinMouse lets you customize & improve mouse pointer movement on Windows PC
If you want to improve the default functions of your mouse pointer use freeware WinMouse. It adds more features to help you get the most out of your h...
Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...
Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...