Menu
Coddy logo textTech

Copying The Files

Part of the Fundamentals section of Coddy's Terminal journey — lesson 77 of 82.

challenge icon

Challenge

Easy

Now it's time to add the core functionality: copying files from the source directory to the backup folder.

Modify backup.sh to:

  1. Keep all the existing code from the previous lesson
  2. After creating the backup folder, use cp to copy all files from the source directory to the backup directory
  3. Print the message: Files copied successfully

Run the script and enter documents when prompted. Your output should be:

Backing up: documents
Created backup folder: backup
Files copied successfully

After running the script, verify the files were copied by listing the contents of the backup folder with ls backup.

Hint: Use cp "$source_dir"/* "$backup_dir" to copy all files from the source to the backup directory. The * wildcard matches all files in the directory.

Try it yourself

Terminal
echo '#!/bin/bash' > backup.sh
echo 'read -p "Enter source directory: " source_dir' >> backup.sh
echo 'echo "Backing up: $source_dir"' >> backup.sh
echo 'backup_dir="backup"' >> backup.sh
echo 'mkdir -p "$backup_dir"' >> backup.sh
echo 'echo "Created backup folder: $backup_dir"' >> backup.sh
chmod +x backup.sh
echo "documents" | ./backup.sh
ls

All lessons in Fundamentals