国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home System Tutorial LINUX How to Use cp Command Effectively in Linux [14 Examples]

How to Use cp Command Effectively in Linux [14 Examples]

Jun 25, 2025 am 10:48 AM

Brief: In this easy-to-follow guide, we will discuss some practical examples of the cp command. After following this guide, users will be able to copy files and directories easily in Linux using the command line interface.

As Linux users, we interact with the files and directories from time to time. One common operation users perform is copying files and directories. Certainly, we can use a graphical file manager to perform the copy operation. However, most Linux users prefer to use the cp command due to its simplicity and rich functionality.

In this beginner-friendly guide, we will learn about the cp command. As the name suggests, the cp command is used to copy files and directories at a given path.

Throughout this guide, we will understand the usage of the cp command using practical examples that can be used on a day-to-day basis.

So let’s get started.

Table of Contents

Cp Command Syntax

The syntax of the cp command is identical to other Linux commands. At a high level, it is divided into two parts – options and arguments:

$ cp [OPTIONS] <source> <dest>
$ cp [OPTIONS] <source-1> <source-2> ... <directory>
</directory></source-2></source-1></dest></source>

In the above syntax, the square brackets ([]) represent the optional arguments whereas angular brackets () represent the mandatory arguments.

1. How to Copy a File in Linux

One of the basic use of the cp command is to copy a file into a current directory. Most of the time users perform this operation to take a backup of the important configuration.

For example, we often create a backup copy of /etc/ssh/sshd_config file before updating the SSH configuration.

To understand the usage, let’s create a simple file:

$ touch file-1.txt

Next, create a copy of the file using the following command:

$ cp file-1.txt file-2.txt

How to Use cp Command Effectively in Linux [14 Examples]

2. Show Copy Command Progress

In the previous example, we used the ls command to verify whether or not the file copy operation succeeded. However, it doesn’t make sense to use one more command just to verify the result of the previous commands.

In such cases, we can enable the verbose mode using the -v option, which provides diagnostics for every processed file.

Let’s make a copy of the file-1.txt file using the following command:

$ cp -v file-1.txt file-3.txt

How to Use cp Command Effectively in Linux [14 Examples]

In the above output, the arrow represents the file that is being copied. The left side argument is the source file whereas the right side argument is the destination file.

3. How to Copy Multiple Files to the Directory

So far we worked with a single file and the current working directory only. However, in the real production environment, we have to work with a large number of files. One of the common use cases in such environments is to copy multiple files into a single directory.

Obviously, we can execute the cp command multiple times to achieve it, but that won’t be the most effective way. To perform such an operation effectively, we can use an alternative syntax of the cp command.

So, first, create a new directory with the name dir-1:

$ mkdir dir-1

Now, let’s copy all three files into the dir-1 directory using the single command:

$ cp -v file-1.txt file-2.txt file-3.txt dir-1

How to Use cp Command Effectively in Linux [14 Examples]

The above output shows that all files have been copied to the dir-1 directory. Also, it is important to note that, to use this alternative syntax the directory must be present already and it must be the last argument of the command.

4. How to Avoid Overwriting the File

By default, the cp command replaces the destination file, which means it will overwrite the file if it exists at the destination with the same name. However, we can disable this default behavior using the -n option.

To understand this, let’s try to overwrite the existing file:

$ cp -n -v file-1.txt file-2.txt

In this example, we have used the -v option to illustrate that the file-2.txt file hasn’t been overwritten.

5. How to Overwrite the File with Confirmation

In the previous example, we saw how to avoid the overwriting of the destination file. However, sometimes we want to overwrite the file destination in a safer way.

In such cases, we can use the -i option of the command to make the copy operation interactive. This option shows the warning message and waits for the user’s confirmation before overwriting the file.

To illustrates this, let’s try to overwrite the existing file:

$ cp -i file-1.txt file-2.txt

<strong>cp: overwrite 'file-2.txt'?</strong>

As we can see, the command is waiting for confirmation. Just like other Linux commands, we can use 'y' to continue or 'n' to abort the operation.

This default non-interactive behavior of the cp command is not very safe. There are chances that the user might overwrite an important configuration by mistake. Hence some of the Linux distributions enforce the interactive behavior by default using the alias command:

$ alias cp='cp -i'

6. Overwrite File Only if the Source Is Newer

In the previous example, we saw how to use the interactive mode. However, sometimes, a user might overwrite the newer file inadvertently.

