How to Copy Files While Preserving Directory Structure in Linux Using cp Command


2 views

When working with development projects (especially IDEs like Eclipse), we often need to copy configuration files while maintaining their original directory structure. The standard cp command behavior doesn't automatically preserve the parent directories unless explicitly instructed.

Consider this example directory structure:

a/
└── myProject/
    └── .project

When you run:

cp -r ./myProject/.project ../b

You'll get:

b/
└── .project

This loses the original myProject directory context, which is often problematic for IDE configurations that expect specific paths.

The most efficient solution is using cp --parents:

cp --parents myProject/.project ../b/

This will create:

b/
└── a/
    └── myProject/
        └── .project

For even better control, combine with other flags:

cp -v --parents myProject/.project ../b/

For more complex scenarios, rsync provides better control:

rsync -R myProject/.project ../b/

This preserves the relative path structure without creating additional parent directories.

When copying multiple project files:

find . -name '.project' -exec cp --parents {} ../b/ \;

Or using xargs for better performance:

find . -name '.project' | xargs -I {} cp --parents {} ../b/

For an Eclipse workspace containing multiple projects:

# From workspace root
find . -maxdepth 2 -name '.project' -exec cp --parents {} ~/backup/ \;

This preserves each project's directory structure in the backup location.


When working with project files like Eclipse's .project files, developers often need to maintain the original directory structure during copying operations. The standard cp command behavior doesn't automatically preserve parent directories, which can cause issues in build systems and version control scenarios.

The basic copy command:

cp -r ./myProject/.project ../b

This produces b/.project instead of the desired b/myProject/.project. The -r flag handles recursion but doesn't maintain the full path structure.

Here are three reliable approaches to solve this common development problem:

1. Using --parents Flag

The most straightforward solution is using GNU cp's --parents option:

cp --parents myProject/.project b/

This creates the complete directory structure relative to the current working directory.

2. Combining with find and xargs

For batch operations on multiple projects:

find . -name '.project' -print0 | xargs -0 -I {} cp --parents {} b/

3. Rsync Alternative

For more complex scenarios, rsync offers better control:

rsync -R myProject/.project b/

The -R flag preserves the relative path structure.

Let's say you're migrating multiple Eclipse projects from /old_workspace to /new_workspace while preserving structure:

cd /old_workspace
find . -name '.project' -exec cp --parents {} /new_workspace \;

For non-GNU systems (like BSD), use this alternative:

mkdir -p b/myProject && cp myProject/.project b/myProject/

Or create a shell function for repeated use:

cpr() {
  mkdir -p "$(dirname "$2")" && cp "$1" "$2"
}
cpr myProject/.project b/myProject/.project

For large directory trees, the rsync method is generally fastest due to its delta-transfer algorithm. The --parents approach is more efficient for smaller operations.