Post

Linux Report Creation and Email Submission Guide

Generating a system report and emailing it from the command line are two fundamental Linux skills that every administrator should have in their toolkit. This guide walks through both approaches — a quick manual method using shell redirection and a reusable Bash script — then covers three ways to submit the result via the mail command.

Understanding Output Redirection

Before diving into the commands, it helps to understand the two redirection operators you will use throughout this guide:

Operator Behavior
> Creates a new file, or overwrites an existing one
>> Appends output to an existing file without deleting its contents
2>&1 Redirects error messages (stderr) to the same destination as standard output (stdout)

The 2>&1 pattern is especially important for reports: if a command fails, the error message is captured inside the report file rather than disappearing from the terminal.

Option A: Manual Report Creation

The manual method builds a report file step by step using three groups of commands. It is ideal for learning redirection and command substitution before moving on to scripting.

Command 1: Create the Report Header

The first command creates the file and writes identification information into it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Create report file with title (overwrites any existing file)
echo "Linux System Report" > report.txt

# Add separator line
echo "=====================" >> report.txt

# Add blank line for spacing
echo "" >> report.txt

# Add your username
echo "Student Name: $(whoami)" >> report.txt

# Add the system hostname
echo "Hostname: $(hostname)" >> report.txt

# Add current date and time
echo "Date: $(date)" >> report.txt

# Add trailing blank line
echo "" >> report.txt

The $(command) syntax is command substitution — the shell executes the command inside $() and inserts its output inline. So $(whoami) is replaced by your username, $(hostname) by the machine name, and $(date) by the current timestamp.

Command 2: Add User Information

Search /etc/passwd for specific users and append the results to the report:

1
2
# Add section header, search for users, capture output and errors
{ echo "=== Created Users ===" ; grep -E "developer1|developer2|analyst1" /etc/passwd ; } >> report.txt 2>&1

Important: The usernames developer1, developer2, and analyst1 are examples only. Replace them with the actual usernames relevant to your project or assignment.

The curly braces { } group multiple commands so their combined output is redirected together in a single operation. grep -E enables extended regular expressions, and the | character inside the pattern means “or” — matching any of the listed names.

Command 3: Add Group Information

Search /etc/group for specific groups using the same pattern:

1
2
# Add blank line, section header, search for groups, capture all output
{ echo "" ; echo "=== Created Groups ===" ; grep -E "devteam|analysts|projectx" /etc/group ; } >> report.txt 2>&1

Important: Replace devteam, analysts, and projectx with your actual group names.

Verify the Report

View the completed file with cat:

1
cat report.txt

Expected output format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Linux System Report
=====================

Student Name: vagrant
Hostname: debian-gui-lab
Date: Fri Oct 25 10:30:45 UTC 2024

=== Created Users ===
developer1:x:1001:1001::/home/developer1:/bin/bash
developer2:x:1002:1002::/home/developer2:/bin/bash
analyst1:x:1003:1003::/home/analyst1:/bin/bash

=== Created Groups ===
devteam:x:1004:developer1,developer2
analysts:x:1005:analyst1
projectx:x:1006:developer1,developer2,analyst1

Option B: Automated Report Creation (Bash Script)

A Bash script packages all of the above into a single reusable file. Run it once after any project change and get a freshly generated, consistently formatted report every time.

Step 1: Create the Script File

Open a text editor and create generate_report.sh:

1
nano generate_report.sh

Paste in the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Linux System Report Generator
# This script creates a comprehensive report of system information

# Define the output report filename
REPORT_FILE="report.txt"

# Clear any existing report file to start fresh
> "$REPORT_FILE"

# Function to add a formatted section header
add_section() {
    echo "" >> "$REPORT_FILE"
    echo "=== $1 ===" >> "$REPORT_FILE"
    echo "" >> "$REPORT_FILE"
}

# Create report title and main header
echo "Linux System Report" > "$REPORT_FILE"
echo "=====================" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

# System information section
add_section "System Information"
echo "Student Name: $(whoami)" >> "$REPORT_FILE" 2>&1
echo "Hostname: $(hostname)" >> "$REPORT_FILE" 2>&1
echo "Date: $(date)" >> "$REPORT_FILE" 2>&1

# Created users section — CUSTOMIZE these user names for your project
add_section "Created Users"
grep -E "developer1|developer2|analyst1" /etc/passwd >> "$REPORT_FILE" 2>&1

# Created groups section — CUSTOMIZE these group names for your project
add_section "Created Groups"
grep -E "devteam|analysts|projectx" /etc/group >> "$REPORT_FILE" 2>&1

# Add completion timestamp
echo "" >> "$REPORT_FILE"
echo "Report generated successfully at $(date)" >> "$REPORT_FILE"

# Confirm to the user
echo "Report generated: $REPORT_FILE"
echo "Use 'cat $REPORT_FILE' to view the report"

Save and exit: Ctrl+XYEnter.

Customization required: Update the grep patterns for users and groups to match your actual project accounts before running the script.

Key script components explained:

  • #!/bin/bash — the shebang line. It must be the very first line of every Bash script with no blank lines or spaces before it. It tells the operating system which interpreter to use. Without it, the system will not know how to execute the file.
  • REPORT_FILE="report.txt" — a variable storing the output filename, making it easy to change in one place.
  • add_section() — a reusable function that prints a blank line, a formatted === Title === header, and another blank line, ensuring consistent section spacing throughout the report.
  • > "$REPORT_FILE" — truncates the file to zero bytes at the start of each run, so re-running the script always produces a clean report rather than appending to an old one.

