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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

2 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

2 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

2 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

2 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

2 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

3 days ago