← Back to all snippets
BASH

Batch Processing Files in a Directory with Case Statements

Automate batch operations on files in Bash using 'for' loops and 'case' statements, enabling efficient processing based on file types for web assets or data.

#!/bin/bash\n\nTARGET_DIR="./web_assets" # Specify the directory to process\n\n# Create dummy files for demonstration\nmkdir -p "$TARGET_DIR"\ntouch "$TARGET_DIR/logo.png"\ntouch "$TARGET_DIR/main.js"\ntouch "$TARGET_DIR/style.css"\ntouch "$TARGET_DIR/README.md"\n\nif [ ! -d "$TARGET_DIR" ]; then\n    echo "Error: Directory '$TARGET_DIR' not found."\n    exit 1\nfi\n\necho "Starting batch processing in '$TARGET_DIR'..."\n\n# Iterate over all files and directories in the target directory\nfor filepath in "$TARGET_DIR"/*; do\n    # Check if the current item is a regular file\n    if [ -f "$filepath" ]; then\n        filename=$(basename -- "$filepath")         # Get just the filename (e.g., 'logo.png')\n        extension="${filename##*.}"                  # Get the file extension (e.g., 'png')\n        filename_no_ext="${filename%.*}"            # Get filename without extension\n\n        echo "\nProcessing file: $filename (Full path: $filepath)"\n\n        # Use a case statement to perform actions based on file extension\n        case "${extension}" in\n            png|jpg|jpeg|gif) # Image files\n                echo "  -> Identified as an image. Optimizing..."\n                # Example: imagemagick convert "$filepath" -quality 80 "optimized_${filepath}"\n                # Example: optipng "$filepath"\n                ;;\n            js) # JavaScript files\n                echo "  -> Identified as a JavaScript file. Minifying..."\n                # Example: uglifyjs "$filepath" -o "min.${filepath}"\n                ;;\n            css) # CSS files\n                echo "  -> Identified as a CSS file. Compressing..."\n                # Example: sass --style compressed "$filepath" > "min.${filepath}"\n                ;;\n            md|txt) # Markdown or text files\n                echo "  -> Identified as a text/markdown file. Skipping specific processing."\n                ;;\n            *) # Other file types\n                echo "  -> Identified as an unknown file type '${extension}'. Skipping."\n                ;;
        esac\n    fi\ndone\n\necho "\nBatch processing complete."\n\n# Clean up dummy files and directory\nrm -f "$TARGET_DIR"/logo.png "$TARGET_DIR"/main.js "$TARGET_DIR"/style.css "$TARGET_DIR"/README.md\nrmdir "$TARGET_DIR"
How it works: This Bash script efficiently processes multiple files within a specified directory. It uses a `for` loop to iterate through each item, `basename` to extract the filename, and parameter expansion (`##*.` and `%.*`) to get the file extension and name without it. A `case` statement then applies different actions based on the file's extension, simulating tasks like image optimization, JavaScript minification, or CSS compression. This pattern is invaluable for web developers managing assets, performing build steps, or automating content processing tasks.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs