How To Copy All Files From One Directory To Another In Linux : Bulk File Move With Wildcards

Copying all files from one directory to another in Linux is a common task that every user needs to master. If you are wondering how to copy all files from one directory to another in linux, you have come to the right place. This guide will walk you through multiple methods, from simple commands to advanced techniques, ensuring you can move your data efficiently and safely.

Linux offers several powerful tools for file copying, each with its own strengths. Whether you are a beginner or an experienced user, understanding these commands will save you time and prevent errors. Let’s start with the most straightforward approach and build up to more complex scenarios.

How To Copy All Files From One Directory To Another In Linux

The core command for copying files in Linux is cp. This command is short for “copy” and is available on virtually every Linux distribution. To copy all files from one directory to another, you use the cp command with specific options.

Here is the basic syntax: cp [options] source_directory/* destination_directory/. The asterisk * is a wildcard that matches all files in the source directory. This includes regular files but not hidden files (those starting with a dot).

For example, to copy everything from /home/user/documents to /home/user/backup, you would run: cp /home/user/documents/* /home/user/backup/. This command copies all visible files, but it does not copy subdirectories or their contents.

Copying Files And Subdirectories Recursively

To copy entire directories including their subdirectories and all files within them, you need the -r or -R option (recursive). The command becomes: cp -r source_directory/* destination_directory/. This tells cp to copy everything recursively.

If you want to preserve file permissions, timestamps, and ownership, add the -p option: cp -rp source_directory/* destination_directory/. This is useful for backups where you need to maintain file attributes.

For a more comprehensive copy that includes hidden files, use the -a (archive) option: cp -a source_directory/. destination_directory/. The dot at the end of the source path ensures all files, including hidden ones, are copied.

Using The Rsync Command For Advanced Copying

While cp is great for simple tasks, rsync is a more powerful tool for copying files. It is especially useful for large transfers or when you need to sync directories. Rsync copies only the differences between source and destination, making it faster for repeated operations.

The basic rsync command to copy all files is: rsync -av source_directory/ destination_directory/. The -a option stands for archive mode (preserving attributes), and -v enables verbose output so you can see what is being copied.

Rsync also handles interruptions gracefully. If a transfer fails, you can run the same command again, and it will resume from where it left off. This is a lifesaver for large file transfers over unreliable connections.

To include hidden files with rsync, the command is the same: rsync -av source_directory/ destination_directory/. The trailing slash on the source directory is crucial—it tells rsync to copy the contents of the directory, not the directory itself.

Copying Files With The Find Command

Sometimes you need to copy only specific types of files, such as all .txt files. The find command combined with cp or rsync gives you fine-grained control. For example, to copy all text files from one directory to another: find source_directory -name "*.txt" -exec cp {} destination_directory/ \;

This command finds all files matching the pattern and executes the copy command for each one. The {} is a placeholder for the found file, and \; terminates the exec command.

You can also use find with rsync for more efficient copying: find source_directory -name "*.txt" -print0 | rsync -av --files-from=- --from0 ./ destination_directory/. This approach is faster for large numbers of files.

Handling Hidden Files And Dotfiles

Hidden files (those starting with a dot) are not included when you use the * wildcard. To copy all files including hidden ones, you have a few options. The simplest is to use the cp -a command with a dot source: cp -a source_directory/. destination_directory/

Another method is to use the shopt -s dotglob option in bash. This makes the * wildcard match hidden files as well. Run shopt -s dotglob before your copy command, then use cp source_directory/* destination_directory/. Afterward, you can disable dotglob with shopt -u dotglob.

Rsync handles hidden files automatically when you use the trailing slash syntax: rsync -av source_directory/ destination_directory/. This is the most reliable method for copying all files, including dotfiles.

Copying Files With Progress And Verification

When copying large amounts of data, you may want to see progress or verify the copy. The cp command does not have a built-in progress indicator, but you can use the pv (pipe viewer) tool. Install it with your package manager, then use: tar cf - source_directory | pv | tar xf - -C destination_directory

Rsync has a progress option: rsync -av --progress source_directory/ destination_directory/. This shows each file being copied and the overall progress.

For verification, you can use the --checksum option with rsync: rsync -avc source_directory/ destination_directory/. This compares file checksums instead of timestamps and sizes, ensuring data integrity.

Copying Files Over A Network

Linux makes it easy to copy files between remote systems. The scp command (secure copy) uses SSH for encrypted transfers. To copy all files from a local directory to a remote server: scp -r source_directory/* user@remote_host:/path/to/destination/

Rsync is even better for network transfers because it compresses data and resumes interrupted transfers: rsync -avz source_directory/ user@remote_host:/path/to/destination/. The -z option enables compression.

For copying from a remote server to your local machine, reverse the order: rsync -avz user@remote_host:/path/to/source/ destination_directory/

Common Mistakes And How To Avoid Them

One common mistake is forgetting the trailing slash on the source directory with rsync. Without it, rsync copies the directory itself into the destination, not its contents. Always double-check your paths.

Another error is overwriting files unintentionally. The cp command will overwrite existing files without warning unless you use the -i (interactive) option: cp -i source/* destination/. This prompts you before each overwrite.

Using the * wildcard without proper escaping can also cause issues if filenames contain spaces or special characters. Always quote your paths or use the -- separator to avoid glob expansion problems.

Copying Files With Preserved Permissions

Preserving file permissions is crucial for system backups or when copying configuration files. The cp -p option preserves the original file’s modification time, access time, and permissions. For complete preservation, use cp -a which also preserves ownership and ACLs.

Rsync’s archive mode (-a) does the same thing. It preserves permissions, ownership, timestamps, and symbolic links. This makes rsync the preferred tool for system backups and migrations.

If you need to preserve extended attributes or ACLs, use cp --preserve=all or rsync -A -X (for ACLs and extended attributes respectively).

Copying Files With Exclusions

Sometimes you want to copy all files except certain ones. Both cp and rsync support exclusion patterns. With rsync, use the --exclude option: rsync -av --exclude='*.log' source_directory/ destination_directory/

You can also use multiple exclusions: rsync -av --exclude='*.log' --exclude='temp/' source_directory/ destination_directory/

For cp, you can combine it with find and grep to exclude files: find source_directory -type f ! -name '*.log' -exec cp {} destination_directory/ \;

Copying Files Using Graphical Tools

While command-line methods are powerful, Linux also offers graphical file managers like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE). You can simply open two windows, select all files (Ctrl+A), and drag them to the destination. Right-click and choose “Copy” or “Move” for more options.

These tools are user-friendly and great for occasional use. However, they lack the automation and scripting capabilities of command-line tools. For repetitive tasks, stick with cp or rsync.

Scripting File Copies For Automation

You can automate file copying with bash scripts. Here is a simple script that copies all files from a source to a destination, logging the results:

#!/bin/bash
SOURCE="/path/to/source"
DEST="/path/to/destination"
rsync -av --log-file=/var/log/copy.log "$SOURCE/" "$DEST/"
echo "Copy completed on $(date)" >> /var/log/copy.log

Save this as copy_script.sh, make it executable with chmod +x copy_script.sh, and run it whenever needed. You can schedule it with cron for regular backups.

Troubleshooting Common Issues

If you get a “Permission denied” error, you likely do not have read access to the source or write access to the destination. Use sudo if necessary, but be careful with system files.

Another issue is running out of disk space. Check available space with df -h before starting a large copy. Rsync’s --dry-run option lets you see what would be copied without actually copying: rsync -av --dry-run source/ dest/

If filenames contain special characters, use quotes around paths or escape characters with backslashes. The -0 option with find and rsync handles null-separated filenames safely.

Performance Tips For Large File Transfers

For very large transfers, consider these tips:

  • Use rsync over cp for incremental copies
  • Enable compression with -z for network transfers
  • Limit bandwidth with --bwlimit=RATE to avoid saturating your connection
  • Use --partial to keep partially transferred files if the transfer is interrupted
  • Run transfers during off-peak hours to reduce system load

For local transfers, rsync is still faster than cp for large directories because it only copies changed files. Use rsync -av --delete to make the destination an exact mirror of the source.

Security Considerations When Copying Files

Always verify the integrity of copied files, especially for critical data. Use checksums: md5sum source/file destination/file to compare. Rsync’s -c option does this automatically.

When copying over a network, use SSH-based tools like scp or rsync over SSH. Avoid unencrypted protocols like FTP for sensitive data.

Be cautious when copying files as root. A small mistake could overwrite system files. Always double-check your source and destination paths.

Frequently Asked Questions

How Do I Copy All Files Including Hidden Ones In Linux?

Use cp -a source_directory/. destination_directory/ or rsync -av source_directory/ destination_directory/. Both methods include hidden files (dotfiles).

What Is The Difference Between Cp And Rsync?

cp is simpler and faster for small copies, while rsync is more powerful for large or incremental transfers. Rsync can resume interrupted transfers, compress data, and sync directories efficiently.

Can I Copy Files From One Directory To Another Without Overwriting Existing Files?

Yes, use cp -n source/* destination/ to avoid overwriting. With rsync, use --ignore-existing option.

How Do I Copy Only Specific File Types?

Use find source -name "*.txt" -exec cp {} destination/ \; or rsync -av --include='*.txt' --exclude='*' source/ destination/.

What Command Copies Files Recursively In Linux?

The cp -r or cp -R command copies directories recursively. For more features, use rsync -a.

Now you have a comprehensive understanding of how to copy all files from one directory to another in Linux. Practice these commands in a safe environment, and soon you will be able to handle any file copying task with confidence. Remember to always verify your commands with --dry-run before executing them on important data.