Three Efficient Methods for Copying Directory Structures in Linux

Dec 04, 2025 · Programming · 9 views · 7.8

Keywords: Linux directory copy | find command | rsync filtering

Abstract: This article comprehensively explores three practical methods for copying directory structures without file contents in Linux systems. It begins with the standard solution based on find and xargs commands, which generates directory lists and creates directories in batches, suitable for most scenarios. The article then analyzes the direct execution approach using find with -exec parameter, which is concise but may have performance issues. Finally, it discusses using rsync's filtering capabilities, which better handles special characters and preserves permissions. Through code examples and performance comparisons, the article helps readers choose the most appropriate solution based on specific needs, particularly providing optimization suggestions for copying directory structures of multi-terabyte file servers.

Background of Directory Structure Copying Requirements

When managing large file systems, there is often a need to copy directory structures without including actual file content. This requirement is particularly common in scenarios such as backing up multi-terabyte file servers, setting up test environments, or analyzing directory structures. When source directories are mounted via CIFS to Linux systems, traditional file copying commands cannot meet the need for structure-only copying, requiring specialized solutions.

Standard Method Using find and xargs

The most reliable approach involves two steps: first generating a list of all directories using the find command, then creating directories in batches through xargs. The core advantage of this method lies in its stability and predictability.

First step, generate directory list:

find . -type d > dirs.txt

This command recursively finds all directories (-type d) starting from the current directory and outputs the results to dirs.txt file. The dot represents the current directory and can be replaced with specific source directory paths as needed.

Second step, batch create directories:

xargs mkdir -p < dirs.txt

Here xargs reads the directory list from dirs.txt file and creates all directories through mkdir -p command. The -p parameter ensures that multi-level directories can be created without failure due to missing parent directories.

Advantages of this method include:

Direct Execution Method with find Command

As a supplementary approach, mkdir command can be directly executed using -exec parameter in find command:

cd /path/to/directories &&
find . -type d -exec mkdir -p -- /path/to/backup/{} \;

This method combines two steps into a single command line, eliminating intermediate files. However, it has some potential issues:

Filtering Method Using rsync

For scenarios requiring permission preservation or handling complex paths, rsync provides a more elegant solution:

rsync -av -f"+ */" -f"- *" "$source" "$target"

This command utilizes rsync's filtering capabilities:

Advantages of rsync method include:

Performance Comparison and Selection Recommendations

For copying directory structures of multi-terabyte file servers, it's recommended to choose appropriate methods based on specific scenarios:

  1. Standard Method (find + xargs): Most suitable for most production environments, particularly when precise control over copying process or batch operations are needed. Its separated steps design facilitates debugging and monitoring.
  2. Direct Execution Method: Suitable for scenarios with fewer directories or temporary tasks. Although the code is concise, it may be less efficient when handling large numbers of directories.
  3. rsync Method: Optimal choice when complete directory attribute preservation or regular synchronization is required. Its filtering mechanism is particularly suitable for handling complex directory structures.

In practical applications, these methods can be combined for optimization. For example, first generate directory list using find, then filter the list as needed, and finally create directories using xargs. For CIFS-mounted file systems, it's recommended to first test with small-scale directory structures locally to ensure command behavior meets expectations.

Considerations and Best Practices

When executing directory structure copying, the following key points should be noted:

By understanding the principles and applicable scenarios of these methods, system administrators can efficiently complete directory structure copying tasks, providing reliable foundations for file server management and maintenance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.