Step 2: Make the Script Executable

By default, newly created files cannot be run as programs. Add execute permission with:

1
chmod +x generate_report.sh

Step 3: Run the Script

1
./generate_report.sh

The ./ prefix means “look in the current directory.” Expected output:

1
2
Report generated: report.txt
Use 'cat report.txt' to view the report

Then verify the result:

1
cat report.txt

Why Use a Script Instead of Manual Commands?

Factor Manual Commands Bash Script
Reusability Must retype every time Single command reruns everything
Consistency Prone to typos between runs Identical output every time
Maintainability Change each command individually Edit once, applies everywhere
Error risk Higher — easy to miss a step Lower — all steps always execute

Email Submission Using the mail Command

Once the report exists, you can send it from the command line using the mail utility.

Prerequisites: Install mailutils

If mail is not yet installed on your system:

1
2
sudo apt-get update
sudo apt-get install mailutils

This step only needs to be done once per system.

Local vs. external email: The mail command works out of the box for delivering messages between users on the same Linux system. Sending to external addresses (Gmail, Outlook, etc.) requires a properly configured mail server (Postfix, sendmail, etc.), which is beyond the scope of this guide. For lab assignments, confirm with your instructor whether local delivery is sufficient.

Method 1: Report as Email Body

Pipe the report contents directly into the email body — the simplest approach:

1
cat report.txt | mail -s "Linux System Report - YourName" name@example.com

Replace YourName with your actual full name and name@example.com with the recipient’s real address. The pipe | takes the output of cat and feeds it as input to mail, which uses it as the message body.

Method 2: Report as Attachment

Send a short message with the report file attached:

1
echo "Please find my Linux System Report attached." | mail -s "Linux System Report - YourName" -A report.txt name@example.com

The -A report.txt flag attaches the file. This keeps the email body clean and gives the recipient a file they can save directly to their filesystem.

Method 3: Professional Email with Custom Body and Attachment

For a polished submission, write a proper message body using a heredoc and send it with the report attached:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Create the email body file
cat > email_body.txt << EOF
Dear Instructor,

I am submitting my Linux System Report for your review.

This report includes:
- System identification information
- Created user accounts
- Group assignments
- Command outputs and error messages

Please find the complete report attached to this email.

Thank you for your time and consideration.

Best regards,
Your Full Name
EOF

# Send the email with the custom body and attachment
mail -s "Linux System Report - YourName" -A report.txt name@example.com < email_body.txt

The << EOF heredoc writes everything between the two EOF markers into email_body.txt. The final mail command then reads that file as its standard input (< email_body.txt) and attaches the report with -A.

Tip: This is the most professional submission format. It demonstrates knowledge of heredocs, input redirection, and proper email etiquette in a single workflow.

Verify the Email Was Sent

Check the mail system log to confirm delivery:

1
sudo tail -20 /var/log/mail.log

Look for a line containing your recipient’s address with a status of sent or delivered, along with a timestamp and a message ID assigned by the mail system.

Troubleshooting Common Issues

Problem Cause Solution
mail: command not found Package not installed sudo apt-get install mailutils
Permission denied on log Needs elevated privileges Prefix with sudo
report.txt: No such file Script or commands not yet run Generate the report first
Email not arriving External delivery not configured Check spam folder; confirm mail server setup with instructor
Script won’t execute Missing execute permission chmod +x generate_report.sh

Quick Reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# --- Manual method ---
echo "Linux System Report" > report.txt
echo "=====================" >> report.txt
echo "Student Name: $(whoami)" >> report.txt
echo "Hostname: $(hostname)" >> report.txt
echo "Date: $(date)" >> report.txt
{ echo "=== Created Users ===" ; grep -E "user1|user2" /etc/passwd ; } >> report.txt 2>&1
{ echo "" ; echo "=== Created Groups ===" ; grep -E "group1|group2" /etc/group ; } >> report.txt 2>&1

# --- Automated method ---
nano generate_report.sh       # create and edit the script
chmod +x generate_report.sh  # make it executable
./generate_report.sh          # run it

# --- Email submission ---
cat report.txt | mail -s "Report - YourName" name@example.com          # body only
echo "See attached." | mail -s "Report - YourName" -A report.txt name@example.com  # attachment
mail -s "Report - YourName" -A report.txt name@example.com < email_body.txt         # professional

# --- Verify ---
cat report.txt
sudo tail -20 /var/log/mail.log

Conclusion

Both methods produce the same result — a structured text report capturing system identity, user accounts, and group memberships. The manual approach is the better learning tool for understanding redirection and command substitution one step at a time. The Bash script is the right tool for anything you will run more than once.

For email submission, choose the method that fits the context: pipe for speed, -A for clean attachments, and the heredoc pattern when a professional presentation matters. Always verify delivery with mail.log before considering the task complete.

Additional Resources

  • man mail — full reference for the mail command and its flags
  • man bash — comprehensive Bash scripting reference
  • man grep — grep options including extended regular expressions

This post is licensed under CC BY 4.0 by the author.