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
cat > backup.sh << 'EOF'
#!/bin/bash
read -p "Enter source directory: " source_dir
echo "Backing up: $source_dir"
backup_dir="backup"
mkdir -p "$backup_dir"
echo "Created backup folder: $backup_dir"
EOF
echo "documents" | bash backup.sh
ls

All lessons in Fundamentals