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

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

11 minutes ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

5 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

6 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

7 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

8 hours ago

How To Work With Files And Directories Using Bash Scripts

Introduction Working with files and directories is one of the most important skills in Bash…

9 hours ago