How To

Bash Write To File: How To Save Output In A File

Introduction

Writing output to a file is a basic and important skill in Bash scripting. In Linux, many tasks produce output in the terminal, but sometimes you need to save that output for later use. Bash allows you to write text, command results, logs, reports, and script output directly into files.

If you are learning Bash scripting, understanding how to write to a file is very useful. It helps in automation, log creation, backup scripts, system reports, cybersecurity monitoring, and error tracking. For example, you can save scan results, failed login attempts, system information, or backup logs into a file.

Write Text To A File Using >

The > operator is used to write output to a file. If the file does not exist, Bash creates it. If the file already exists, Bash overwrites it.

Example:

echo "Welcome to Bash scripting" > output.txt

Check the file content:

cat output.txt

Output:

Welcome to Bash scripting

Be careful with > because it replaces old content.

Append Text To A File Using >>

The >> operator is used to add new content to the end of a file without deleting existing content.

Example:

echo "This is a new line" >> output.txt

Check the file:

cat output.txt

Output:

Welcome to Bash scriptingThis is a new line

Use >> when you want to keep old data and add new information.

Write Command Output To A File

You can save the output of Linux commands into a file.

Example:

whoami > user.txt

Save system information:

uname -a > system-info.txt

Save current date:

date > date.txt

This is useful when creating system reports.

Write Multiple Lines To A File

You can use multiple echo commands:

#!/bin/bashecho "System Report" > report.txtecho "User: $(whoami)" >> report.txtecho "Hostname: $(hostname)" >> report.txtecho "Date: $(date)" >> report.txt

Save it as:

nano write-report.sh

Run it:

chmod +x write-report.sh./write-report.sh

View the report:

cat report.txt

Write To File Using Heredoc

A here document is useful for writing multiple lines clearly.

#!/bin/bashcat > message.txt << EOFHello,This file was created using a Bash script.Bash scripting is useful for Linux automation.EOF

This creates message.txt with multiple lines.

Cybersecurity Example: Save Failed SSH Logins

You can save failed SSH login attempts to a file:

#!/bin/bashgrep "Failed password" /var/log/auth.log > failed-ssh-logins.txtecho "Failed SSH login report saved."

This is useful for basic log analysis and security monitoring.

Conclusion

Writing to a file in Bash is simple and powerful. You can use > to overwrite a file and >> to append content. You can also save command output, create reports, write logs, and generate files using Bash scripts.

For beginners, learning how to write output to files is important because many real-world Bash scripts depend on logging and reporting. This skill is useful for Linux administration, automation, backups, and cybersecurity scripting.

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

4 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

15 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

16 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

16 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

16 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

17 hours ago