How To Copy Multiple Files In Linux : Using Wildcard Characters For Selection

Handling several files at once in Linux means using the cp command with a list of source names separated by spaces. Understanding how to copy multiple files in linux is a fundamental skill that saves time and reduces errors. This guide walks you through every practical method, from basic commands to advanced techniques.

Copying files one by one is slow and tedious. Linux offers powerful ways to duplicate many files at once, whether they share a pattern, are in different directories, or need to be renamed. You will learn the exact commands and options to use.

How To Copy Multiple Files In Linux

The core command for copying files is cp. To copy multiple files, you list them as source arguments, followed by the destination directory. The basic syntax is:

cp file1.txt file2.txt file3.txt /target/directory/

This copies all listed files into the specified folder. The destination must be a directory, not a file. If you try to copy multiple files to a single file name, Linux will show an error.

Basic Syntax And Examples

Let’s start with a simple example. Suppose you have three files: report.pdf, summary.docx, and data.csv. You want to copy them to a folder called backup.

cp report.pdf summary.docx data.csv backup/

After running this command, the backup directory contains copies of all three files. The original files remain unchanged.

You can also use relative or absolute paths for the source files. For instance:

cp ~/Documents/notes.txt ~/Downloads/image.png ./backup/

This copies files from different locations into the current directory’s backup folder.

Using Wildcards For Pattern Matching

Wildcards make copying multiple files much faster. The asterisk (*) matches any number of characters, while the question mark (?) matches a single character.

To copy all text files in the current directory:

cp *.txt /target/folder/

This copies every file ending with .txt. You can combine patterns:

cp report* /archive/

This copies all files starting with “report”, regardless of extension.

Be careful with wildcards. If no files match the pattern, the command will throw an error. Always double-check the list by running ls *.txt first.

More Wildcard Examples

  • cp file?.log /logs/ – copies file1.log, file2.log, but not file10.log
  • cp [abc]*.jpg /images/ – copies files starting with a, b, or c and ending with .jpg
  • cp [!0-9]*.pdf /docs/ – copies PDFs that don’t start with a digit

Copying Files From Multiple Directories

Sometimes your files are scattered across different folders. You can specify full paths for each source:

cp /home/user/docs/report.txt /var/log/error.log /tmp/backup/

This copies report.txt from docs, error.log from log, and places both in /tmp/backup. The destination must exist beforehand.

If you need to copy files from many directories, consider using a loop or find command. For example:

