If you’re a developer, you’re likely getting a lot of assistance from generative AI (GenAI). However, all AI platforms have maximum file size and limit the number of files you can upload. This can be frustrating if you’re working with a directory that may have dozens of small files that are organized well.
I have encountered this issue repeatedly when working on WordPress plugins or child theme modifications. I started to wonder if there was an easy way to combine all the code and organize it in text files that I could upload easily. And there was!
Shell Script to Combine Code FilesI developed this minify.sh shell script that can be run on Linux, Unix, or MacOS from a terminal command within your working folder. I’ve utilized this to upload code for review on Gemini, ChatGPT, and Claude with no issues.
```
``` ConfigurationThe script starts by defining some key parameters:
MAX_SIZE: This variable sets the maximum size (650KB) for each output file. This ensures that the combined files don’t become too large to handle.FILE_EXTENSIONS: This array lists the file types the script will process: PHP, JavaScript, HTML, and CSS.OUTPUT_PREFIX: This cleverly uses the current folder’s name as a prefix for the output files, keeping things organized.The Minification EngineThe heart of the script lies in the minify_content function. It takes a file as input and, based on its extension, applies a series of sed commands to strip out comments, unnecessary whitespace, and line breaks. This results in a compressed version of the original code.
File Handling and CombiningThe script uses associative arrays (declare -A) to keep track of file counters, current file sizes, and output file names for each file type. The process_file_type function iterates through each file type and performs the following steps:
find command to locate all files with the specified extension.minify_content function to compress the file.MAX_SIZE. If it does, a new output file is created.Putting It All TogetherThe main execution section of the script iterates through the FILE_EXTENSIONS array, calling the process_file_type function for each one. I even have it parse the different types of code in different files (PHP, JS, CSS, HTML) to make it easier. Working on a PHP issue? Just upload the file for PHP and ignore the rest. Finally, it lists the generated files with their sizes.
Combining minification and file combining significantly reduces file sizes and the number of files to upload to your favorite generative AI platform.
How To Use This Shell Script1. Save the Script: Copy the code provided in the article, paste it into a text editor, and save the file with a .sh extension (e.g., minify.sh).
2. Make it Executable: Open your terminal or command prompt, navigate to the directory where you saved the script using the cd command, and make the script executable by running: chmod +x minify.sh.
3. Navigate to the directory: Use the cd command to navigate to the directory with your code. One easy way to do this is to type cd and drag the folder from a Finder window to your terminal.
cd /Users/douglaskarr/Documents/my-folder
4. Run the Script: Execute the script by typing the path where minify.sh is located and hitting Enter.
/Users/douglaskarr/Documents/minify.sh
5. The script will output the following:
* Progress messages as it processes each file type.
* Information about original and minified file sizes.
* Notifications when new output files are created due to size limits.
* A list of the generated files with their sizes.
6. Locate the Output Files:
* The combined and minified files will be in the same directory where you ran the script.
* They will be named using the current folder name as a prefix, followed by the file type, and a counter (e.g., myfolder-js-1.txt, myfolder-css-2.txt).
Important Notes: File Types: The script currently handles PHP, JavaScript, HTML, and CSS files. You can modify the FILE_EXTENSIONS array in the script to include other file types if needed.
* Maximum File Size: The MAX_SIZE variable determines the maximum size of each output file. Adjust this value if you need larger or smaller combined files.
* Dependencies: This script relies on the sed command, which is typically available on Unix-like systems. If you’re on Windows, ensure you have a suitable environment like WSL, Cygwin, or Git Bash.
* Customization: You can further customize the minification rules within the minify_content function to match your specific requirements.
* Backup:* It’s always a good practice to back up your original files before running this script, just in case.
By following these simple steps, you can leverage the power of this Bash script to combine and minify your code for your AI platform to review!
©2024 DK New Media, LLC, All rights reserved | Disclosure
Originally Published on Martech Zone: Shell Script: Append And Minify Your Code Into Files for AI to Analyze