How to Portable Copy Directory Structure Without Copying Files
Copying a directory tree without its files is useful when you want to recreate folder layouts on another machine, prepare empty project skeletons, or share a structure without large data. This guide shows simple cross-platform methods, explains common options, and offers portable solutions that work from USB drives or minimal environments.
1. What “portable copy” means here
Portable copy = recreate the same folder hierarchy (folders and subfolders) somewhere else without copying regular file contents. The result is an empty directory tree that preserves names, nesting, and optionally timestamps, permissions, and empty placeholder files.
2. Key considerations
- Platform differences: commands differ between Windows, macOS, and Linux.
- Preserve: names and nesting are always preserved; timestamps and permissions may require extra options and elevated rights.
- Portability: use built-in shell commands or a single small script/binary so you can run from USB or constrained systems.
3. Cross-platform approaches
Linux / macOS (POSIX)
Method A — using find + cpio (fast, preserves structure):
cd /path/to/sourcefind . -type d | cpio -pdmv /path/to/destination
-type dselects directories only.cpio -pdmvcopies directories, creates parents, preserves mode and timestamps.
Method B — using rsync:
rsync -a -f”+/” -f”- *” /path/to/source/ /path/to/destination/
-apreserves attributes; filter rules include directories, exclude files.
Method C — using find + mkdir (very portable):
cd /path/to/sourcefind . -type d -print0 | while IFS= read -r -d “ dir; do mkdir -p “/path/to/destination/\(dir"; done</code></pre></div></div><ul><li>No special tools required beyond find and mkdir.</li></ul><h4>Windows (PowerShell)</h4><p>Method A — PowerShell (recursive):</p><div><div></div><div><div><button disabled="" title="Download file" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button disabled="" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div><pre><code>\)src = “C:\path\to\source”\(dst = "D:\path\to\destination"Get-ChildItem -Path \)src -Directory -Recurse | ForEach-Object { \(rel = \).FullName.Substring(\(src.Length).TrimStart('\') \)target = Join-Path \(dst \)rel New-Item -ItemType Directory -Path \(target -Force | Out-Null}</code></pre></div></div><p>Method B — robocopy (very simple):</p><div><div></div><div><div><button disabled="" title="Download file" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button disabled="" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div><pre><code>robocopy "C:\path\to\source" "D:\path\to\destination" /e /xf *.*</code></pre></div></div><ul><li><code>/e</code> copies subdirectories including empty ones; <code>/xf *.*</code> excludes files. Note: robocopy may still create zero-byte files for some attributes—verify after.</li></ul><h3>4. Preserving metadata (optional)</h3><ul><li>POSIX: use <code>cpio -pdm</code> or <code>rsync -a</code> to keep permissions and timestamps. Owner preservation may require root.</li><li>Windows: PowerShell’s New-Item will create folders but not preserve NTFS ACLs automatically; use advanced scripting (Get-Acl / Set-Acl) if needed.</li></ul><h3>5. Creating placeholders instead of empty folders</h3><p>If you need placeholder files (e.g., to keep empty folders tracked in VCS), create a small file in each directory: POSIX example:</p><div><div></div><div><div><button disabled="" title="Download file" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button disabled="" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div><pre><code>find /path/to/destination -type d -exec touch {}/.gitkeep \;</code></pre></div></div><p>PowerShell example:</p><div><div></div><div><div><button disabled="" title="Download file" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M8.375 0C8.72 0 9 .28 9 .625v9.366l2.933-2.933a.625.625 0 0 1 .884.884l-2.94 2.94c-.83.83-2.175.83-3.005 0l-2.939-2.94a.625.625 0 0 1 .884-.884L7.75 9.991V.625C7.75.28 8.03 0 8.375 0m-4.75 13.75a.625.625 0 1 0 0 1.25h9.75a.625.625 0 1 0 0-1.25z"></path></svg></button><button disabled="" title="Copy Code" type="button"><svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="14" height="14" color="currentColor"><path fill="currentColor" d="M11.049 5c.648 0 1.267.273 1.705.751l1.64 1.79.035.041c.368.42.571.961.571 1.521v4.585A2.31 2.31 0 0 1 12.688 16H8.311A2.31 2.31 0 0 1 6 13.688V7.312A2.31 2.31 0 0 1 8.313 5zM9.938-.125c.834 0 1.552.496 1.877 1.208a4 4 0 0 1 3.155 3.42c.082.652-.777.968-1.22.484a2.75 2.75 0 0 0-1.806-2.57A2.06 2.06 0 0 1 9.937 4H6.063a2.06 2.06 0 0 1-2.007-1.584A2.75 2.75 0 0 0 2.25 5v7a2.75 2.75 0 0 0 2.66 2.748q.054.17.123.334c.167.392-.09.937-.514.889l-.144-.02A4 4 0 0 1 1 12V5c0-1.93 1.367-3.54 3.185-3.917A2.06 2.06 0 0 1 6.063-.125zM8.312 6.25c-.586 0-1.062.476-1.062 1.063v6.375c0 .586.476 1.062 1.063 1.062h4.374c.587 0 1.063-.476 1.063-1.062V9.25h-1.875a1.125 1.125 0 0 1-1.125-1.125V6.25zM12 8h1.118L12 6.778zM6.063 1.125a.813.813 0 0 0 0 1.625h3.875a.813.813 0 0 0 0-1.625z"></path></svg></button></div></div><div><pre><code>Get-ChildItem -Path D:\path\to\destination -Directory -Recurse | ForEach-Object { New-Item -Path \).FullName.gitkeep -ItemType File -Force | Out-Null }
6. Making a portable single-file script
- POSIX: create a shell script that accepts source and destination, bundled on a USB drive.
- Windows: use a PowerShell script (.ps1). Optionally compile to an executable with tools like PS2EXE for ultra-portability.
Example minimal POSIX script (save as copy-tree.sh):
#!/bin/shsrc=”\(1"; dst="\)2”cd “\(src" || exit 1find . -type d -print0 | while IFS= read -r -d '' d; do mkdir -p "\)dst/$d”; done
Usage: ./copy-tree.sh /media/usb/source /target/path
7. Quick checklist before running
- Confirm there’s enough space for folders and metadata.
- Run a dry run where possible (preview list of directories).
- Check permissions and run as admin/root only if you need to
Leave a Reply