Mastering the Linux Copy File Command With Examples

29/12/2020
Chưa phân loại
On any operating system, copying of files and directories is one of the most common actions a user makes. On the Linux command line, you can learn the proper usage of the cp command to customize and master file-copying according to your needs. In this article, we will explain the usage of the Linux cp command by presenting some useful examples.

We have run the commands and examples mentioned in this article on a Debian 10 Buster system but you can replicate them on almost all Linux distros.

The Linux command line, the Terminal, can be easily accessed through the Application.  Launcher search as follows:

Example 1: Copying a single file to a target directory

The simplest use of the cp command is to copy a single source file to a target directory. Here is the syntax you can use to do so:

$ cp sourcefile /target/directory

Example:

In this example, I am copying a file named sample_file.txt to my Documents folder:

Later, I verified the presence of the file in the target folder through the ls command.

Example 2: Copying multiple files to a target directory

With the cp command, you can copy multiple files to a target directory by using the following syntax:

$ cp sourcefile1 sourcefile2 sourcefile3…  /target/directory

Example:

In this example, I am copying two sample files to my Documents folder through the cp command:

Later, I verified the presence of these files in the target folder through the ls command.

Example 3: Copying of files in an interactive manner

If a file with the same name exists in your target folder, you can use the cp command to prompt you before overwriting the target file. You can use the -i switch in the following manner:

$ cp -i sourcefile /target/directory

Example:

In this example, a file named sample_file.txt already exists in my Documents folder, therefore the cp command, with the -i flag, prompts me if I want to overwrite it. The file will be overwritten if i enter y at this prompt.

Example 4: Copying files along with a Verbose output

A verbose output prints what the command is doing. This can be incorporated in the cp command with the -v switch as follows:

$ cp -v sourcefile /target/directory

Example:

You can see in the following example how the cp command prints a verbose output while copying a command:

Example 5: Copying a directory recursively

Using the -r option with the cp command recursively copies all files and folders in a directory to another location. This is how you can use the cp command in this scenario:

$ cp -r directory1 directory2

Example:

In the following example, the entire folder 1, with all its files, will be copied to folder2.

I later verified the contents of folder2 through the ls command. The folder2 contains a copy of the source folder now.

Example 6: Copying a directory recursively while keeping an archive

By using the -a switch with the cp command, you can do two things at once:

  • Copy files of a directory recursively to another directory
  • Keep the permissions, time stamp, symbolic links, and all such properties intact while copying files.

This is how you can use the cp command in this scenario:

$ cp -a directory1 directory2

Example:

In the following example, all entire folder 1, with all its files, will be copied to folder2. Also, the files will be archived as verified later through the ‘ls -l command’.

Example 7: Copy a file in case it is newer than the target file

Sometimes you want to copy a file to the target directory only if it is newer than the target file. This can be done by using the -u switch with the cp command:

$ cp -u sourcefileORsourcedirectory /target/directory

Example:

In this example, I edited a file sample_file.txt from my source folder. These files were already copied to the target folder before i edited the file. This can be seen in the following output of the ‘ls -l’ command:

Now, when I copied the source folder to the target folder, the verbose output verified that only that file will be copied that was edited by me.

Example 8: Copy a file but do not overwrite if a target file already exists

You can tell the cp command to not overwrite the file if it already exists in the target location. This can be done through the -n switch as follows:

$ cp -n sourcefile /target/directory

Example:

In this example, I first tried copying a file to the target location where it already existed. The -i switch prompted me if I want to overwrite it.

In the second command, I added the -n switch. It ignored the -i switch but did not overwrite the already existing file in the target directory.

Example 9: Create a symbolic link to a file in target directory

With the -s switch in the cp command, you can create a symbolic link to the source file in the target folder instead of copying the source file.

$ cp -s sourcefile /target/directory

Example:

In the following example, I created a symbolic link to the file sample_file.txt in the target folder.

I later verified the presence of the symbolic link in the target folder through the ‘ls -l’ command.

Example 10: Create a hard link to a file in target directory

When you create a hard link to a file in another folder, the file is not copied to the target folder; instead, a hard link to the source file is created. The inode number of both the source file and the copied hard link is the same. This is how to create a hard link:

$ cp -l sourcefile /target/directory

Example:

In this example, I created a hard link to the source file in the Documents folder.

Through the ‘ls -il’ command, I verified that both the original file and the hard link have the same inode numbers.

This was all you needed to know in order to master the cp command in Linux. By using a combination of these switched, you can make file copying much more customizable.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Fedora 28 in VirtualBox

Installing Fedora 28 would offer a lot of old-school Debian users a change of environment. Fedora’s 6 monthly release...
28/12/2020

RedHat Versus SUSE: The Battle of Enterprise Linux Distributions

The enterprise Linux space is crowded, with several established companies providing open source software products to the...
28/12/2020

Hướng dẫn cài đặt MacOS trên AWS EC2

Step 1: Configure MacOS Amazon Machine Image Let’s start creating an AWS EC2 Instance. Go to AWS console and navigate to...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024