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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

3 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

3 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

4 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

4 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

1 day ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

1 day ago