To avoid such error-prone cases, we can use the -u option, which attempts copy operation only if the source is newer than the destination or if the file is not present at the destination.

First, update the timestamp of the source file:

$ touch -t 10101010 file-1.txt
$ ls -l file-1.txt

In the above example, we have used the -t option of the touch command to set the timestamp of the file to 10-Oct-2010.

Next, let’s update the timestamp of the destination file to the current time:

$ touch file-2.txt

Now, let’s try to perform copy operation using the -u option:

$ cp -u -v file-1.txt file-2.txt

Here, we can see that copy operation hasn’t been attempted because the destination file is newer than the source.

Finally, let’s swap the source and destination arguments and perform the copy operation:

$ cp -u -v file-2.txt file-1.txt

In the above output, we can observe that copy operation succeeds because the source file is newer than the destination.

How to Use cp Command Effectively in Linux [14 Examples]

7. How to Backup File Before Overwriting

We can instruct the cp command to take a backup of the destination file before overwriting it. To achieve this we can use the --backup option, which performs automated backups.

$ cp --backup=numbered -v file-1.txt file-2.txt

In this example, we have used the numbered backup policy. This policy uses incremental numbers in the backup file names.

To understand this, let’s execute the same command multiple times and observe the output:

$ cp --backup=numbered -v file-1.txt file-2.txt
$ cp --backup=numbered -v file-1.txt file-2.txt
$ cp --backup=numbered -v file-1.txt file-2.txt

How to Use cp Command Effectively in Linux [14 Examples]

8. How to Force Copy to Overwrite File

In the previous few examples, we saw how to overwrite the file in a safer way. However, in some rare cases, the requirement is to overwrite the file. However, there isn’t a guarantee that the operation will succeed every time.

For example, the copy operation will fail if the destination file doesn’t have the write permissions. Let’s illustrate this with an example.

First, change the permissions of the destination file:

$ chmod 444 file-2.txt
$ ls -l file-2.txt

Now, let’s try to overwrite the file-2.txt file:

$ cp file-1.txt file-2.txt

In the above output, we can see that the command has failed with the permission denied error.

To overcome this limitation, we can use the -f option, which deletes the destination files and attempts the copy operation if the destination file cannot be opened.

Now, let’s use the -f option to overwrite the file forcefully:

$ cp -f -v file-1.txt file-2.txt

How to Use cp Command Effectively in Linux [14 Examples]

9. How to Remove the Destination File Before Copying

In the previous example, we saw how to remove the destination file if there is an error while operating it. However, sometimes the requirement is to remove the destination file first and then perform the copy operation.

To fulfill such a requirement, we can use the --remove-destination option.

$ cp --remove-destination -v file-1.txt file-2.txt

How to Use cp Command Effectively in Linux [14 Examples]

In the above output, we can see that the cp command first removes the destination file and then performs the copy operation.

10. How to Create a Hard Link File Instead of Copying

We can create a hard link instead of creating a new copy of the source file. This option plays an important role when there is a scarcity of disk space.

So, let’s use the -l option to create a hard link:

$ cp -l -v file-1.txt file-4.txt

Now, let’s check the inode numbers of both files to verify the hard links:

$ ls -i1 file-1.txt file-4.txt

How to Use cp Command Effectively in Linux [14 Examples]

In the above output, the numbers in the first column represent the inode numbers.

11. How to Create a Soft Link File Instead of Copying

In a similar way, we can create a soft link instead of creating a new copy using the -s option as shown below:

$ cp -s -v file-1.txt file-5.txt

Now, let’s verify that the symbolic link has been created correctly:

$ ls -l file-5.txt

How to Use cp Command Effectively in Linux [14 Examples]

In the above output, the last columns represent the symbolic link relation.

12. How to Preserve the File Attributes While Copying

There are various attributes associated with the file, such as its access time, modification time, permissions, etc. By default, these attributes are not preserved while copying the file. To override this default behavior we can use the -p option.

To understand this, first, update the timestamp of the file-1.txt:

$ touch -t 10101010 file-1.txt

Now, let’s create a copy of this file by preserving all its attributes:

$ cp -p -v file-1.txt file-6.txt

Finally, verify the timestamp of the file-6.txt file:

$ ls -l file-6.txt

How to Use cp Command Effectively in Linux [14 Examples]

How to Use cp Command Effectively in Linux [14 Examples]

13. How to Perform Copy Operation Recursively

