Hello, scripters! Welcome to this focused tutorial on a fundamental and powerful shell operation: appending files.
What does "append" mean? It simply means adding the contents of one file to the end of another file, without deleting or overwriting any of the original data. This is an incredibly common task, perfect for creating log files, combining reports, or merging data.
Today, we'll write a simple, safe script that asks a user for a "source" file and a "destination" file, and then appends the first file to the second.
Let's get started!
The Shell Script
Here is the complete, commented script. It includes an important check to make sure the file you're copying from actually exists before it tries to do anything.
#!/bin/bash
# This script appends the content of one file to another.
echo "--- File Append Script ---"
# 1. Ask for the source file
read -p "Enter the source file (to copy FROM): " source_file
# 2. Ask for the destination file
read -p "Enter the destination file (to append TO): " dest_file
# 3. Check if the SOURCE file exists and is a regular file
if [ -f "$source_file" ]; then
# 4. If it exists, append its content to the destination file
# The ">>" operator is the key!
cat "$source_file" >> "$dest_file"
echo "-----------------------------------------"
echo "Success: Content of '$source_file' has been appended to '$dest_file'."
echo "Run 'cat $dest_file' to see the result."
else
# 5. If the source file doesn't exist, show an error
echo "-----------------------------------------"
echo "Error: Source file '$source_file' not found. No action taken."
fi
How It Works: A Step-by-Step Breakdown
Let's break down the most important lines of this script.
#!/bin/bash
The "shebang," which tells your system to use the Bash shell to run the script.read -p "..." source_file
Theread -pcommand prints the prompt ("...") and then waits to store the user's answer in thesource_filevariable. We do this again fordest_file.if [ -f "$source_file" ]; then
This is our safety check.if [ ... ]: This is a conditional test.-f: This tests if the path stored in$source_file"exists and is a regular file.""$source_file": We use quotes just in case the filename has spaces.- If this test fails (the file doesn't exist), the script jumps to the
elseblock and prints an error.
cat "$source_file" >> "$dest_file"
This is the core of the entire script. It's a two-part command.cat "$source_file": Thecat(concatenate) command reads the entire contents of the source file and prints it out.>>: This is the append redirect operator. It "catches" all the output from thecatcommand and adds it to the very end of the file specified on the right ($dest_file).
Important Warning: >> vs. >
Be extremely careful!
>>(Append): Adds to the end of a file. This is what we want.>(Overwrite): Deletes everything in the file first, then adds the new content.
If you accidentally use > in this script, you would erase your destination file. Always double-check for >> when you mean to append.
Note: If the destination file ($dest_file) doesn't exist, the >> operator is smart enough to create it for you!
How to Run the Script
Let's test our new script. First, we need to create some test files.
Step 1: Create your test files
In your terminal, run these two commands:
echo "This is File A. (Line 1)" > fileA.txt
echo "This is File B. (The only line... for now)" > fileB.txt
Step 2: Create the script file
Use nano (or any text editor) to create your file:
nano append.sh
Step 3: Paste and Save
Copy the script from the top of this post, paste it into nano, and save/exit (Ctrl+O, Enter, Ctrl+X).
Step 4: Make it executable
Give the script permission to run:
chmod +x append.sh
Step 5: Run it!
Execute your script:
$ ./append.sh
--- File Append Script ---
Enter the source file (to copy FROM): fileA.txt
Enter the destination file (to append TO): fileB.txt
-----------------------------------------
Success: Content of 'fileA.txt' has been appended to 'fileB.txt'.
Run 'cat fileB.txt' to see the result.
Step 6: Check the result
Now, let's look inside fileB.txt to see if it worked. Use the cat command:
$ cat fileB.txt
This is File B. (The only line... for now)
This is File A. (Line 1)
It worked perfectly! The content of fileA.txt was added right to the end of fileB.txt.
Challenge Yourself
You've just mastered a core file operation. Want to make the script even better?
Try modifying it to ask the user, "Are you sure you want to append? (y/n)" and only proceed if they type 'y'. (Hint: you'll need another read command and another if statement!)
Happy scripting!

0 Comments