How to Copy Files and Directory in Linux?

File handling is one of the most crucial responsibilities of a Linux System Administrator. One important concept in file handling is to copy files and directories to the same and other directories. 

If you have trouble copying directories or files in Linux or Unix-based OS like macOS, this post will guide you.

Let’s begin with the first one!

How to Copy a File Using the cp Command in Linux

The cp “copy” command is used to copy files and directories in Linux. To use this command, you need to follow the given syntax:

cp source_file target_file

Let’s try some examples of this file regarding copying files in Linux.

Copying a File in the Same Directory (Creating a Duplicate)

If you want to duplicate the file within the same folder, then you have to pass in the file’s name to be copied and the name of the duplicate file. For example, we have the following in a directory:

To create the duplicate of the file “” as “,” open a terminal and head inside the directory where the file is placed. After that, run the following command:


Afterward, you can verify that the file has been copied into the same directory. You can run the “ls” command or view the directory in the explorer:

Copying a File in a Different Directory

In case you want to copy the file in a different directory than the pwd, then you simply pass in the path of the directory and the target file in the cp command. The command template is given as:

cp sourceFilePath targetFilePath

To showcase the working of this command, let’s copy the “” from the previous example onto the Desktop. To do this, the command will become:

cp ~/Desktop/

Verify that the copy process was a success by running the ls command on the Desktop:

Note: Remember to use the tilde symbol (~) if you are passing the complete path from the root. Otherwise, the Linux Kernel will try to locate the directory in the targetPath as a sub-directory of the pwd.

Copying Multiple Files Into a Directory in Linux

To copy multiple files into the same with the same name as the source, you need to specify all the files to be copied on by one in the cp command and pass the destination directory at the end.

To demonstrate this, we have the following files on the Desktop:

Let’s copy the files “”, “helloWolrd.txt” and the “” into the directory “CopyIntoMe” by using the following command:

cp helloWolrd.txt ~/Desktop/CopyIntoMe/

Verify that the files have been copied into the “CopyIntoMe” directory:

You have successfully copied files into a specific directory.

How to Copy a Directory Using the cp Command in Linux

The cp command can be used to copy one directory into another directory. However, most people make the mistake of using the command “cp dirSource dirTarget.” That doesn’t work because the Linux Kernel will not copy the sub-directories and the files inside the directory. 

You need to use the flag “-r,” which stands for “recursive.” This flag executes the copy command for every subdirectory and file inside the directory that is to be copied. 

To illustrate the working of the cp command with the “-r,” we have the following directories on the Desktop:

To copy the “CopyIntoMe” directory in the “MyDir,” use the following command:

cp -r CopyIntoMe/ MyDir/

After the execution of this command, you can verify if the file has been copied or not:

You have successfully copied the directory into another directory.

Affordable VPS Hosting With Dracula Servers

Dracula Servers offers high-performance server hosting at entry-level prices. The plans include Linux VPS, Sneaker Servers, Dedicated Servers & turnkey solutions. If you’re looking for quality self-managed servers with high amounts of RAM and storage, look no further.

Dracula Server Hosting is also Perfect for Hosting Telegram.Forex App with built-in support for MT4 with trade copier. Check the plans for yourself by clicking Here!

How to Copy Files With the cp Command Without Overwriting Files

The default behavior of the cp command is to copy the files from the source to the target. And if the files already exist with the same name in the target destination, then the file will be overwritten even without a prompt. 

To know when an overwrite is about to happen, use the “-i.” This flag is known as the interactive flag, and its alternative is “–interactive.” When a cp command is executed with an overwrite about to happen, you will get a similar prompt:

If you wish to continue with the overwrite, you must type “y” and then press the Enter key. 

Wrap up

You can use the built-in cp command to copy a file or a directory in Linux and in a Unix-based operating system like macOS. The cp command itself stands for “copy.”

The cp command takes the path of the source file to be copied and the path of the destination file. You can use the command to create a duplicate of a file as well. However, if you want to copy a directory and its content, you will have to use the “-r” flag with the cp command. The “-r” flag stands for recursive, and it causes the cp command to be executed for every sub-directory and file within the directory to be copied.

Check out more useful Linux/Ubuntu Tutorials Here!

Notify of
Inline Feedbacks
View all comments
× Dracula Servers

Subscribe to DraculaHosting and get exclusive content and discounts on VPS services.