So far we saw how to copy a single file. However, often times we have to copy all files and sub-directories of the particular directory.

In such cases, we can use the recursive mode either using the -r or -R option.

So, let’s create a directory and add a few files and sub-directories under it:

$ mkdir -p dir-1/dir-2
$ touch dir-1/file-1.txt dir-1/dir-2/file-2.txt

Next, verify the directory structure has been created correctly:

$ tree dir-1

Now, let’s copy the directory dir-1 recursively using the following command:

$ cp -r -v dir-1 dir-3

Finally, verify that all files and sub-directories have been copied successfully:

$ tree dir-3

How to Use cp Command Effectively in Linux [14 Examples]

14. How to Copy Multiple Directories

Similar to files, we can copy multiple directories recursive using a single command. However, to achieve this the destination directory must be present already and it must be the last argument in the command. Let’s understand this with an example.

First, create a directory a new directory:

$ mkdir dir-4

Now, let’s copy the dir-1 and dir-3 directories into the dir-4 directory using the below command:

$ cp -r -v dir-1 dir-3 dir-4

In a similar way, we can use the -t option of the command to achieve the same result. This option allows us to specify the destination directory. So we can use it as a first argument as well:

$ cp -t dir-4 -r -v dir-1 dir-3

How to Use cp Command Effectively in Linux [14 Examples]

In this article, we discussed how to copy files and directories using the cp command. Beginners can refer to these examples in day-to-day life while working with Linux systems.

Do you know of any other best example of the cp command in Linux? Let us know your views in the comments below.

The above is the detailed content of How to Use cp Command Effectively in Linux [14 Examples]. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

5 Best Open Source Mathematical Equation Editors for Linux 5 Best Open Source Mathematical Equation Editors for Linux Jun 18, 2025 am 09:28 AM

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

SCP Linux Command – Securely Transfer Files in Linux SCP Linux Command – Securely Transfer Files in Linux Jun 20, 2025 am 09:16 AM

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

Gogo - Create Shortcuts to Directory Paths in Linux Gogo - Create Shortcuts to Directory Paths in Linux Jun 19, 2025 am 10:41 AM

Gogo is a remarkable tool to bookmark directories inside your Linux shell. It helps you create shortcuts for long and complex paths in Linux. This way, you no longer need to type or memorize lengthy paths on Linux.For example, if there's a directory

What is a PPA and how do I add one to Ubuntu? What is a PPA and how do I add one to Ubuntu? Jun 18, 2025 am 12:21 AM

PPA is an important tool for Ubuntu users to expand their software sources. 1. When searching for PPA, you should visit Launchpad.net, confirm the official PPA in the project official website or document, and read the description and user comments to ensure its security and maintenance status; 2. Add PPA to use the terminal command sudoadd-apt-repositoryppa:/, and then run sudoaptupdate to update the package list; 3. Manage PPAs to view the added list through the grep command, use the --remove parameter to remove or manually delete the .list file to avoid problems caused by incompatibility or stopping updates; 4. Use PPA to weigh the necessity and prioritize the situations that the official does not provide or require a new version of the software.

Install LXC (Linux Containers) in RHEL, Rocky & AlmaLinux Install LXC (Linux Containers) in RHEL, Rocky & AlmaLinux Jul 05, 2025 am 09:25 AM

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

How to create a file of a specific size for testing? How to create a file of a specific size for testing? Jun 17, 2025 am 09:23 AM

How to quickly generate test files of a specified size? It can be achieved using command line tools or graphical software. On Windows, you can use fsutilfilecreatenew file name size to generate a file with a specified byte; macOS/Linux can use ddif=/dev/zeroof=filebs=1Mcount=100 to generate real data files, or use truncate-s100M files to quickly create sparse files. If you are not familiar with the command line, you can choose FSUtilGUI, DummyFileGenerator and other tool software. Notes include: pay attention to file system limitations (such as FAT32 file size upper limit), avoid overwriting existing files, and some programs may

How to install Linux alongside Windows (dual boot)? How to install Linux alongside Windows (dual boot)? Jun 18, 2025 am 12:19 AM

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

NVM - Install and Manage Multiple Node.js Versions in Linux NVM - Install and Manage Multiple Node.js Versions in Linux Jun 19, 2025 am 09:09 AM

Node Version Manager (NVM) is a simple bash script that helps manage multiple Node.js versions on your Linux system. It enables you to install various Node.js versions, view available versions for installation, and check already installed versions.NV

See all articles