Practical Examples
Real-World Directory Management Scenarios
1. File Organization Script
#!/bin/bash
## Organize downloads by file type
organize_downloads() {
mkdir -p ~/Downloads/{Images,Documents,Videos,Archives}
mv ~/Downloads/*.{jpg,png,gif} ~/Downloads/Images/ 2>/dev/null
mv ~/Downloads/*.{pdf,doc,txt} ~/Downloads/Documents/ 2>/dev/null
mv ~/Downloads/*.{mp4,avi,mkv} ~/Downloads/Videos/ 2>/dev/null
mv ~/Downloads/*.{zip,tar,gz} ~/Downloads/Archives/ 2>/dev/null
}
Workflow Visualization
graph TD
A[Source Directory] --> B{File Type}
B -->|Image| C[Images Folder]
B -->|Document| D[Documents Folder]
B -->|Video| E[Videos Folder]
B -->|Archive| F[Archives Folder]
2. Large File Finder
import os
def find_large_files(directory, size_threshold_mb=100):
large_files = []
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
if os.path.getsize(file_path) > size_threshold_mb * 1024 * 1024:
large_files.append((file_path, os.path.getsize(file_path) / (1024 * 1024)))
return large_files
## Example usage
large_files = find_large_files('/home/user', 50)
for file, size in large_files:
print(f"Large File: {file}, Size: {size:.2f} MB")
Common Directory Management Tasks
Task |
Command/Method |
Purpose |
Backup |
rsync -av /source /destination |
Sync directories |
Space Check |
du -sh /directory |
Check directory size |
Permission Reset |
chmod -R 755 /directory |
Recursive permissions |
3. Disk Space Monitoring Script
#!/bin/bash
## Monitor directory disk usage
check_disk_usage() {
df -h | grep -E 'Filesystem|/home'
echo "Top 5 Large Directories:"
du -h /home/* | sort -rh | head -5
}
Advanced Traversal Techniques
Recursive Search with Multiple Criteria
## Find files modified in last 7 days, larger than 10MB
find /home -type f -mtime -7 -size +10M
LabEx Learning Approach
In LabEx, these practical examples provide hands-on experience with directory traversal, helping learners develop real-world Linux skills.
Key Takeaways
- Automate repetitive directory management tasks
- Use scripts to enhance productivity
- Understand disk usage and file organization
- Practice different traversal techniques
- Develop problem-solving skills through practical examples