Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Copy a directory to an existing directory Linux?
Copying directories is one of the most common operations in Linux system administration. The cp command is typically used for this purpose, but copying a directory into an existing directory requires specific techniques to handle content merging properly.
This article explores different methods to recursively copy directory contents into an existing target directory, both with and without overwriting existing files.
Understanding the Problem
When copying a directory to an existing directory, we need to distinguish between two scenarios:
Copying directory contents Merge files from source into target
Copying the entire directory Create a subdirectory within target
Consider this initial directory structure:
$ tree -a
.
??? src
? ??? .hidden.file
? ??? srcFile.txt
? ??? subSrc
? ??? subSrcFile.txt
??? target
??? originalTarget.file
Our goal is to merge the contents of src into the existing target directory while preserving the original files in target.
Using the cp Command
Method 1: Using -rT Options
The -T option treats the target as a regular directory rather than creating a subdirectory. Combined with -r for recursive copying:
$ cp -rT src target
$ tree -a
.
??? src
? ??? .hidden.file
? ??? srcFile.txt
? ??? subSrc
? ??? subSrcFile.txt
??? target
??? .hidden.file
??? originalTarget.file
??? srcFile.txt
??? subSrc
??? subSrcFile.txt
This successfully merges all files from src into target, including hidden files and subdirectories.
Method 2: Bash Globbing with Wildcard
Using src/* copies all visible files but misses hidden files:
$ cp -r src/* target
$ tree -a target
target
??? originalTarget.file
??? srcFile.txt
??? subSrc
??? subSrcFile.txt
The .hidden.file is missing because shell globbing ignores files starting with a dot.
Method 3: Including Hidden Files with Globbing
Enable dotglob temporarily to include hidden files:
$ ( shopt -s dotglob; cp -r src/* target )
$ tree -a target
target
??? .hidden.file
??? originalTarget.file
??? srcFile.txt
??? subSrc
??? subSrcFile.txt
Alternatively, use src/. to copy all contents including hidden files:
$ cp -r src/. target
Using the rsync Command
Basic rsync Syntax
The rsync command offers more control and efficiency for directory synchronization:
$ rsync -a src/ target
$ tree -a target
target
??? .hidden.file
??? originalTarget.file
??? srcFile.txt
??? subSrc
??? subSrcFile.txt
The trailing slash in src/ is crucial it tells rsync to copy the contents of src rather than the directory itself.
Overwriting Target Directory
To replace the target directory contents completely, use the --delete option:
$ rsync -a --delete src/ target
$ tree -a target
target
??? .hidden.file
??? srcFile.txt
??? subSrc
??? subSrcFile.txt
The --delete option removes files from the target that don't exist in the source, ensuring both directories are identical.
Comparison of Methods
| Method | Hidden Files | Preserves Target | Efficiency |
|---|---|---|---|
| cp -rT | Yes | Yes | Good |
| cp -r src/* | No | Yes | Good |
| cp -r src/. | Yes | Yes | Good |
| rsync -a src/ | Yes | Yes | Excellent |
| rsync --delete | Yes | No | Excellent |
Key Points
The
-Toption incptreats the destination as a file, not a directory parentTrailing slashes in rsync commands are significant for determining copy behavior
Hidden files require special handling with globbing patterns
The
rsynccommand provides more control and better performance for large transfers
Conclusion
Copying directories to existing directories can be accomplished using both cp and rsync commands. The rsync method is generally preferred for its efficiency and fine-grained control over file synchronization. Choose the appropriate method based on whether you need to preserve existing files or completely replace the target directory contents.
