Linux Copy File – Linux CP Made Easy!

In this article, you will learn everything you need to know about the Linux Copy File, or short, the Linux CP command. Copying files in Linux is an essential skill, and we will show you how to utilize this command in a step-by-step fashion.

Table of Contents

Top Things to After Installing Kali...
Top Things to After Installing Kali Linux in 2023

How do I copy Files in Linux?

There are a few different ways to copy files in Linux. The most common method is to use the cp command. This command stands for “copy” and it allows you to copy files from one location to another. For example, if you want to copy a file called “example.txt” from your current directory to a new directory called “temp”, you would use the following command:

cp example.txt temp/Code language: Bash (bash)

This would create a new file called “example.txt” in the “temp” directory that is identical to the original file.

Another way to copy files in Linux is to use the mv command. This command stands for “move” and it allows you to move files from one location to another. However, unlike the cp command, the mv command will delete the original file after it has been copied to the new location. If you want to move a file called “example.txt” from your current directory to a new directory called “temp” you would use the following command:

mv example.txt temp/Code language: Bash (bash)

This would create a new file called “example.txt” in the “temp” directory and delete the original file.

Finally, you can also use the GUI interface to copy files in Linux. Most distributions come with a file manager application that provides a graphical way to copy, move, and delete files. To use this method, simply open up your file manager application and navigate to the location of the file you want to copy. Then, click on the file and select “Copy” from the menu. Next, navigate to the location where you want to copy the file and click on “Paste.” The file will be copied to the new location.

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 --helpCode language: Bash (bash)
The Linux CP command options page
The Linux CP command options page

In Linux, the cp command is a shorthand for copy. This command is used to copy files or a group of files or directories 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 commands, 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>Code language: Bash (bash)

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

Copy a File in the Same Directory

The cp command helps the user copy 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, this command will overwrite that file without any warning message, and if the destination file is not existing, then a new file will be created, which will copy the content of the source file as per the user’s desire:

cp ceos3c.txt ceos3c_new.txtCode language: CSS (css)
The Linux Copy File Command

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 the cp command will overwrite the pre-existing content in the destinated directory.

cp ceos3c.txt newfolder/
The Linux Copy File Command

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 are required to be copied all at once to a specific directory newfolder then you can frame the above command as shown.

Using the cp command 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/Code language: Bash (bash)
The Linux Copy File Command copy to new folder

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
Linux CP example

Interactive copy command

Usually, when you use the copy command if the destination file already exists, it merely overwrites it. To make it interactive so it will show a confirmation prompt while copying a file, the –i argument comes into play. Using this argument, whenever you will copy the source file ceos3c.txt to the destination location and a file with the same name would exist there then it would prompt to ask for confirmation to overwrite the destination file demo.txt.

If you answer the prompt as y (yes), then it will overwrite the destination file, or if your answer is n (no) then it will save the destination content from being erased.

cp -i ceos3c.txt demo.txtCode language: Bash (bash)
Linux CP command example
Linux CP command example 2

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, there would be several chances of typing errors or a 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 is estimated:

cp -v ceos3c.txt ceos3c_new.txt ceos3c2.txt ceos3c3.txt newfolder/
Linux CP example 5

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. In this case, there is the -b flag for this purpose. It helps create a backup of the file in the same folder with a different name and format.

cp -b ceos3c.txt demo.txtCode language: Bash (bash)
Linux CP example 6

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 flag 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.txtCode language: Bash (bash)
Linux CP example 7

Copy using Wildcard character

The star * wildcard character is used to select all files and folders inside of the current folder. As shown in the example above, I have used the source in two ways to copy all files (in the first screenshot) and a group of files (in the second screenshot).

Firstly, I have copied all the files using a * character which has copied all the files 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 files of any extension in this way:

cp * newfolder/
Linux CP example 8
cp *.txt newfolder/
Linux Copy File example 9

Force copy to a file

When a destination file permission does not allow to overwrite the data of the source file on it, and it is needed to write the data on it forcefully, then in this case, the –f flag becomes handy. By using the –f (force) argument of the 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.txtCode language: CSS (css)
Linux Copy File example 10

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 the –s flag (symbolic link), which will create a pointer from the 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
Linux Copy File example 11

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

The icon when the pointer is present

The 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 easily be achieved by using the -u flag of the Linux copy file command. In the below-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 with 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
Linux CP example 12
Linux Copy File Example 1

Conclusion of the Linux Copy File Command

As you have seen until now, the usage of the cp command is straightforward to understand, as shown in the examples which are covered in this tutorial. I hope this will help you better understand the Linux CP command and all of its different use cases.

🐧 There are some related articles!
👉 Top Things to do after installing Kali Linux
👉 Linux on Windows – The Complete WSL 2 Tutorial Series
👉 Create Generalized Windows Images with FOG Server
👉 Install FOG Server on Ubuntu
👉 Install Kali Linux on Windows – Complete Guide
👉 The Complete Kali Linux and Windows Dual Booting Guide
👉 Cronjob – The Complete Guide to Cronjobs
👉 Open Bitlocker Drive on Linux – Quick & Easy

4 thoughts on “Linux Copy File – Linux CP Made Easy!”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap