| name | file-operations |
| description | Production-grade file operations - permissions, find, archives, rsync |
| sasmp_version | 1.3.0 |
| bonded_agent | 03-file-operations |
| bond_type | PRIMARY_BOND |
| version | 2.0.0 |
| difficulty | intermediate |
| estimated_time | 5-7 hours |
File Operations Skill
Master file system operations with production-ready patterns
Learning Objectives
After completing this skill, you will be able to:
Prerequisites
- Bash basics
- Understanding of file systems
- Command line navigation
Core Concepts
1. Permission Management
chmod 755 script.sh
chmod 644 file.txt
chmod 600 secret.key
chmod u+x script.sh
chmod g-w file.txt
chmod a+r public.txt
chown user:group file
chown -R user:group dir/
chmod 600 ~/.ssh/id_rsa
chmod 755 /var/www/html/
2. Find Command
find . -name "*.txt"
find . -iname "*.TXT"
find . -type f
find . -type d
find . -type l
find . -size +100M
find . -mtime -7
find . -name "*.tmp" -delete
find . -type f -exec chmod 644 {} +
3. Archive Operations
tar -cvf archive.tar dir/
tar -czvf archive.tar.gz dir/
tar -cjvf archive.tar.bz2 dir/
tar -xvf archive.tar
tar -xzvf archive.tar.gz -C /dest/
zip -r archive.zip dir/
unzip archive.zip
4. Rsync
rsync -avz source/ dest/
rsync -avz local/ user@host:/remote/
rsync -avz --delete source/ dest/
rsync -avzn source/ dest/
Common Patterns
Safe Delete Pattern
rm -i file.txt
rm -rf "${DIR:?}/"
Backup Pattern
backup() {
local src="$1"
local timestamp=$(date +%Y%m%d_%H%M%S)
cp -a "$src" "${src}.${timestamp}.bak"
}
Find and Process
find /var/www -type d -exec chmod 755 {} +
find /var/www -type f -exec chmod 644 {} +
find /tmp -type f -mtime +7 -delete
Anti-Patterns
| Don't | Do | Why |
|---|
rm -rf $VAR/ | rm -rf "${VAR:?}/" | Empty VAR = delete / |
find | xargs rm | find -delete | Handles spaces |
cp -r for sync | rsync -a | rsync is smarter |
Practice Exercises
- Permission Fixer: Script to fix web dir permissions
- Old File Cleaner: Remove files older than N days
- Backup Script: Timestamped backup with rotation
- Sync Tool: Two-way directory sync
Troubleshooting
Common Errors
| Error | Cause | Fix |
|---|
Permission denied | Wrong permissions | Check with ls -la |
No such file | Path typo | Verify path exists |
Directory not empty | rm without -r | Add -r flag |
Cross-device link | Hard link across fs | Use symlink |
Debug Techniques
stat file.txt
ls -la file.txt
find . -name "*.txt" -print
rsync -avzn source/ dest/
Safety Guidelines
- Always dry-run rsync with
--delete first
- Quote paths with spaces:
"$path"
- Verify paths before
rm -rf
- Use trash instead of rm when possible
- Backup before bulk operations
Resources