Linux cp Command: How to Copy a File or Directory

The cp command allows you to duplicate files or directories from one part of the file system to another.

Published on Aug. 28, 2024
The word "Linux" spelled out in keyboard keys above a keyboard
Image: Shutterstock / Built In
Brand Studio Logo

In Linux, tasks like efficient navigation through file systems and certain operations like copying, moving or deleting files rely on the terminal. This tool gives users access to the core functionalities of the operating system. Unlike graphical interfaces, which can sometimes be limiting or slow, the Linux terminal provides an interface for quick and efficient execution of certain tasks that interact directly with the underlying system. By running commands, users can quickly execute tasks that would otherwise require multiple steps through a graphical interface.

One of the most useful and commonly used commands is cp. Short for copy, the command is used to duplicate files or entire directories from one location of the file system to another.

Linux cp Command Syntax

cp source_file target_file

More From Giorgos MyrianthousHow to Fix AttributeError: ‘DataFrame’ Object Has No Attribute ‘Append’

 

The cp Command Syntax

Before looking into the details, the basic synopsis of the cp command is as follows:

cp source_file target_file

 

Features and Options of the cp Command 

The capabilities and customization options of the cp command extend far beyond its basic syntax, however. This versatility make it adaptable to a wide range of use cases.

Some of the most useful options are outlined below. 

-R 

This option allows you to copy directories, along with their contents, into the specified destination location. The newly created destination directories will also have the same mode as the corresponding source directory.

cp -R folder_with_files new_folder_with_the_same_files

-p

This option preserves the attributes of each source file in the copy, including modification time, access time, file flags and file mode.

cp -p my_file.txt /home/my_file_copy.txt

-i

This option is particularly useful when you want to have some additional control over the process of copying file(s) or directories. Short for “interactive,” it will prompt the user before overwriting any existing files. In order for the operation to proceed, the user will have to type y (or Y). Alternatively, the copy operation is canceled. 

cp -i my_file.txt /home/my_file.txt

-v 

This option, short for “verbose,” will cause the command to show the progress of the files being copied. This is particularly useful when the copy operation involves multiple files.

cp -v my_file.txt /home/my_file_copy.txt

-n

Finally, this option will prevent any file overwrites. This flag will override the -i option and, if the destination file or directory already exists, it will prevent any resources from getting overwritten.

Note, however, this is a small subset of all possible options that you can use with the cp command. For the full list of available options, you can refer to the General Commands Manual, which is accessible by running the following command in the terminal:

man cp
Video: YouTube

 

How to Use the cp Command to Copy Files and Directories in Linux

Now that we’ve covered the basics and features of the cp command, lets dive into more practical examples. In this section, well explore how to use the cp command to copy files and directories in various scenarios, ranging from simple file copies to more complex operations involving multiple files and directories.

How to Copy a File in Linux

Copying a single file with the cp command is simple. The following command will simply copy the my_file.txt file and store it under the same (current working) directory, with the name my_file_copy.txt.

cp my_file.txt my_file_copy.txt

If you’d like to create a copy of the file and store it in a directory other than the current one, you can simply specify the desired location in the destination. 

cp my_file.txt /home/Desktop/my_file_copy.txt

If you want to keep the same name while copying the file into a different directory, you can even omit the file name itself.

cp my_file.txt /home/Desktop/

This command will create a copy of my_file.txt file under /home/Desktop/ directory and retain the destination file name.

How to Copy Multiple Files in Linux

To copy multiple files at once, list each file as a source, followed by the destination directory:

cp file1.txt file2.txt /path/to/destination

This command will copy both file1.txt and file2.txt into the specified destination directory.

How to Copy Files That Match a Glob Pattern in Linux

You can use wildcard characters to copy files that match a specific pattern. For example, to copy all .txt files from one directory to another:

cp *.txt /path/to/destination

This command copies all files with a .txt extension to the destination directory.

How to Copy Directories in Linux

To copy a directory and all of its contents, use the -R (recursive) option:

cp -r /path/to/source_directory /path/to/destination/

This command copies the entire source_directory and all its subdirectories and files to the specified destination directory.

 

How to Prevent Overwriting Files With cp Command

When working directly in the terminal and executing commands, you always need to be careful since you can’t reverse most operations. So, when it comes to creating copies of files or directories, you should ensure that you won’t accidentally overwrite existing files. 

Fortunately, a few different options that come along with the cp command can help you mitigate this risk. 

By default, the cp command will overwrite files in the destination if they already exist. To prevent this, you can use the -i option, which will prompt you before overwriting:

cp -i file.txt /path/to/destination/

If the file already exists, you will be asked whether you want to overwrite it. Alternatively, you can use the -n option to prevent overwriting without a prompt:

cp -n file.txt /path/to/destination/

This ensures that existing files are not overwritten, and the command will silently skip any files that already exist in the destination.

 

The scp Command for Secure Remote Copying

Although the cp command is a great tool for copying files and directories within your local system, there are times when you need to transfer files between different machines securely. 

That’s where the scp command comes in. The term scp, short for secure copy, is a command-line command that allows you to securely transfer files and directories between a local and remote host or between two remote hosts.

The basic syntax of the scp command is:

scp [options] source user@host:destination

where

  • source is the file or directory you want to copy
  • user@host specifies the remote user and host (e.g. [email protected])
  • Destination is the path on the remote host where the file or directory will be copied

For instance, the following command copies file.txt from your local machine to the specified directory on the remote server.

scp file.txt user@remotehost:/path/to/destination/

Likewise, if you would like to copy a directory, the -R option should do the trick:

scp -R /home/contacts user@remotehost:/home

You can even copy a file from a remote server into your local machine by switching source and destination specification: 

scp user@remotehost:/home/file.txt /local/destination/

More on the scp CommandHow to Use the SCP Command in Linux to Securely Transfer Files

 

Master the cp Command

Mastering the cp command is a crucial skill for any Linux user, providing the ability to efficiently manage and organize files and directories. From basic copying to more advanced operations with various options, the cp command is simple, yet powerful. Additionally, understanding related commands like scp further enhances your ability to securely transfer files across different machines. 

Frequently Asked Questions

Short for copy, the cp command is used in Linux to duplicate files or entire directories from one location of the file system to another.

You can use the cp command to copy a file in Linux. Its syntax is cp source_file target_file.

Explore Job Matches.