User Agents

Web browsers and everyday web tech, explained

Copy Files by Extension Recursively: 5 Methods

· 5 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Copying files by extension recursively means finding every file with a specific ending (like .jpg or .pdf) in a folder and all its subfolders, then copying those files to a new location while preserving the folder structure or flattening it. This guide covers five reliable methods using built-in tools on Windows, macOS, and Linux — no extra software needed. You'll need a command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows) and basic familiarity with typing commands.

What You'll Need

  • A computer running Windows, macOS, or Linux
  • Access to a terminal or command prompt (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows)
  • Know the source folder path and the destination folder path
  • Know the file extension(s) you want to copy (e.g., .txt, .png)

Step-by-Step

Method 1: Using robocopy (Windows)

  1. Open Command Prompt (press Win+R, type cmd, press Enter).
  2. Type the following command, replacing source and destination with your actual paths, and *.jpg with your extension pattern:
    robocopy source destination *.jpg /s
  3. Press Enter. The /s flag copies subdirectories (excluding empty ones). To include empty directories, use /e instead.
  4. Robocopy will show a summary. Check for errors and verify the copied files.

Method 2: Using find and cp (Linux/macOS)

  1. Open Terminal.
  2. Navigate to the source directory if needed, or use full paths. The command below copies all .pdf files from /source to /destination, preserving the folder structure:
    find /source -type f -name '*.pdf' -exec cp --parents {} /destination \;
    On macOS, cp doesn't support --parents by default. Use ditto instead:
    find /source -type f -name '*.pdf' -exec ditto {} /destination/{} \;
  3. Press Enter. The files are copied, keeping their relative paths under the destination.
  4. To flatten (put all files in one folder), use -exec cp {} /destination/ \; but beware of name conflicts.

Method 3: Using tar and pipe (Linux/macOS)

  1. Open Terminal.
  2. Use tar to create an archive of matching files and pipe it to another tar to extract in the destination. This preserves structure and handles spaces in filenames:
    tar -C /source -cf - --include='*.txt' . | tar -C /destination -xf -
  3. Press Enter. This copies all .txt files from /source to /destination, keeping subfolder structure.
  4. Note: The --include flag may not work on older tar versions; test first.

Method 4: Using PowerShell (Windows)

  1. Open PowerShell (search for it in Start menu).
  2. Run the following command, adjusting paths and extension:
    Copy-Item -Path 'C:\source\*' -Filter '*.png' -Recurse -Destination 'C:\destination'
  3. Press Enter. This copies .png files recursively, but it also copies the folder structure from source to destination.
  4. To flatten, use Get-ChildItem with a loop:
    Get-ChildItem -Path 'C:\source' -Filter '*.png' -Recurse | Copy-Item -Destination 'C:\destination'

Method 5: Using rsync (Linux/macOS, also Windows via WSL)

  1. Open Terminal (or WSL on Windows).
  2. Run rsync with a filter to include only certain extensions:
    rsync -avm --include='*.docx' --include='*/' --exclude='*' /source/ /destination/
  3. Press Enter. The -m prunes empty directories. This preserves structure and is efficient for large sets.

Common Mistakes

  • Forgetting the trailing slash in rsync source paths — this changes behavior.
  • Using *.jpg without quotes in Unix commands — the shell may expand it in the current directory, not recursively.
  • Overwriting files with the same name when flattening — use unique names or a different destination.
  • Not checking permissions — you may get 'Permission denied' errors.
  • Confusing /s (subdirectories, excluding empty) with /e (including empty) in robocopy.

Tips

  • Test with a small sample first to verify the command works as expected.
  • Use quotes around paths with spaces: "C:\My Files".
  • For multiple extensions, use patterns like *.jpg and *.png separately or use *.{jpg,png} in bash (not in all shells).
  • Use --dry-run in rsync to see what would be copied without copying.
  • If you need to copy files from many subfolders into one folder, consider renaming files to avoid collisions, e.g., prefix with folder name.

Troubleshooting

  • No files copied: Check the extension spelling (case-sensitive on Linux/macOS). Use find to list matching files first.
  • Permission denied: Run the command with sudo on Unix (but be careful) or run Command Prompt as Administrator on Windows.
  • Command not found: Ensure robocopy is available (it is on Windows 7+), or install rsync on macOS via Homebrew if missing.
  • Files copied but structure wrong: For flattening, you need to specify a single destination folder without subfolder creation; check your command.
  • Spaces in filenames cause errors: Always quote paths and patterns in Unix commands.

Frequently Asked Questions

Can I copy multiple extensions at once?

Yes. In robocopy, you can list multiple patterns separated by spaces: robocopy src dst *.jpg *.png /s. In bash, use find with -o (or) condition: find . -type f \( -name '*.jpg' -o -name '*.png' \) -exec cp {} dest \;.

Does this preserve the folder structure?

Most methods (robocopy, find with --parents, tar, rsync) preserve the relative folder structure by default. To flatten, use a simple cp to a single destination without the --parents flag, or use the PowerShell loop method.

How do I copy files from a specific subfolder only?

Include the subfolder path in the source. For example, find /source/subfolder -type f -name '*.txt' will only search that subfolder and its children.

What if I want to copy files with no extension?

Use a pattern like *. (with a dot) or use -not -name '*.*' in find. For example: find . -type f -not -name '*.*' -exec cp {} dest \;.

Can I copy files larger than 4GB?

Yes, all these methods handle large files fine. The limitation is disk space, not the tool.

Is there a GUI way to do this?

On Windows, you can use File Explorer's search (search for *.jpg), select all results, and copy-paste, but that only works for the current folder and its subfolders if you enable 'Include subfolders' in search options. On macOS, use Finder's search with 'Name matches' and then drag to destination. However, these GUI methods may not preserve structure automatically.