The cp command is how you copy files and directories from the terminal in Linux. No GUI needed. One command, one result. This guide covers the syntax, the -r flag for directories, wildcard patterns, and the most useful options — with practical examples for each.

cp Command Syntax for Copying Files in Linux

The command takes at minimum two arguments: a source and a destination.

cp [options] source destination

The source is the file or directory you want to duplicate. The destination is where it ends up. If the destination is an existing directory, the copied file lands inside it with its original name. If the destination is a new filename, cp creates the copy under that name. The full cp command reference lists every available option and its behavior.

How to Linux Copy File to Another Directory

To copy a single file, pass the source path first and the destination second.

cp ./DirectoryA_1/README.txt ./DirectoryA_2

This places a copy of README.txt inside DirectoryA_2. The original file stays where it is. To rename the copy at the same time, include the new filename in the destination path:

cp ./DirectoryA_1/README.txt ./DirectoryA_2/README_backup.txt

After any copy operation, run the ls command on the destination directory to confirm the file landed where you expected.

Copy Multiple Files at Once with cp

List all source files before the destination. The last argument is always treated as the destination — everything before it is a source.

cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2

Both files end up in DirectoryA_2. The rule holds regardless of how many files you list — the final argument is where they all go.

Linux Copy File Directory Using cp -r

By default, cp only handles files. Attempt to copy a directory without any flags and the command stops with an error:

cp ./DirectoryA_1/Folder/ ./DirectoryA_2
# cp: omitting directory './DirectoryA_1/Folder/'

The -r flag tells cp to copy recursively — the named directory along with everything inside it, including nested subdirectories.

cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2

If the destination path doesn’t exist, cp creates it. If it does exist, the source directory is copied inside it as a subdirectory. The duplicating folders guide on commandlinux.com covers force-copy behavior and how destination path existence changes the output.

Match Files for Linux Copy Using Glob Patterns

Glob patterns let you select multiple files based on naming rules instead of listing them one by one. The * character matches any sequence of characters in a filename.

cp ./DirectoryA_1/*.txt ./DirectoryA_2

This copies every .txt file in DirectoryA_1 to DirectoryA_2 in one shot. You can build more targeted patterns — matching files that start with a specific string, end with a number, or contain a particular substring — by adjusting the glob expression.

One thing worth knowing: * does not match hidden files (those beginning with a dot). To include them in a directory copy, use cp -r on the source directory rather than a wildcard.

Useful cp Options for File Copy Operations in Linux

Flag What It Does
-r Copies a directory and all its contents recursively
-p Preserves file mode, ownership, and timestamps from the source
-i Prompts before overwriting any existing file at the destination
-n Skips the copy entirely if the destination file already exists
-v Prints each filename as it gets copied (verbose output)
-f Removes write-protected destination files before copying without prompting

Flags can be combined. cp -rp copies a directory recursively while preserving all timestamps and permissions — useful when archiving project folders or making system backups where original modification times need to stay intact.

Copying Linux Files Between Remote Machines

cp works within the local filesystem only. For transfers between two machines, scp is the right tool. It routes data through SSH, so authentication and encryption are built into every transfer. All the available flags are in the scp command documentation.

The syntax follows the same source-then-destination pattern, with remote paths formatted as user@host:/path/to/file:

scp user@remotehost:/home/data/file.txt /local/destination/
scp -r user@remotehost:/home/project/ /local/backup/

Before running any remote copy, you need an active SSH connection to the target machine. The SSH login guide on commandlinux.com walks through setup, key-based authentication, and common connection errors. For large or incremental transfers, rsync is generally a better fit than scp since it only sends the parts that changed.

FAQs

What does the cp command do in Linux?

The cp command copies files or directories from a source path to a destination. The original stays in place. It requires at least two arguments: a source file or directory and a destination path.

How do I copy a directory in Linux using cp?

Use the -r flag: cp -r /source/folder/ /destination/. Without -r, cp refuses to copy directories and returns an error. The flag copies the directory along with all its contents recursively.

How do I copy multiple files at once with cp?

List all source files before the destination: cp file1.txt file2.txt /destination/. The last argument is always the destination. Every argument before it is treated as a source file to be copied.

How do I prevent cp from overwriting existing files?

Add the -n flag: cp -n source.txt destination/. This skips the copy if a file with the same name already exists at the destination. Use -i instead to get a prompt before each overwrite.

What is the difference between cp and scp in Linux?

cp copies files within the local filesystem only. scp copies files between two machines over a network using SSH encryption. For any remote transfer, scp is the correct command — cp has no network capability.

Image

Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.