Portable Directory Copy Techniques — Preserve Paths, Permissions, and Timestamps
Copying directory trees portably while preserving paths, permissions, and timestamps is a common need for backups, migrations, and development workflows. This guide covers cross-platform techniques and practical commands to recreate directory structures accurately and reliably.
When to preserve what
- Paths: Keep the same relative layout so references and scripts still work.
- Permissions: Preserve execute/read/write and ownership where relevant (important for scripts and services).
- Timestamps: Preserve modification and access times for build systems, incremental backups, and audits.
General approach
- Decide whether you need to copy only the directory tree (no files) or the whole tree including files.
- Choose a tool that supports the preservation features you need and that’s available on target systems.
- Use an archive or specialized copy command that records metadata (permissions/timestamps).
- Verify results with checksums or metadata inspection.
Tools & commands (portable options)
Unix-like systems (Linux, macOS)
- Copy directory structure only:
find source_dir -type d -print | cpio -pdvmP target_dir- Creates directories and preserves permissions/timestamps.
- Copy entire tree, preserving permissions, timestamps, ownership:
rsync -aHAX –numeric-ids source_dir/ target_dir/-aarchive (recurses and preserves many attributes),-Hhard links,-AACLs,-Xxattrs. Use–numeric-idsfor portability across user ID mismatches.
- Using tar for portable archive and extract:
tar –preserve-permissions -cf archive.tar –directory=source_dir .tar -xpf archive.tar -C target_dir-ppreserves permissions. Use–numeric-ownerwhen restoring on systems where usernames differ.
Windows (PowerShell)
- Recreate directory tree only:
Get-ChildItem -Path .\sourcedir -Recurse -Directory | ForEach-Object { \(dest = \).FullName.Replace(‘C:\path\to\source_dir’,’D:\path\to\targetdir’) New-Item -ItemType Directory -Path \(dest -Force (Get-Item \).FullName).CreationTime = (Get-Item $_.FullName).CreationTime}- PowerShell preserves basic timestamps; permission models differ from Unix.
- Copy including metadata (NTFS permissions, timestamps):
robocopy C:\path\to\source_dir D:\path\to\target_dir /E /COPYALL /SEC /DCOPY:T/COPYALLcopies all file info (data, attributes, timestamps, security)./DCOPY:Tpreserves directory timestamps.
Cross-platform portable archive (recommended for transfers)
- Create gzipped tar on Unix:
tar -czpf archive.tar.gz –numeric-owner -C source_dir . - On Windows with WSL, Git Bash, or 7-Zip create/extract tar.gz. This approach bundles metadata and is easy to move between systems.
Preserving ownership and UID/GID portability
- Ownership is stored as numeric IDs in archives; when restored on a different system these IDs may map to different usernames. Use
–numeric-ownerwhen creating archives and–no-same-ownerwhen extracting if you want files to be owned by the extracting user. - For cross-account transfers, consider standardizing user IDs or using group-based permissions and ACLs.
Handling extended attributes, ACLs, and SELinux contexts
- rsync options
-Aand-Xhandle ACLs and xattrs respectively. - To preserve SELinux contexts: include
–selinuxor usecp -a –preserve=contexton systems that support it. - For Windows, NTFS ACLs are preserved by robocopy
/COPYALLor xcopy/O /X.
Verification
- Compare directory trees (structure and metadata):
- Unix:
find source_dir -printf “%P %M %u %g %TY-%Tm-%Td %TH:%TM:%TS\n” | sort > source_meta.txtand same for target, then diff. - Windows: use
Get-ChildItem -Recurse | Select FullName, Mode, LastWriteTimeand compare lists.
- Unix:
- Use checksums for file contents when files are copied:
rsync –checksumorshasum/Get-FileHash.
Practical examples
- Portable tree only (Unix):
cd source_dirfind . -type d -print0 | cpio -0pdvmP /path/to/target - Full copy preserving everything (Unix):
rsync -aHAX –numeric-ids /source/ /target/ - Windows robust copy:
robocopy C:\source D:\target /MIR /COPYALL /DCOPY:T
Troubleshooting tips
- Permission denied: run as an administrator/root or adjust capability settings (e.g., use
sudofor rsync/tar). - Different UID/GID mappings: use
–numeric-ids, accept remapped owners, or change ownership after transfer. - Long path issues on Windows: enable long path support or use tool options that handle extended paths.
Recommendations
- For cross-OS transfers, prefer tar/gzip archives created with options that preserve metadata.
- For incremental syncs between Unix hosts, use rsync with ACL/xattr options.
- For Windows-to-Windows transfers, use robocopy with
/COPYALLand/DCOPY:T. - Always verify with metadata listings or checksums after transfer.
If you want, I can generate exact commands tailored to your source and target paths and operating systems.
Leave a Reply