The two main tools for copying files and directories in Linux are cp and rsync. cp 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.
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.
Copying a directory requires the -R (recursive) flag:
bashcp -R Pictures Pictures_backup
The behavior depends on whether the destination exists:
cp -R creates it and copies the contents insidePictures_backup/Pictures/ structureTo 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 attributesUse -a for backups or whenever you need a faithful copy of a directory tree:
bashcp -a /home/user/project /backup/project
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.
git clone copies an existing Git repository into a new directory on your local machine. It…
The jobs command is a Bash builtin that lists all background and suspended processes belonging to the…
The Linux kernel writes messages to the kernel ring buffer throughout the boot process and…
rsync over SSH combines secure encrypted transport with fast incremental transfers. Instead of copying every file…
The pstree command in Linux displays running processes in a tree structure rather than a flat list.…
The type command in Linux shows how the current shell would interpret a name typed on the…