The Linux Copy File Command Made Easy

In Linux systems, copying of files and directories is one of the most common tasks performed when working on the command line. The cp is a Linux file copy command which has made the life easier for completing all the copy related operations. It is a command-line based utility for copying files and directories on Linux/Unix systems from a source location to the destination location. In most cases, it is just a single line command which makes much bigger tasks possible. In this article, you are going to read how to perform different types of interesting operation using cp.

Introduction to the Linux Copy File Command (cp)

From the start, I will run its help command to make you more aware of it.

cp --help

In Linux, the cp means to copy. This command is used to copy files or a group of files or directory from a source location to the destination location. It generates the exact content of the copied file(s) on a disk with your pre-defined different file name. The Linux copy file (cp) command needs at least two file names in its arguments to operate. There are many types of utilization of copy command, but I will show you the foremost and daily life practical commands which will help you in the workflow.

The base syntax for copying any file from a source address to the address is:

cp <source address> <destination address>

Now by further changing the arguments of the base syntax, I will be performing different Linux copy file operations which you will see more in this article.

Copy a File in the Same Directory

As said earlier, that the cp command helps the user in copying the content of a source file to its destination, so here I will be replicating the content of a source file (ceos3c.txt) to the new destination file (ceos3c_new.txt). If the destination file already exists, so this command will overwrite that file without any warning message, and if the destination file will not exists, then a new file will be created which will copy the content of source file as per user’s desire.

cp ceos3c.txt ceos3c_new.txt

Copy a Single File to a Directory

By using this copy command, you can copy a source file (ceos3c.txt) to the destination directory (newfolder) whenever needed. If the directory doesn’t exist then first, it will create a new directory and will copy the source to it, but if the directory already exists then cp command will overwrite the pre-existing content in the destinated directory.

cp ceos3c.txt newfolder/

Copy Multiple Files to a Directory

With the help of this Linux copy file command, you can copy multiple files to a directory whenever needed. Suppose the various source files (ceos3c1.txt, ceos3c2.txt, ceos3c3.txt) required to be copied all at once to a specific directory (newfolder) then you can frame the above command as shown. Using this command, the cp will copy the entire content from the source file to mentioned destinated directory. If the directory doesn’t exist then first it will create a new directory and will copy the content to it but, if the directory already exists then, cp will overwrite all the preexisting files in the destinated directory so be careful while copying the content from source to destination.

cp ceos3c1.txt ceos3c2.txt ceos3c3.txt newfolder/

Copy source directory into the destination directory

This Linux copy file command uses its recursive performance by replicating (-r or -R) the entire source directory structure recursively to the destination location. It is used in a situation where you want to copy all the files and sub-directories that a directory contains, so it will simply copy the whole directory instead only its files to the desired destination path. Even if the destination directory does not exist, it will create one, and if t would already exist, then it will replace and overwrite the existing file(s).

cp -R ignite demo2

Interactive copy command

Usually, when you use the copy command if the destination file already exists, it merely overwrites it. For making it interactive so that it will show a confirmation prompt while copying a file, the –i argument comes into the play. Using this argument, whenever you will copy source (ceos3c.txt) to the destination location and a file with the same name would exist there then it will prompt to ask for the confirmation to overwrite the destination file (demo.txt). If you answer the prompt as y (yes) then it will overwrite to the destination file or your answer would be n (no) then it will save the destination content from being erased.

cp -i ceos3c.txt demo.txt

Verbose Copy command

Sometimes when you perform a complex operation with the Linux copy file command which includes multiple source files (ceos3c.txt, ceos3c_new.txt, ceos3c2.txt, ceos3c3.txt) to the destination (newfolder) location so there would be several chances of typing errors or the wrong destination as it is a command-line based operation, then in this case you can use verbosity (-v). It will show you the detailed output of the actions that are performed, from where the source occurs and where it destinated.

cp -v ceos3c.txt ceos3c_new.txt ceos3c2.txt ceos3c3.txt newfolder/

Backing up a file

There is a situation when you need to create a backup of the destination file (demo.txt) while copying the content of the source file (ceos3c.txt) on it; then there is -b for this purpose. It helps in creating a backup of the file in the same folder with the different name and format.

cp -b ceos3c.txt demo.txt

Preserving file attributes

While copying a source file (ceos3c.txt) to the destination file (demo.txt), you are required to preserve the attributes of the file for forensics or other reasons then use -p for it. It will preserve the following attribute of the source file to the destination one;

Modification and Access Date/Time

File flags and modes

User and Group ID

Access Control List (ACL)

Extended Attributes (EA)

cp -p ceos3c.txt demo.txt

Copy using Wildcard character

The star (*) wildcard character is used to select all or a group of the file concerning the way it could be used. As shown in the example above, I have used the source two ways to copy all files (in a first screenshot) and a group of file (in the second screenshot). Firstly, I have copied all the files using a * character which have copied all the file regardless of their differences to the destination folder, while in a second way, I have copied all the files which have .txt extension irrespective of their names to the destination folder. And not just .txt, you can copy file of any extension in this way.

cp * newfolder/

cp *.txt newfolder/

Force copy to a file

When a destination file permission does not allow to overwrite the data of source file on it, and it is needed to write the data on it forcefully, then in this case –f becomes handy. By using the –f (force) argument of Linux copy file command, you can forcefully overwrite the destination file with the data of the source file. It deletes the destination file first and then copies the contents of the source file on it.

cp -f ceos3c.txt demo.txt

Symbolic Link Copy

A symbolic link is a particular type of file which points toward another file that is like a shortcut in a Mac or Windows. You may need the contents of the source file without placing the actual file in place due to the dynamic changes of a particular directory which may damage the file. In this scenario, you can use –s (symbolic link), which will create a pointer from destination towards the source and will create an image (exact copy) of the source file into the destination file. Whenever the source file gets deleted, the pointer will get broken, and the destination will no longer maintain the image of the source file.

cp –s ceos3c demo

The presence of pointer between destination to the source file will change the icon of the destination file as shown below:

The icon when the pointer is present

Icon when the pointer is not present

Copy Command with Recency attribute

There could be scenarios when you only want to copy the source files only if they are newer than the destination ones. This functionality can be easily be achieved by using -u of the Linux copy file command. In the above-shown example, you can see that destination (demo) file was more recent than the source (ceos3c) file, so it did not change the contents of the destination file the -u command. You can manage the redundancy of the files, and the data loss can be prevented, which could occur by mistakenly overwriting the old data on the new one.

cp –u ceos3c demo

Conclusion of the Linux Copy File Command



As you have seen til now, the usage of the cp command is straightforward to understand as shown in the examples which are covered in this tutorial. I hope it will help you in learning about the Linux copy file command more effectively. Try all of them by yourself. That’s all from this article and please do share your feedback and comments.

Please Share! Email

Facebook

Reddit

Twitter

Pinterest

More

LinkedIn

