Practice task
6.1 Task Overview
Now that you've learned the basics, complete this comprehensive task that combines all the skills you've practiced. This task will test your ability to navigate directories, manage files, create scripts, and handle permissions.
6.2 Task Requirements
Starting location: Your home directory
Complete the following tasks in order:
-
Setup Phase:
- Create a new directory called
assessment_task - Inside it, create three subdirectories:
data,scripts, andoutput - Navigate to the
datadirectory
- Create a new directory called
-
File Management Phase:
- Create a text file called
student_info.txtcontaining your name and course - Copy this file to create
student_backup.txt - Move
student_backup.txtto theoutputdirectory - Create another file called
notes.txtwith any content you like
- Create a text file called
-
Scripting Phase:
- Navigate to the
scriptsdirectory - Create a script called
file_manager.shthat:- Displays the current date and time
- Shows the current working directory
- Lists all files in the parent
datadirectory - Asks the user for their favorite programming language
- Creates a file in the
outputdirectory named after their response (e.g.,python.txt)
- Make the script executable
- Run the script and test it works
- Navigate to the
-
Permissions Phase:
- Create a file called
restricted.txtin thedatadirectory - Set its permissions to 600 (owner read/write only)
- Verify the permissions are set correctly
- Create a file called
-
Final Verification:
- Navigate back to
assessment_taskroot directory - List the contents of all subdirectories to verify your work
- Create a final script called
cleanup_check.shin the scripts directory that displays a summary of all files and directories you've created
- Navigate back to
6.3 Expected Directory Structure
When complete, your directory structure should look like this:
~/assessment_task/
├── data/
│ ├── student_info.txt
│ ├── notes.txt
│ └── restricted.txt (permissions: 600)
├── scripts/
│ ├── file_manager.sh (executable)
│ └── cleanup_check.sh
└── output/
├── student_backup.txt
└── [language].txt (created by your script)
6.4 Sample Solution
Below is the complete sequence of commands to accomplish this task:
Click here to view the sample solution for Setup Phase
# Setup Phase
cd ~
mkdir assessment_task
cd assessment_task
mkdir data scripts output
cd data
Click here to view the sample solution for File Management Phase
# File Management Phase
echo "Student Name: [Your Name]
Course: [Your Course]" > student_info.txt
cp student_info.txt student_backup.txt
mv student_backup.txt ../output/
echo "These are my notes for the Linux tutorial." > notes.txt
Click here to view the sample solution for Scripting Phase
# Scripting Phase
cd ../scripts
cat > file_manager.sh << 'EOF'
#!/bin/bash
# File Manager Script
# This script demonstrates file operations and user interaction
echo "=== File Manager Script ==="
# Display current date and time
echo "Current date and time: $(date)"
# Show current working directory
echo "Current directory: $(pwd)"
# List files in the parent data directory
echo "Files in data directory:"
ls -la ../data/
# Ask user for favorite programming language
echo "What is your favorite programming language?"
read fav_language
# Create file in output directory named after their response
echo "You chose: $fav_language" > "../output/${fav_language}.txt"
echo "Created file: ${fav_language}.txt in output directory"
echo "=== Script completed ==="
EOF
chmod +x file_manager.sh
./file_manager.sh
Click here to view the sample solution for Permissions Phase
# Permissions Phase
cd ../data
touch restricted.txt
chmod 600 restricted.txt
ls -l restricted.txt
Click here to view the sample solution for Final Verification
# Final Verification
cd ..
echo "=== Directory Contents ==="
echo "Data directory:"
ls -la data/
echo "Scripts directory:"
ls -la scripts/
echo "Output directory:"
ls -la output/
Click here to view the sample solution for Create Final Summary Script
# Create final summary script
cd scripts
cat > cleanup_check.sh << 'EOF'
#!/bin/bash
# Cleanup Check Script
# This script provides a summary of all created files and directories
echo "=== Assessment Task Summary ==="
echo "Task completed on: $(date)"
echo ""
echo "Directory structure created:"
echo "assessment_task/"
echo "├── data/"
for file in ../data/*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
perms=$(ls -l "$file" | cut -d' ' -f1)
echo "│ ├── $filename ($perms)"
fi
done
echo "├── scripts/"
for file in .*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
if [ -x "$file" ]; then
echo "│ ├── $filename (executable)"
else
echo "│ ├── $filename"
fi
fi
done
echo "└── output/"
for file in ../output/*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
echo " ├── $filename"
fi
done
echo ""
echo "Total files created: $(find ../ -type f | wc -l)"
echo "Total directories created: $(find ../ -type d | wc -l)"
echo "=== Summary Complete ==="
EOF
# Change the file permissions to execute
chmod +x cleanup_check.sh
./cleanup_check.sh
# Return to assessment_task root
cd ..
pwd
Note: When you run file_manager.sh, you'll be prompted to enter your favorite programming language. The script will create a file with that name in the output directory.
Summary
Congratulations! You've completed this Linux tutorial. You've learned to:
- Navigate the file system using
cd,pwd, andls - Create directories with
mkdir - Create and manage files with
touch,cp,mv - Understand file permissions and the meaning of
rwx - Create bash scripts with proper comments
- Use chmod to modify file permissions
- Create interactive scripts that accept user input
- Remove directories recursively with
rm -r
Key Commands Reference
| Command | Purpose | Example |
|---|---|---|
pwd | Show current directory | pwd |
ls | List directory contents | ls -la |
cd | Change directory | cd /home/user |
mkdir | Create directory | mkdir new_folder |
touch | Create empty file | touch file.txt |
cp | Copy files | cp file1.txt file2.txt |
mv | Move/rename files | mv old.txt new.txt |
chmod | Change permissions | chmod 755 script.sh |
rm -r | Remove recursively | rm -r directory/ |
Next Steps
Now that you're comfortable with basic Linux operations, consider exploring:
- Advanced bash scripting with loops and conditionals
- Text processing with
grep,sed, andawk - Process management with
ps,top, andkill - File searching with
findandlocate - Network commands like
wget,curl, andssh
Happy scripting!