Copy Files by Extension Recursively: 5 Methods
In this article
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)
- Open Command Prompt (press Win+R, type
cmd, press Enter). - Type the following command, replacing
sourceanddestinationwith your actual paths, and*.jpgwith your extension pattern:robocopy source destination *.jpg /s
- Press Enter. The
/sflag copies subdirectories (excluding empty ones). To include empty directories, use/einstead. - Robocopy will show a summary. Check for errors and verify the copied files.
Method 2: Using find and cp (Linux/macOS)
- Open Terminal.
- Navigate to the source directory if needed, or use full paths. The command below copies all .pdf files from
/sourceto/destination, preserving the folder structure:find /source -type f -name '*.pdf' -exec cp --parents {} /destination \;On macOS,cpdoesn't support--parentsby default. Usedittoinstead:find /source -type f -name '*.pdf' -exec ditto {} /destination/{} \; - Press Enter. The files are copied, keeping their relative paths under the destination.
- 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)
- Open Terminal.
- 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 -
- Press Enter. This copies all .txt files from /source to /destination, keeping subfolder structure.
- Note: The
--includeflag may not work on older tar versions; test first.
Method 4: Using PowerShell (Windows)
- Open PowerShell (search for it in Start menu).
- Run the following command, adjusting paths and extension:
Copy-Item -Path 'C:\source\*' -Filter '*.png' -Recurse -Destination 'C:\destination'
- Press Enter. This copies .png files recursively, but it also copies the folder structure from source to destination.
- 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)
- Open Terminal (or WSL on Windows).
- Run rsync with a filter to include only certain extensions:
rsync -avm --include='*.docx' --include='*/' --exclude='*' /source/ /destination/
- Press Enter. The
-mprunes 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
*.jpgwithout 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
*.jpgand*.pngseparately or use*.{jpg,png}in bash (not in all shells). - Use
--dry-runin 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
findto list matching files first. - Permission denied: Run the command with
sudoon 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.