Creating Backup Folder
Part of the Fundamentals section of Coddy's Terminal journey — lesson 76 of 82.
Challenge
EasyNow let's extend the backup script to create a backup folder where the files will be stored.
Modify backup.sh to:
- Keep the existing code that prompts for the source directory
- Add a new variable called
backup_dirwith the valuebackup - Use
mkdir -pto create the backup directory (the-pflag ensures no error if it already exists) - 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: backupAfter 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 usemkdir -p "$backup_dir"to create the folder. Useechoto print the confirmation message with the variable.
Try it yourself
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.shAll 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