sodcalculator

08 Everyday Math

Duplicate File Linux Command Line? (cp, rsync, dd)

Guide 08-64580 · Updated · Step by step

To duplicate a file on the Linux command line, you use the cp command followed by the source file and the destination name or path. For example, cp file.txt file_copy.txt creates an exact copy. This article covers the core cp command, its most useful options, and alternatives like rsync and dd for specific scenarios.

Why Duplicate Files from the Command Line?

Graphical file managers can copy files, but the command line offers speed, precision, and automation. When you need to duplicate a file, you often also need to:

  • Preserve permissions, timestamps, and ownership.
  • Copy multiple files at once using wildcards.
  • Duplicate files across different filesystems or over a network.
  • Create exact byte-for-byte copies, including hidden data or sparse files.
  • Integrate the copy into a script or a one-liner.

The command line gives you full control over these aspects, which is why it remains the preferred method for system administrators and power users.

The Essential cp Command

The cp command is the standard tool for copying files and directories. Its basic syntax is:

cp [OPTION]... SOURCE... DEST

Where SOURCE is the file you want to duplicate and DEST is the destination. DEST can be a filename (to create a copy with a new name) or a directory (to copy the file into that directory, keeping the original name).

Common examples:

  • cp report.pdf report_backup.pdf – creates a copy named report_backup.pdf in the same directory.
  • cp report.pdf /home/user/Documents/ – copies report.pdf into the Documents directory.
  • cp *.txt /tmp/backup/ – copies all .txt files in the current directory to /tmp/backup/.

Useful cp Options

To get the most out of cp, you need to know its options. Here are the ones you’ll use most often:

  • -a (archive): Preserves all attributes (permissions, timestamps, ownership, and symbolic links) and copies directories recursively. Equivalent to -dR --preserve=all.
  • -r or -R (recursive): Copies directories and their contents. Without this, cp will error on a directory.
  • -p (preserve): Preserves the original file’s mode, ownership, and timestamps. Useful when you want a copy that looks identical in metadata.
  • -i (interactive): Prompts before overwriting an existing file. Good safety habit.
  • -u (update): Copies only when the source is newer than the destination or when the destination is missing.
  • -v (verbose): Prints what is being done, helpful for debugging.
  • -n (no-clobber): Does not overwrite an existing file. This is the opposite of -i in effect.

For example, to duplicate a directory with all its contents and preserve everything, you would run:

cp -a /path/to/source_dir /path/to/destination_dir

This creates a full copy that behaves like the original.

Duplicating Files with rsync

While cp is perfect for simple copies, rsync is a more powerful tool, especially when you need to sync directories or copy over a network. Its key advantage is that it only copies the differences between source and destination, making subsequent updates fast.

Basic local copy with rsync:

rsync -av source_file destination_file

Options commonly used:

  • -a (archive): Recursive copy with permissions, timestamps, and other attributes preserved.
  • -v (verbose): Shows progress.
  • -z (compress): Compresses data during transfer, useful over slow networks.
  • --delete: Deletes files in the destination that are not in the source (use with caution).

For a true duplicate of a directory, you might use:

rsync -a --delete /source/ /destination/

The trailing slashes are important: /source/ means copy the contents, while /source would copy the directory itself. rsync is also the go-to tool for backing up to external drives or remote servers via SSH.

When to Use dd for Exact Copies

If you need a byte-for-byte copy of a file, including its filesystem metadata or even an entire disk, dd is the tool. It reads and writes raw data, making it ideal for cloning drives or creating disk images.

Basic file duplication with dd:

dd if=input_file of=output_file

Where if specifies the input file and of the output file. You can also set block size with bs and count with count to copy only a portion of a file.

Example: dd if=/dev/sda of=/dev/sdb bs=4M clones a disk. Warning: This is destructive; it will overwrite the entire destination. Always double-check device names before running dd.

For regular file duplication, cp is usually sufficient and safer. dd is overkill and can be dangerous if misused.

Common Pitfalls and How to Avoid Them

Several mistakes are common when duplicating files from the command line:

  • Overwriting files unintentionally: cp will overwrite an existing destination by default. Use -i or -n to prevent this.
  • Forgetting -r for directories: cp dir1 dir2 will fail if dir1 is a directory. Always include -r or -a.
  • Losing permissions: Without -p or -a, the copy will have default permissions (often based on umask), which may break scripts or executables.
  • Copying a symlink instead of the target: By default, cp copies the target file. To copy the symlink itself, use -P (or -d in some versions).
  • Trailing slashes in rsync: Forgetting the trailing slash on the source can lead to an extra nested directory.

Always test with -v and check the destination to ensure the copy is what you expect.

Frequently Asked Questions

How do I copy a file and keep the original timestamp?

Use cp -p source dest. The -p option preserves the modification time, access time, and file mode (permissions). For a full archive copy, use cp -a.

What is the difference between cp and rsync?

cp is a simple copy command that copies everything every time. rsync is a synchronization tool that only copies changed parts, making it faster for incremental backups and network transfers. rsync also has more options for preserving attributes and deleting extraneous files.

How do I copy multiple files at once?

You can use wildcards: cp *.txt /destination/ copies all files ending in .txt. Or list them explicitly: cp file1 file2 file3 /destination/. The destination must be a directory in both cases.

Does cp preserve hard links?

By default, cp breaks hard links; it creates a new file with the same content. To preserve hard links, use cp -a (or cp -d in some versions) which preserves links when copying directories recursively.

Can I duplicate a file to a different filesystem?

Yes, cp works across filesystems. It reads the source and writes to the destination, regardless of the underlying filesystem type. rsync is also cross-filesystem and often preferred for large transfers because it can resume and verify.

What is the fastest way to copy a large file?

For a single large file, cp is usually fast enough. If you need to copy many files or a large directory, rsync with -a and possibly -z (compression) can be faster, especially over a network. For raw disk cloning, dd with a large block size (e.g., bs=64M) is often used.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Everyday Math bed →