Menu
Coddy logo textTech

Creating Backup Folder

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

challenge icon

Challenge

Easy

Now let's extend the backup script to create a backup folder where the files will be stored.

Modify backup.sh to:

  1. Keep the existing code that prompts for the source directory
  2. Add a new variable called backup_dir with the value backup
  3. Use mkdir -p to create the backup directory (the -p flag ensures no error if it already exists)
  4. Print the message: Created backup folder: [backup_dir]

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

Backing up: documents
Created backup folder: backup

After running the script, verify the folder was created by running ls to list the directory contents.

Hint: Add backup_dir="backup" after the read command, then use mkdir -p "$backup_dir" to create the folder. Use echo to print the confirmation message with the variable.

Try it yourself

Terminal
cat > backup.sh << 'EOF'
#!/bin/bash
read -p "Enter source directory: " source_dir
echo "Backing up: $source_dir"
EOF
chmod +x backup.sh
echo "documents" | ./backup.sh

All lessons in Fundamentals