Skip to main content

Lab 3.4 - Complete File System Practice

Objectives​

By the end of this lab, you will be able to:

  • Apply all acquired skills
  • Manage a complex file structure
  • Organize projects with appropriate permissions
  • Automate file management tasks

Prerequisites​

  • Ubuntu 24.04 installed
  • Terminal open
  • Knowledge from previous labs (3.1, 3.2, 3.3)

Scenario: Managing a Web Project​

You need to create and organize the structure of a web project with:

  • Executable scripts
  • Read-only documentation
  • Logs with rotation
  • Automatic backups
  • Secure configuration

Part 1: Project Structure​

Step 1: Create the Tree​

# Create the complete structure
mkdir -p ~/projet-web/{bin,src/{scripts,config},docs,logs,backup,www/{html,css,js}}

# View the structure
tree ~/projet-web
# If tree is not installed: sudo apt install tree

Step 2: Create the Files​

cd ~/projet-web

# Scripts
cat > src/scripts/deploy.sh << 'EOF'
#!/bin/bash
echo "Deployment at $(date)" >> ../logs/deploy.log
EOF

cat > src/scripts/backup.sh << 'EOF'
#!/bin/bash
tar -czf ../backup/backup-$(date +%Y%m%d).tar.gz ../www
EOF

# Configuration
cat > src/config/app.conf << 'EOF'
# Application Configuration
APP_NAME=MyApp
APP_VERSION=1.0
DEBUG=false
EOF

# Documentation
cat > docs/README.md << 'EOF'
# Web Project
Project documentation
EOF

# Web files
echo "<html><body>Hello World</body></html>" > www/html/index.html
echo "body { color: blue; }" > www/css/style.css
echo "console.log('Hello');" > www/js/app.js

Part 2: Appropriate Permissions​

Step 1: Script Permissions​

# Executable scripts for owner and group
chmod 750 src/scripts/*.sh

# Verify
ls -l src/scripts/

Step 2: Documentation Permissions​

# Read-only docs for everyone
chmod 644 docs/*

# Verify
ls -l docs/

Step 3: Configuration Permissions​

# Sensitive config: owner only
chmod 600 src/config/app.conf

# Verify
ls -l src/config/

Step 4: Web Permissions​

# Web files: read for everyone
chmod 644 www/html/* www/css/* www/js/*

# Web directories: executable for navigation
chmod 755 www www/html www/css www/js

# Link to config from bin
ln -s ../src/config/app.conf bin/config

# Link to logs from bin
ln -s ../logs bin/logs

# Link to current version
ln -s www www/current

# Verify
ls -l bin/

Step 2: Scripts in PATH​

# Create a link to the deployment script
sudo ln -s ~/projet-web/src/scripts/deploy.sh /usr/local/bin/web-deploy

# Test
web-deploy
cat ~/projet-web/logs/deploy.log

Part 4: Search and Maintenance​

Step 1: Search Script​

# Create a search script
cat > bin/search.sh << 'EOF'
#!/bin/bash
echo "Searching in the project..."
find . -name "$1" -type f
EOF

chmod +x bin/search.sh

# Use
cd ~/projet-web
./bin/search.sh "*.sh"

Step 2: Automatic Cleanup​

# Cleanup script
cat > src/scripts/cleanup.sh << 'EOF'
#!/bin/bash
# Delete logs > 30 days
find ../logs -name "*.log" -mtime +30 -delete
# Delete backups > 90 days
find ../backup -name "*.tar.gz" -mtime +90 -delete
echo "Cleanup done: $(date)"
EOF

chmod +x src/scripts/cleanup.sh

Part 5: Monitoring and Verification​

Step 1: Verification Script​

# Verification script
cat > bin/check-project.sh << 'EOF'
#!/bin/bash
echo "=== Project Verification ==="
echo
echo "Structure:"
tree -L 2
echo
echo "Script permissions:"
ls -l src/scripts/
echo
echo "Disk space:"
du -sh .
echo
echo "Recent files:"
find . -type f -mtime -1
EOF

chmod +x bin/check-project.sh

# Execute
cd ~/projet-web
./bin/check-project.sh

Step 2: Statistics​

# Count files
find ~/projet-web -type f | wc -l

# By type
find ~/projet-web -name "*.sh" | wc -l
find ~/projet-web -name "*.html" | wc -l
find ~/projet-web -name "*.log" | wc -l

Part 6: Security​

Step 1: Check Permissions​

# Find files with wide permissions
find ~/projet-web -type f -perm 777
find ~/projet-web -type f -perm -o+w # World writable

# Correct if necessary
find ~/projet-web -type f -perm 777 -exec chmod 755 {} \;

Step 2: Sensitive Files​

# Identify config files
find ~/projet-web -name "*.conf" -o -name "*.config"

# Check their permissions
find ~/projet-web -name "*.conf" -exec ls -l {} \;

# Ensure they are protected
find ~/projet-web -name "*.conf" -exec chmod 600 {} \;

Part 7: Automation​

Step 1: Full Deployment Script​

# Deployment script
cat > src/scripts/full-deploy.sh << 'EOF'
#!/bin/bash
set -e

PROJECT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
LOG="$PROJECT_DIR/logs/deploy-$(date +%Y%m%d).log"

{
echo "=== Deployment $(date) ==="

# Backup
echo "Creating backup..."
$PROJECT_DIR/src/scripts/backup.sh

# Verification
echo "Checking structure..."
$PROJECT_DIR/bin/check-project.sh

# Deployment (simulated)
echo "Deploying files..."
# cp -r $PROJECT_DIR/www/* /var/www/html/

echo "Deployment completed"
} >> "$LOG" 2>&1

echo "Deployment completed. Log: $LOG"
EOF

chmod +x src/scripts/full-deploy.sh

Final Verification​

Complete Checklist​

Ensure that:

  • Project structure created and organized
  • Appropriate permissions for each file type
  • Symbolic links configured correctly
  • Functional and executable scripts
  • Accessible documentation
  • Logs and backups configured
  • Security verified (no 777 permissions)
  • Functional automation scripts

Reflection Questions​

  1. Why organize a project with this structure?

    • Clear separation of responsibilities
    • Ease of maintenance
    • Improved security
  2. What is the importance of permissions in a project?

    • Protection of sensitive data
    • Access control
    • Error prevention
  3. How do symbolic links improve organization?

    • Simplified access
    • Version management
    • Reduction of duplication

Next Steps​

  • Module 4: Process Management
  • Module 5: Users and Permissions
  • Module 6: Shell Scripting

Lab created on: December 2024 - Tested on Ubuntu 24.04