π οΈ Collect .env Files Script for macOS
This script searches your entire macOS filesystem for .env* files (e.g., .env, .env.local, .env.production, etc.), then copies them to your Desktop in a folder named envs, renaming each file to reflect its full path using dot notation.
You can run this from anywhere using the command: collect-envs
π Features
- β
Scans entire macOS (
/) for.env*files - β
Renames files using full folder path (e.g.,
Users.arif.Projects.my-app.env.local) - β
Copies to
~/Desktop/envs - β Automatically creates the target folder if it doesnβt exist
- β Replaces files if a matching name already exists
π Setup Instructions
1. Create the Script File
mkdir -p ~/bin
nano ~/bin/collect-envs
2. Paste the Following Script
#!/bin/bash
# Define the target directory
TARGET_DIR="$HOME/Desktop/envs"
# Create the target directory if it doesn't exist
mkdir -p "$TARGET_DIR"
echo "π Scanning filesystem for .env* files. This may take a while..."
# Find and process .env* files
sudo find / -type f -name ".env*" 2>/dev/null | while read file; do
# Remove leading slash
relative_path="${file#/}"
# Get folder path and env file name
folder_path="$(dirname "$relative_path")"
env_name="$(basename "$file")"
# Replace slashes with dots and remove leading dot from filename
new_name="${folder_path//\//.}.${env_name#.}"
# Full destination path
dest="$TARGET_DIR/$new_name"
# Copy the file (overwrite if exists)
cp -f "$file" "$dest"
done
echo "β
All .env* files copied to: $TARGET_DIR"
3. Make the Script Executable
chmod +x ~/bin/collect-envs
4. Add ~/bin to Your PATH (If Needed)
If itβs not already in your PATH, add it to your shell config file:
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
(Use .bashrc instead if you use Bash.)
π§ͺ Usage
Once installed, simply run:
collect-envs
π You may be prompted for your password due to
sudoaccess required for full system scanning.
π Example Output
If the original file is:
/Users/arif/Projects/my-app/.env.local
It will be copied and renamed as:
~/Desktop/envs/Users.arif.Projects.my-app.env.local
π§Ή Optional Improvements
- Exclude folders like
/Systemor/Volumesto reduce scan time - Log output to a
.txtfile - Automatically zip the final
envsfolder
β License
Feel free to copy, modify, and share!
π§ Contact
If you find this script useful or have suggestions for improvements, feel free to reach out!
- π devarif.me
- πΌ LinkedIn
- π» GitHub