Hey there! Welcome to Hostman! 🎉

Linux cp Command

05.02.2024
Reading time: 8 min
Hostman Team
Technical writer

Linux has an unlimited set of commands to perform assigned tasks. The Linux cp command is the primary tool and the basis for copying and managing files and directories in this operating system. This function is designed to duplicate files or directories in the same or different location. Armed with this functionality, users have advanced capabilities: from creating backup copies to moving files between directories.

This tutorial covers all the essential information readers are looking to master the skills they need in their working process. You will learn about the mechanics of the cp command and cp directory in Linux, its syntactic structures, important tips, parameters, options and best practices. Readers will gain insight on the secrets of using the cp command that leads to improving their skills.

The core of the cp command in Linux

The functionality of the command allows users to control the creation of copies. One feature offers overwriting existing files, another is responsible for recursively copying a directory with its entire entities, and the third protects the first data for repeating backups. This command demonstrates more features for specific purposes and user experience during the process.

A key benefit of the cp command is its exceptional accuracy in duplicating files and directories. You can be absolutely sure that the duplicated files are identical to the original ones with all its interior. Therefore, the user can replicate the original file without any changes. The cp command in Linux inherently tells the user a destination directory for storing copies in a specific repository. The command's precision makes it indispensable for both novice and advanced users.

Linux cp syntax

This command consists of the following parameters: source file or directory and destination directory. The basic syntax of the Linux cp command is as follows:

cp [...file/directory-sources] [destination]

Here [file/directory-sources] specifies the files or directories sources to copy, while the [destination] specifies the location to copy the file to.

There are the letter flags to specify the way of creation a replica of files and directories:

  • -a leaves the first file attributes the same;
  • -r recursively replicates directories and their interior entities;
  • -v shows copied files in detail;
  • -i requires consent to overwrite the file;
  • -u rewrites new or missing files in the destination directory;
  • -f forcibly copies without user consent;
  • -s makes a symbolic link instead of a file replica;
  • -ra recreates an exact duplicate of a file or directory without changing attributes;
  • -rf updates or changes a file or directory with the original name in the same place;
  • -pv (if installed) monitors and shows the time required to complete copying large folders.

How to copy files with the cp command

To make a file copy, apply the cp command in Linux as follows:

cp ./DirectoryA_1/README.txt ./DirectoryA_2

where ./DirectoryA_1/README.txt is the source file, and ./DirectoryA_2 is the destination.

The cp command was originally designed to interact with files. To replicate directories, you must use the -r flag to command that the directory with all its interior entities to be copied recursively. Therefore, you should write cp -r before the directory sources in Linux as follows:

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

The cp -r command in Linux will recursively duplicate the Folder directory in ./DirectoryA_1/ as well as all contents in the Folder directory.

For instance, if you need to replicate the whole file contents in DirectoryA_1 with the .txt extension, try following command:

cp ./DirectoryA_1/*.txt ./DirectoryA_2

where ./DirectoryA_1/*.txt matches files with the .txt extension in their names, and the cp command duplicates all those data to the destination.

Best practices of the cp Linux command

To duplicate one unit of information via the Linux cp command, write down the file name and destination directory. For instance, to replicate a file named example.txt to the 'Documents' directory, try the following command:

cp example.txt Documents/

The action leads to creating a file duplicate in the 'Documents' directory with the original name.

To copy multiple files at once, utilize the cp command in Linux, specifying the file names separated by a space. For instance, to duplicate three files named 'file1.txt', 'file2.txt', and 'file3.txt' to the 'Documents' directory, try the following command:

cp file1.txt file2.txt file3.txt Documents/

To replicate a directory with all its interior entities, apply the -r that means cp recursive feature in Linux. For instance, to duplicate a directory named 'Pictures' to the 'Documents' directory, try the following command:

cp -r Pictures Documents/

The action leads to creating a copy of the 'Pictures' directory with all its interior contents in the 'Documents' directory.

To replicate a folder in Linux, you should utilize the -r flag. For instance, to duplicate a folder named 'Pictures' from the existing directory to a folder named 'Photos' in the home directory, try the following command:

cp -r Pictures/ ~/Photos/

If there is no destination folder, it will be created automatically. If there is already a folder, the core of the source folder will be merged with the destination folder files.

The cp -a feature in Linux leaves unchanged the initial file attributes while copying. Therefore, the duplicates will have the same parameters as their originals. For instance, to replicate a file named 'example.txt' to the 'Documents' directory while leaving unchanged its attributes, try the following command:

cp -a example.txt Documents/

The Linux cp -v function showcases the progress of the duplication. At the same time the user can copy large files while monitoring the process. For instance, to replicate a file named 'largefile.zip' to the 'Downloads' directory while watching the progress, try the following command:

cp -v largefile.zip Downloads/

The -i option requires the consent before overwriting an initial file. to protect against an accidental file rewriting. For instance, to duplicate a file named 'example.txt' to the 'Documents' directory, if a file with the identical name already exists, the cp command will require the consent before rewriting the original file.

Initially, the Linux cp command copies a file or a directory to a default location. The system allows the user to specify any other location for the duplicate file or directory. For instance, to replicate a file named 'example.txt' from the 'Documents' directory to the 'Downloads' directory, try the following command:

cp Documents/example.txt Downloads/

The cp -ra function in Linux is designed to carry out the copying process of directories with all their contents inside. The -r flag gives an order to repeat all the files and directories within an existing location, while the -a flag keeps the initial attributes preserved. Therefore, it is possible to make an exact duplicate of a directory without changing attributes.

For instance, if you apply the command cp -ra /home/user1/documents /home/user2, it will replicate the 'documents' directory with all its entities inside in the 'user2' directory. The new folder will show the identical attributes as the initial item.

The cp -rf feature in Linux is similar to the previous -ra option. The difference between these two functions is that the -f flag rewrites the given files or directories in the destination without requiring consent. Therefore, it is possible to update or replace an item with the identical name in the place of destination.

For instance, if you apply the command cp -rf /home/user1/documents /home/user2, and there is already a 'documents' directory in the 'user2' directory, it will be overwritten with the contents of the 'documents' directory from the 'user1' directory.

Be careful while utilizing the -rf function. Incorrect use of it leads to data loss. Check up twice the destination folder to avoid unwanted rewriting items.

The cp -r feature in Linux with the -a and -f flags makes it easier to interact with files and directories. The -ra particle duplicates exactly a directory with its items inside, while the -rf particle updates or replaces files and directories. Proper application of these differences will teach you how to effectively manage items in this operating system.

If you want to monitor and control the process of item duplication, which is not possible with other parameters of the cp command, use the -pv utility.

To install the pv utility on Debian/Ubuntu you need to open the terminal and run the following command: 

apt-get install pv

After the installation is complete, verify it by running the following command in the terminal

pv --version

To install the pv utility on CentOS/Fedora, you need to connect the EPEL repository, which contains additional software packages unavailable in the default repositories. Run in the terminal:

yum install epel-release

Then run the following command in the terminal: 

yum install pv 

After the installation is complete, verify it by running the following command in the terminal: 

pv --version

To use this particle with the cp command, you should utilize | symbol. You can use the ~ symbol to indicate the root directory if the full path needs to be specified.

For instance, to replicate a folder named 'Documents' from the root directory to a folder named 'Backup' in the home directory, try the following action:

cp -r Documents/ ~/Backup/ | pv

Conclusion

The cp command, although not an inherently difficult tool to learn, nevertheless provides basic knowledge of using the Linux operating system in terms of managing files and directories. In this tutorial, we tried to show the capabilities of the cp command in Linux from all sides, demonstrating best practices and useful tips of its various parameters. With new knowledge, you will be able to improve your skills in interacting with files and directories in Linux.

The extreme accuracy of the copying process and additional options allow you to solve a wide range of problems. Multifunctionality helps users choose the file management mode and complete tasks efficiently. The command is a prime example of the many capabilities of this operating system, including the cp with progress feature in Linux. Altogether they unlock a potential of the system for novice and advanced users.