for file in /path1/*.txt /path2/*.csv; do cp "$file" /target/; done

This loops through each matching file and copies it individually. It’s slower but gives you more control.

Advanced Copy Techniques

Beyond basic copying, Linux offers options to preserve attributes, show progress, and handle errors. These become essential when dealing with large numbers of files.

Preserving File Attributes With -P

The -p flag preserves timestamps, ownership, and permissions. This is crucial when copying configuration files or backups.

cp -p *.conf /etc/backup/

Without -p, copied files get the current date and time. With -p, the original modification times are kept.

Verbose Output With -V

To see what the command is doing, add -v (verbose). It prints each file as it is copied.

cp -v *.html /public_html/

Output might look like:

'index.html' -> '/public_html/index.html'
'about.html' -> '/public_html/about.html'

This helps verify that all intended files were copied, especially when using wildcards.

Interactive Mode With -I

If you might overwrite existing files, use -i to prompt before each overwrite.

cp -i *.jpg /pictures/

Linux asks: “cp: overwrite ‘/pictures/photo.jpg’?” Type y or n for each file. This prevents accidental data loss.

Recursive Copy With -R Or -R

To copy directories along with their contents, use -r (recursive). This copies all files and subdirectories.

cp -r /source_folder/ /destination_folder/

Note: If the destination exists, the source folder is copied inside it. If it doesn’t, a new folder is created.

For copying multiple directories at once:

cp -r dir1 dir2 dir3 /backup/

This copies each directory tree into the backup folder.

Using Find And Xargs For Bulk Copies

When you have hundreds or thousands of files, the find command combined with xargs is extremely efficient. It avoids command line length limits.

To find all PDF files modified in the last 7 days and copy them:

find . -name "*.pdf" -mtime -7 -print0 | xargs -0 cp -t /target/

The -print0 and -0 options handle filenames with spaces or special characters. The -t flag specifies the target directory before the source files.

Alternatively, you can use find with -exec:

find . -name "*.log" -exec cp {} /logs/ \;

This runs cp for each file individually, which is slower but simpler for small sets.

Copying Files Based On Size Or Type

You can filter files by size. To copy files larger than 10MB:

find . -size +10M -exec cp {} /large_files/ \;

To copy only regular files (not directories):

find . -type f -name "*.txt" -exec cp {} /texts/ \;

These commands give you surgical control over which files get copied.

Using Rsync For Efficient Bulk Copying

Rsync is a powerful tool for copying large numbers of files. It only transfers differences, making subsequent copies faster.

Basic rsync command to copy multiple files:

rsync -av source1.txt source2.txt /destination/

The -a flag (archive) preserves permissions, timestamps, and other attributes. The -v flag shows progress.

To copy an entire directory tree:

rsync -av /source/dir/ /destination/dir/

Note the trailing slash on the source. It means “copy the contents of this directory.” Without it, the directory itself is copied.

Rsync With Include And Exclude Patterns

You can selectively copy files using --include and --exclude:

rsync -av --include="*.txt" --exclude="*" /source/ /destination/

This copies only .txt files, excluding everything else. Order matters: include patterns are checked first.

To copy multiple file types:

rsync -av --include="*.jpg" --include="*.png" --exclude="*" /source/ /destination/

Rsync is ideal for backups and syncing large projects because it resumes interrupted transfers.

Copying And Renaming Simultaneously

Sometimes you need to copy files and give them new names. The basic cp command doesn’t support renaming multiple files at once directly. You need a loop.

To add a prefix to each file during copy:

for f in *.txt; do cp "$f" "backup_$f"; done

This creates backup_file1.txt, backup_file2.txt, etc.

To change extensions:

for f in *.html; do cp "$f" "${f%.html}.htm"; done

This copies index.html to index.htm, about.html to about.htm.

For more complex renaming, use the rename command after copying, or combine with sed.

Handling Spaces And Special Characters

Filenames with spaces can break your commands. Always quote them or use escape characters.

Bad: cp my file.txt /target/ (tries to copy two files: “my” and “file.txt”)

Good: cp "my file.txt" /target/ or cp my\ file.txt /target/

When using wildcards, spaces are handled automatically. But if you list files manually, quoting is essential.

For filenames with special characters like $ or \, use single quotes:

cp 'budget$2024.xlsx' /finance/

This prevents shell interpretation.

Common Errors And Troubleshooting

Even experienced users encounter issues. Here are frequent problems and solutions.

“Cp: Target Is Not A Directory”

This occurs when you list multiple source files but the last argument is a file, not a directory. Ensure the destination exists and is a folder.

Fix: Create the directory first: mkdir -p /target/

“Argument List Too Long”

When copying thousands of files, the command line becomes too long. Use find ... -exec or xargs instead.

Fix: find . -name "*.txt" -exec cp {} /target/ \;

Permission Denied

You lack write access to the destination. Use sudo if appropriate, or change permissions.

Fix: sudo cp *.conf /etc/ (use with caution)

Overwriting Without Warning

By default, cp overwrites existing files silently. Use -i for interactive mode or -n to never overwrite.

Fix: cp -n *.jpg /pictures/ (no overwrite)

Automating Copies With Scripts

For repetitive tasks, write a simple bash script. Save the following as copy_files.sh:

#!/bin/bash
cp -v "$@" /backup/

Make it executable: chmod +x copy_files.sh

Then run: ./copy_files.sh file1.txt file2.pdf

This copies any files you pass as arguments to /backup/. You can expand the script with error checking and logging.

For scheduled backups, combine with cron. Edit your crontab with crontab -e and add:

0 2 * * * /home/user/copy_files.sh /home/user/documents/*.docx

This runs the script daily at 2 AM.

Comparing Performance: Cp Vs Rsync Vs Find

For small numbers of files (under 100), cp is fastest. For thousands of files, rsync with archive mode is efficient because it checks differences. Find with xargs is best when you need complex filtering.

Here’s a quick comparison:

  • cp: Best for simple, one-time copies of few files
  • rsync: Ideal for backups, syncing, and large transfers
  • find + xargs: Perfect for selective copies based on criteria

Test each method with your specific workload. Use time command to measure: time cp -r source dest

Practical Use Cases

Let’s apply these techniques to real scenarios.

Backing Up Configuration Files

You need to copy all .conf files from /etc to a backup folder:

sudo cp -v /etc/*.conf /backup/configs/

Use sudo because /etc files require root access. The -v flag confirms each copy.

Organizing Photos By Date

Copy all JPEG files from a camera folder to a dated subfolder:

mkdir -p /photos/2025/03
cp /camera/DCIM/*.jpg /photos/2025/03/

Add -p to preserve original timestamps.

Deploying Website Files

Copy updated HTML and CSS files to the web server directory:

cp -u *.html *.css /var/www/html/

The -u flag only copies files that are newer than existing ones, saving time.

Frequently Asked Questions

Q: How do I copy multiple files with different extensions?
A: Use multiple wildcards: cp *.txt *.pdf *.jpg /target/. Or list them explicitly.

Q: Can I copy multiple files to multiple destinations?
A: No, cp only supports one destination. Use a loop or rsync for multiple targets.

Q: What is the difference between cp and rsync for multiple files?
A: Cp is simpler and faster for small sets. Rsync offers compression, delta transfers, and better error handling for large or remote copies.

Q: How do I copy multiple files without overwriting existing ones?
A: Use cp -n (no clobber) or cp -u (update only newer files).

Q: Why does my cp command say “omitting directory”?
A: You tried to copy a directory without -r. Add -r or -a to copy directories recursively.

Summary And Best Practices

Mastering how to copy multiple files in linux boosts your productivity. Start with basic cp and wildcards, then graduate to rsync for complex tasks. Always test commands with -v or --dry-run (rsync) before executing.

Remember these key points:

  • Use wildcards (*, ?) for pattern matching
  • Quote filenames with spaces
  • Use -i or -n to prevent accidental overwrites
  • Use -p to preserve attributes
  • For large sets, use find with xargs or rsync

Practice these commands in a test directory. Create dummy files with touch file{1..10}.txt and experiment. The more you use them, the more natural they become.

Linux gives you immense control over file operations. With these techniques, you can handle any bulk copy task efficiently and safely.