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
Skip Hidden Files and Directories During Recursive Copy
Recursive copying is a common task in Linux systems, but sometimes we need to exclude hidden files and directories (dotfiles) during the process. Hidden files in Linux are those that start with a dot (.) and are typically used for configuration files and system data that should remain invisible during normal operations.
This tutorial demonstrates various methods to skip hidden files and directories when performing recursive copy operations using different Linux utilities.
Note ? Linux commands are case-sensitive.
Using cp Command with Shell Globbing
The standard cp command can be combined with shell globbing patterns to exclude hidden files during recursive copying.
Copy Only Visible Files and Directories
# Copy all visible files and directories recursively $ cp -r source_dir/[^.]* destination_dir/ # Alternative using extended globbing $ shopt -s extglob $ cp -r source_dir/!(.*) destination_dir/
The [^.]* pattern matches all files and directories that do not start with a dot, effectively excluding hidden items.
Using rsync Command
The rsync utility provides powerful exclusion capabilities and is ideal for selective copying operations.
Exclude Hidden Files with Pattern Matching
# Exclude all hidden files and directories $ rsync -av --exclude='.*' source_dir/ destination_dir/ # Copy from local to remote while excluding hidden files $ rsync -av --exclude='.*' ~/sample/ user@remote_host:/destination/
Multiple Exclusion Patterns
# Exclude hidden files and specific file types $ rsync -av --exclude='.*' --exclude='*.tmp' --exclude='*.log' source_dir/ dest_dir/
Example Output
sending incremental file list documents/ documents/report.txt documents/data.csv images/ images/photo1.jpg images/photo2.png sent 2,456,789 bytes received 145 bytes 98,277.36 bytes/sec total size is 2,445,678 speedup is 1.00
Using find Command with cp
The find command offers precise control over which files to copy by combining search criteria with execution.
# Find and copy only visible files
$ find source_dir -type f ! -name '.*' -exec cp --parents {} destination_dir/ \;
# Create directory structure and copy visible files
$ find source_dir -type f ! -path '*/\.*' -exec cp --parents {} dest_dir/ \;
Comparison of Methods
| Method | Advantages | Disadvantages | Best Use Case |
|---|---|---|---|
| cp with globbing | Simple, fast | Limited pattern control | Basic exclusion tasks |
| rsync | Powerful exclusions, remote support | More complex syntax | Network transfers, complex filtering |
| find + cp | Maximum control, precise filtering | Slower, complex commands | Custom selection criteria |
Using tar for Archive-based Copying
The tar command can also exclude hidden files during archive creation and extraction.
# Create archive excluding hidden files, then extract $ tar --exclude='.*' -czf backup.tar.gz source_dir/ $ tar -xzf backup.tar.gz -C destination_dir/ # Direct copy using tar (preserves permissions) $ tar --exclude='.*' -c source_dir/ | tar -x -C destination_dir/
Key Points
Hidden files start with a dot (.) and include system configuration files
The
.*pattern matches any file or directory beginning with a dotrsyncis most efficient for network operations and complex exclusionsShell globbing with
cpworks well for simple local operationsAlways test commands with
--dry-runoption when available
Conclusion
Excluding hidden files during recursive copying helps maintain clean backups and prevents accidental transfer of sensitive configuration data. The rsync command with --exclude='.*' provides the most flexible and reliable solution for most scenarios, while shell globbing with cp offers simplicity for basic tasks.
