cp Command in Linux with Examples

November 30, 2023

Introduction

The cp command is a UNIX CLI tool for creating copies of files and directories across a filesystem. Although modern desktop Linux distributions provide the copy functionality via GUI, the command-line method is still more efficient when working with multiple files.

This article will introduce the cp command in Linux and provide examples of its use.

The cp command in Linux with examples.

Prerequisites

  • Sudo privileges.
  • Access to the terminal.

cp Command Syntax

The cp command syntax for copying files and directories is simple but offers various options for performing advanced actions. Below is the basic form of the command for copying files:

cp [options] [source] [destination]

To copy directories, add the -r (recursive) option:

cp -r [options] [source] [destination]

If the source command argument is a file, the destination can be another file or a directory. If the source is a directory, the destination must also be a directory.

Note: If a destination directory does not exist, the cp command cannot create it and reports an error. Ensure you have first created the destination with the mkdir command.

cp Command Options

The cp command options allow users to see more details about the copying operation, control the process, create file backups, and see the help file. The table below contains the most commonly used cp options.

OptionDescription
-a, --archivePreserve the source's metadata, such as creation date, permissions, and extended attributes.
-b, --backupCreate backups for destination files. The backup file has the (~) suffix unless --suffix is used.
-f, --forceIf the destination file/directory already exists, replace it with the source.
-i, --interactiveAsk before overwriting the destination with the source.
-l, --linkCreate hard links instead of new files. The destination file will have the same inode attribute as the source.
-n, --no-clobberDo not overwrite the destination file if it exists.
-R, -r, --recursiveCopy the source directory recursively.
-S, --suffix=Provide a custom suffix for the backup file.
-t, --target-directory=Specify a directory for the source files.
-u, --updateReplace files only if they satisfy the update condition. The short option replaces files if the destination is older than the source. The long option lets the user specify the condition (all, none, or older).
-v, --verboseOutput information about the copying process.
--helpShow help.
--versionShow version information.

cp Command Examples

The options listed above allow the cp command to be customized to fit a specific scenario. Below are examples of the operations that cp can perform.

Copy Multiple Files to Directory

Copy file-1.txt, file-2.txt, and file-3.txt to the test-dir directory.

cp file-1.txt file-2.txt file-3.txt test-dir

The command has no output, but use the ls command to verify that the files are now in test-dir.

Using the cp command to copy multiple files to a directory.

Use Wildcard Symbols

Use the (*) symbol as a wild card if the filenames follow a pattern. The following example copies all the files that start with file- to test-dir.

cp file-* test-dir
Using the cp command with a wildcard to copy multiple files.

Use Interactive Prompt on File Overwrite

Copy file-1.txt to test-dir. Show verbose output and prompt to overwrite if the file exists at the destination.

cp -v -i file-1.txt test-dir
Using the cp command in verbose mode to copy a file.

Create File Backup

Copy file-1.txt to test-dir and create a backup file with the .bak extension. Show verbose output.

cp -v -b -S .bak file-1.txt test-dir
Using the cp command to create a file backup.

Conclusion

This tutorial presented the syntax and the commonly used options for the cp command in Linux. It also offered examples of the command to help you understand its use.

To take the next step in mastering the Linux command line, read Linux Commands All Users Should Know.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
mv Command in Linux
October 24, 2023

This article shows you how to use the mv command in Linux, provides a list of options, and offers illustrative examples.
Read more
How to Delete a File in Linux
September 7, 2023

Find out how to remove files in Linux using straightforward commands like rm and unlink.
Read more
How to Create a File in Linux
July 11, 2024

Creating a new file in Linux is simple, but there are also some surprising and clever techniques.
Read more
How To Use grep Command
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. Learn how to use grep to search...
Read more