cp Command in Linux Explained [With Examples]

Written by: Subhash Chandra   |   Last updated: August 31, 2023

The cp command in Linux is used to copy files and directories from one location to another. It provides a simple and efficient way to duplicate data within the file system.

Syntax

cp [OPTION]… SOURCE… DESTINATION

where SOURCE refers to the file or directory to be copied, and DESTINATION specifies the target location where the file or directory will be copied to.

Basic Copying

When it comes to copying a single file, the operation is straightforward, but there are some nuances and options that users should be aware of.

Example:

To copy a file named file1.txt from your current directory to another directory called /home/ubuntu/projects/ you would use:

cp file1.txt projects/
copy a file

If you want to copy file1.txt and rename it to file1_backup.txt in the projects directory, you would use:

cp file1.txt projects/file1_projects.txt
copy multiple files

By default, when you use the cp command to copy a file to a destination where a file with the same name already exists, it will overwrite the destination file without any warning or prompt.

If you want to avoid unintentional overwriting, you can use the -i (interactive) option with cp. When used, it will prompt you for confirmation before overwriting:

cp -i example.txt projects/
copy interactively using -i option

You can then decide whether to proceed (by pressing y) or cancel the operation (by pressing n).

If you want prevent existing files from being overwritten use -n option. ie. If the DESTINATION file already exists, cp with the -n option will NOT overwrite the destination file.

Copying multiple files

The cp command is flexible enough to allow users to copy multiple files in a single command.

To copy files named file3.txt, file4.txt, and file5.txt from your current directory to another directory called projects, you would use:

cp file3.txt file4.txt file5.txt projects/

You may use verbose mode (-v) option to provide a descriptive output of the operation for each file.

cp -v file3.txt file4.txt file5.txt projects/
copy multiple files

Wildcards for Multiple Files

Often, you may want to copy multiple files based on a pattern rather than specifying each one. The wildcard character (*) can be handy here:

To copy all .txt files from the current directory to projects, you can use:

cp *.txt projects/

To copy all files that start with 'file' (e.g., file1.txt, file.csv), you can use:

cp file* projects/

Use wildcards carefully as they can also lead to unintended matches. Always be sure about the pattern you're using or check first using commands like ls to list matched files.

Copying directories

Copying directories with the cp command in Linux is a common task, but it requires a bit more attention than copying individual files due to the hierarchical nature of directories.

Recursive Copying

To copy a directory, especially if it contains files or subdirectories, you'll need to use the -r or -R option, which stands for "recursive". Default symbolic links within the directory will be followed, meaning the linked files/directories get copied, not the symlink itself.

Ensure you have read permissions on the source directory and write permissions on the destination. Otherwise, the copy operation may fail.

Examples:

Copy a directory named projects to another directory called projectsbackup:

cp -r projects projectsbackup/

If projectbackup doesn't already contain a directory named projects, this command will create one. If projectbackup does have a projects directory, the command will merge the contents. Note: This also copies hidden files and directories (those starting with a dot).

Copy the projects directory to projectsbackup but rename it newprojects_backup:

cp -r projects projectsbackup/newprojects

Remember unless -a or p option provided permissions, ownership, timestamps of files and directories are not retained from original items when copied.

Options

The cp command offers various options to customize its behavior. Some commonly used options include:

  • --preserve[=ATTR_LIST]: Preserve the specified attributes, where ATTR_LIST can be mode, ownership, timestamps, etc. --preserve=all preserves everything.
  • -d: If sending a directory as source, copy the symlink instead of the actual directory it points to. Also, preserve links within the copied directory.
  • -r, -R, or --recursive: Copy directories recursively. Note that the -R option is a more standard way to specify this and is typically recommended over -r.
  • -a, --archive: This option is equivalent to -dR --preserve=all.
  • -i or --interactive: Prompts for confirmation before overwriting existing files in the destination directory.
  • -v or --verbose: Displays detailed information about each file as it is copied, providing a verbose output.
  • -n, --no-clobber: Do not overwrite an existing file (useful to prevent unintentional overwrites).
  • -b, --backup: Make a backup of each existing destination file.
  • -f: If the target file cannot be opened, remove it and try again.
  • -P, --no-dereference: Never follow symbolic links (useful to copy symlinks as symlinks).
  • -b, --backup: Make a backup of each existing destination file.

Preserving File Attributes

When copying files and directories in Linux, there might be instances where you want to ensure that certain attributes of the original items (like permissions, timestamps, and ownership) are preserved in the copies.

To preserve file attributes with cp use --preserve=ATTR_LIST option. The ATTR_LIST can be one or more of the following:

  • mode: Preserves file permissions.
  • ownership: Preserves file ownership (user and group).
  • timestamps: Preserves the modification and access times.
  • all: Preserve file mode (permissions), ownership, and timestamps.

You can combine attributes by separating them with commas:

cp --preserve=mode,timestamps source destination

The archive Option -a, --archive is a very handy option that is equivalent to using -dR --preserve=all. It's especially useful when copying directories.

Example:

cp -a source_directory destination_directory

This command will perform a recursive copy of source_directory into destination_directory while maintaining symbolic links, file permissions, ownership, and timestamps. When using -a option you may need to run the command with super admin privilege, this is because regular users can't assign ownership of files to someone else. So, if you're copying files owned by different users, you might need to use sudo.

About The Author

Subhash Chandra

Subhash Chandra

Subhash Chandra, an Oracle Certified Database Administrator and professional writer, works as a Consulting User Assistance Developer at Oracle, bringing over 15 years of experience to the role. He enjoys sharing his technological passion through how-to articles, simplifying complex concepts in Linux, Windows, Mac OS, and various other platforms and technologies for a wide audience.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment