Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

📋 File Copier Script

A powerful bash script that recursively finds files with a specific name in a source folder and copies them to corresponding directories in a destination folder while preserving the directory structure.

Description

This script is perfect for copying configuration files (like .env, .config.json, etc.) across different folder hierarchies while maintaining the same relative paths. It automatically creates any missing directories in the destination folder with zero configuration needed.

⭐ Key Features

  • 🔍 Recursively searches for files by name in all subfolders
  • 📁 Preserves directory structure when copying
  • ➕ Automatically creates missing destination directories
  • 💬 Interactive prompts for user input
  • 🎨 Colored output for better readability
  • 📊 Summary report showing files found and copied
  • 🖥️ POSIX shell compatible

Platform Compatibility

Platform Support Notes
macOS ✅ Full Support Works natively on all macOS versions with Bash/Zsh
Linux ✅ Full Support Works on all Linux distributions with Bash/Sh
Windows ⚠️ WSL/Git Bash Use Windows Subsystem for Linux or Git Bash

Platform Differences

Both macOS and Linux are fully compatible with this script since they share the same Unix-like architecture:

  • Same core: Both use POSIX-compliant shells and Unix tools
  • Identical behavior: Output, directory structure, and copying mechanisms work identically
  • No platform-specific code: The script uses only standard Unix commands (find, mkdir, cp)
  • No special configuration: No need to adjust the script for different platforms

The only minor difference is the default shell:

  • macOS: Uses zsh by default (since macOS Catalina), but bash and sh work perfectly
  • Linux: Typically uses bash by default

Requirements

  • Shell: Bash, Sh, Zsh, or any POSIX-compatible shell
  • Unix Tools: find, mkdir, cp (standard on all macOS and Linux systems)
  • Permissions: Read access to source folder, write access to destination folder

🚀 Usage

Interactive Mode (Default - Prompts for all inputs)

./copier.sh

You'll be prompted for:

  1. Filename or pattern to search for (e.g., .env, *.docx, config.*)
  2. Source folder path
  3. Destination folder path

With Filename Argument

./copier.sh "*.PDF"

Prompts for source and destination folders only.

Fully Automated (All Arguments)

./copier.sh "*.PDF" ~/projects/source ~/projects/backup

No prompts - direct execution with all parameters specified.

Pattern Matching & Wildcards

The script supports flexible pattern matching:

Pattern Matches Example
.env Exact filename .env file only
*.PDF Any file ending with .PDF document.PDF, report.PDF
*.docx Any Word document file.docx, resume.docx
config.* Files starting with "config" config.json, config.yaml, config.xml
test* Files starting with "test" test.py, test-1.txt, test_data.csv

Case-Insensitive Search: All patterns are case-insensitive by default, so *.PDF matches .pdf, .Pdf, and .PDF files.

💡 Examples

Example 1: Copy .env Files with Directory Structure

graph TD
    A["📁 source/"] -->|Find .env| B["🔍 Copy with structure"]
    A --> C["📄 .env"]
    A --> D["📁 backend/"]
    D --> E["📄 .env"]
    D --> F["📁 auth/"]
    A --> G["📁 frontend/"]
    
    C -.->|Copy| H["📁 backup/"]
    E -.->|Copy| I["📁 backup/backend/"]
    H --> J["📄 .env"]
    I --> K["📄 .env"]
    
    style H fill:#e1f5e1
    style I fill:#e1f5e1
    style B fill:#fff3cd
Loading

Command:

./copier.sh ".env" ~/projects/source ~/projects/backup

Before:

~/projects/source/
├── .env                # Root level config
├── backend/
│   ├── .env            # Backend config
│   └── auth/
└── frontend/

After:

~/projects/backup/
├── .env                # Copied from root
├── backend/
│   └── .env            # Copied from backend
└── frontend/           # Created (no .env here)

Example 2: Copy Multiple Files Using Wildcards

Wildcards allow you to match multiple files with patterns. Case-insensitive matching is enabled by default!

# Copy all PDF files (case-insensitive - .PDF, .pdf, .Pdf all match)
./copier.sh "*.PDF" ~/Desktop/Pendrive ~/Desktop/backup

# Copy all Word documents
./copier.sh "*.docx" ~/Documents/source ~/Documents/backup

# Copy all files starting with "config"
./copier.sh "config.*" ~/app/config-source ~/app/config-backup

# Copy all tar.gz archives
./copier.sh "*.tar.gz" ~/archives/source ~/archives/destination

Example with output:

$ sh copier.sh "*.PDF" ~/Desktop/Pendrive ~/Desktop/mini_serv

Searching for '*.PDF' files in: /Users/vkatasonov/Desktop/Pendrive

✓ Copied: DELE.PDF → DELE.PDF
✓ Copied: subdirs/report.pdf → subdirs/report.pdf
✓ Copied: docs/manual.PDF → docs/manual.PDF

Summary:
Found: 3 '*.PDF' file(s)
Copied: 3 '*.PDF' file(s)

Key Features:

  • 🔍 Case-insensitive: *.PDF, *.pdf, and *.Pdf all match the same files
  • 📁 Directory structure preserved: Files keep their relative directory paths
  • 💾 Actual filenames preserved: Files are copied with their real names, not the pattern
  • Accurate summary: Counter displays number of files found and copied

Example 3: Copy Configuration Files

./copier.sh ".config.json" ~/app/source ~/app/destination

Example 4: Copy Database Setup Scripts

./copier.sh "setup.sql" ~/databases/old ~/databases/new

📤 Output Example

Enter filename to search for (e.g., .env):
.env
Enter source folder path:
~/Desktop/ft_transcendence_local_copy
Enter destination folder path:
~/Desktop/ft_transcendence

Searching for '.env' files in: /home/user/Desktop/ft_transcendence_local_copy

✓ Copied: .env → .env
✓ Copied: backend/.env → backend/.env
✓ Created directory: /home/user/Desktop/ft_transcendence/auth-service
✓ Copied: auth-service/.env → auth-service/.env

Summary:
Found: 3 '.env' file(s)
Copied: 3 '.env' file(s)

⚠️ Important Notes

  • Overwrites files: The script will replace existing files without prompting. Backup important files first!
  • 🏠 Tilde expansion: Paths like ~/Documents/project are fully supported
  • 🔐 Permissions: Script needs read access to source and write access to destination
  • 🔍 Hidden files: Works perfectly with hidden files (like .env)

🔧 Troubleshooting

Issue Solution
"Source folder does not exist" Check that the folder path is correct and accessible
"No files found" Verify filename is correct (include dot prefix for hidden files like .env). Test with: find ~/source -name ".env"
Permission denied Ensure read permissions on source and write permissions on destination. May need elevated privileges
Not working on macOS/Linux Script is fully compatible with both platforms. Ensure the script is executable: chmod +x copier.sh

📜 License

MIT License © 2026 vkatasonov

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages