How To

How to Copy Files and Directories in Linux: cp and rsync

The two main tools for copying files and directories in Linux are cp and rsynccp handles quick, local file and directory copies. rsync is the better choice for large directories — it can resume interrupted transfers and skip files that have not changed.

Before copying, you need read permission on the source and write permission on the destination directory. If you are missing write access to the destination, the copy will fail regardless of the file’s own permissions.

How to Copy Files in Linux with cp

Copy a file in the current directory:

bashcp file.txt file_backup.txt

If the destination file already exists, cp overwrites it without warning. Use -i to get a confirmation prompt first:

bashcp -i file.txt file_backup.txt

By default, the copied file is owned by the user running the command — not the original owner. Use -p to preserve the original file mode, ownership, and timestamps:

bashcp -p file.txt file_backup.txt

Add -v for verbose output that prints each file as it is copied:

bashcp -v file.txt file_backup.txt

Copy to a directory. Specify a directory as the destination and the file keeps its original name:

bashcp file.txt /backup/

To save it under a different name, include the filename in the destination path:

bashcp file.txt /backup/new_file.txt

Copy multiple files. List all source files with the destination directory as the last argument:

bashcp file1.txt file2.txt *.png /backup/

When copying multiple sources, the destination must be a directory. The glob pattern * does not match hidden dotfiles by default.

Copy Directories with cp: -R, -T, and -a Explained

Copying a directory requires the -R (recursive) flag:

bashcp -R Pictures Pictures_backup

The behavior depends on whether the destination exists:

  • Destination does not exist: cp -R creates it and copies the contents inside
  • Destination already exists: the source directory itself is placed inside the destination, creating a nested Pictures_backup/Pictures/ structure

To avoid that nesting when the destination already exists, use -T:

bashcp -RT Pictures Pictures_backup

-T copies only the files and subdirectories, not the source directory itself.

-r vs -a. Both copy recursively, but they are not equivalent:

  • -r – recursive only; does not preserve ownership, permissions, or symbolic links
  • -a – equivalent to -dR --preserve=all; creates an exact replica including all attributes

Use -a for backups or whenever you need a faithful copy of a directory tree:

bashcp -a /home/user/project /backup/project

Copy Files and Directories with rsync

rsync is the better tool when copying large directory trees. It compares source and destination and transfers only the differences, making repeat copies much faster than cp.

Copy a single file:

bashrsync -a file.txt file_backup.txt

Copy a directory with human-readable progress output:

bashrsync -ah --progress /var/www/public_html/ /var/www/public_html_backup/

Trailing slash behavior. With rsync, a trailing slash on the source copies the contents; without it, rsync copies the directory itself inside the destination. The safest habit is to add a trailing slash to both the source and destination:

bashrsync -a /src/ /dest/

rsync can also resume interrupted transfers and copy over SSH, making it the standard choice for file synchronization between servers.

Use cp for quick local copies, cp -a when you need to preserve all attributes, and rsync for large directories or remote transfers. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

git clone: Clone a Repository from Remote or Local Sources

git clone copies an existing Git repository into a new directory on your local machine. It…

4 minutes ago

jobs Command in Linux: List and Manage Background Processes

The jobs command is a Bash builtin that lists all background and suspended processes belonging to the…

1 day ago

dmesg Command in Linux: Read and Filter Kernel Messages

The Linux kernel writes messages to the kernel ring buffer throughout the boot process and…

1 day ago

How to Transfer Files with rsync over SSH: A Practical Guide

rsync over SSH combines secure encrypted transport with fast incremental transfers. Instead of copying every file…

1 day ago

pstree Command in Linux: Visualize Running Process Hierarchies

The pstree command in Linux displays running processes in a tree structure rather than a flat list.…

1 day ago

type Command in Linux: Show How the Shell Resolves a Name

The type command in Linux shows how the current shell would interpret a name typed on the…

2 days ago