Copying The Files
Part of the Fundamentals section of Coddy's Terminal journey — lesson 77 of 82.
Challenge
EasyNow it's time to add the core functionality: copying files from the source directory to the backup folder.
Modify backup.sh to:
- Keep all the existing code from the previous lesson
- After creating the backup folder, use
cpto copy all files from the source directory to the backup directory - 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 successfullyAfter 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
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
lsAll lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder11Permissions
Understanding PermissionsReading PermissionsChmod With NumbersChmod With SymbolsFile OwnershipRecap - Lock It Down14Backup Script Project
Project OverviewGetting The Source Path6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline