Copy Command in Linux

Copy-Command-in-Linux.jpg

Copy Command in Linux is used to copy a file and a directory from one location to another. It is represented as cp and includes various other options that are used by the user to make it more efficient. It can also be used to copy multiple files and directories, with some access permissions. The copy command not only allows copying data locally but can also copy files to remote systems. In this guide, we will discuss how to use the copy or cp command in Linux, with its options, with examples, and working in detail.

Table of Contents:

What is the Copy Command in Linux?

The copy command is denoted as cp in Linux and is used to copy files and directories from one location to another. It is one of the most widely used commands in Unix-like operating systems for file manipulation. The main purpose of the cp command is to create an exact copy of a file or directory, including its metadata such as permissions, timestamps, and ownership.

Syntax of the cp Command in Linux

The syntax of the copy command in Linux is as follows:

cp [options] source destination

In the above syntax,

  • source refers to the file or directory you want to copy
  • destination refers to the location where you want to copy it
  • options are the flags that modify how the copy command will behave, like preserving timestamps.

Before demonstrating file copy operations, let’s create directories named intellipaat and intellipaat2. There are two files named sample1.txt and sample11.txt inside the directory intellipaat, and sample2.txt and sample22.txt inside the directory intellipaat2.

Syntax of the cp Command in Linux

In the above image, the command

  • mkdir -p intellipaat makes the directory named intellipaat
  • cd intellipaat moves from the current directory to the intellipaat directory
  • touch sample1.txt creates a file sample1.txt
  • cat > sample1.txt allows you to insert the content inside the file. After writing the content, press Ctrl+D to save the file.
  • cat sample1.txt displays the content present inside the file
  • cd .. allows you to move back to the directory.
Master DevOps Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

In the same way, create the following additional files and directories, and create another file inside it named sample11.txt, and in the directory named intellipaat2, the files sample2.txt and sample22.txt, with the content as below.

Copy Command in Linux

How to Copy Multiple Files in Linux?

The cp command in Linux is used to copy multiple files at once from a specific directory to another directory. It is mainly used when the user is backing up files.

Below is the syntax of the cp command to copy multiple files.

cp file1 file2 file3 ... destination_directory/
  • file1, file2, and file3 are the files you want to copy
  • destination_directory is the directory in which the copy of the files is made

Example:

How to Copy Multiple Files in Linux

The above command copies the files sample1.txt and sample11.txt from the intellipaat directory into the temp directory inside intellipaat.

How to Copy Multiple Directories in Linux?

To copy multiple directories in Linux, we can use the cp command with the -r or -R option, which allows us not just to copy a directory, but also the content present in the directory, i.e., subdirectories and files.

Below is the syntax of the cp command to copy multiple directories

cp -r source_directory/ destination_directory/

Example:

Before applying the command:

How to Copy Multiple Directories in Linux

After applying the command:

How to Copy Multiple Directories in Linux

The above command copies the entire temp folder (including all its files and subfolders) from the intellipaat directory into the intellipaat2 directory.

How to Copy Files That Match a Glob Pattern in Linux?

The glob pattern refers to the simple way to match filenames using the wildcard characters, like * is used to match several characters, and ? is used to match exactly one character. In Linux, the glob patterns are used with the cp command to copy multiple files that follow a specific naming pattern or extension. It is useful when dealing with a large number of files.

Below is the syntax of it:

cp [options] <glob-pattern> <destination_directory>
  • [options] are the flags like -i, -r, -v, etc
  • <glob-pattern> is the pattern to match source files
  • <destination_directory> is the directory where the matched files will be copied

Example:

Before using the command:

How to Copy Files That Match a Glob Pattern in Linux

After using the command:

How to Copy Files That Match a Glob Pattern in Linux

The above command copies the files sample1.txt and sample11.txt from the intellipaat directory into the intellipaat2 directory. The sample* pattern matches all files that start with the word sample, and the ../intellipaat2/ points to the destination directory.

Copying and Renaming a File in Linux

To copy a file and rename it at the same time in the same directory, you can use the cp command by specifying the new filename as the destination filename.

Below is the syntax of it:

cp source_file destination_file
  • source is the original file to copy
  • destination_file is the new name of the file

Example:

Copying and Renaming a File in Linux

The above command changes the name of the sample1.txt to the new_sample.txt in the intellipaat folder.

Now, we will discuss how to copy a file with options.

Get 100% Hike!

Master Most in Demand Skills Now!

Options of the cp Command in Linux

Now, let us discuss different options of the cp Command in Linux.

1. Copy a File in Linux Using the -i Option

The -i (interactive) option in the cp command is used to prompt the user before overwriting an existing file in the destination, which helps to prevent accidental data loss.

Below is the syntax of it:

cp -i source_file destination_file

Example:

Copy a File in Linux Using the -i Option

The above command creates a new file, new_temp.txt, in the temp folder, by confirming the user.

2. Copy a File in Linux Using the -f Option

The -f option of the cp command stands for force, and forces the copy operation by overwriting the destination file without prompting from the user. It should be used carefully, as important data can be overwritten without any warning.

Below is the syntax of it:

cp -f source_file destination_file

Example:

Copy a File in Linux Using the -f Option

The above command copies temp1.txt and overwrites new_temp1.txt without asking the user.

3. Copy a File in Linux Using -r or -R Option

The -r or -R (recursive) command in Linux is used to copy directories and their complete content.

Below is the syntax of it:

cp -r source_directory/ destination_directory/

OR

cp -R source_directory/ destination_directory/

Example:

Before applying the command:

Copy a File in Linux Using -r or -R Option

After applying the command:

Copy a File in Linux Using -r or -R Option1

The above command copies the entire temp folder (including all its files and subfolders) from the intellipaat directory into the intellipaat2 directory.

Note: The reason both exist is for historical compatibility. Some older UNIX systems only recognized -R, but Linux supports both.

4. Copy a File in Linux Using the -p Option

The -p option in the cp command is used to preserve the original files’ attributes while copying another file. These attributes include

  • Timestamps (modification and access time)
  • Owner (user and group)
  • File permissions (read, write, or execute)

Below is the syntax of it:

cp -p source_file destination_file

Example:

Copy a File in Linux Using the -p Option

Below are the stats of the sample22.txt after the cp command, and the new_sample.txt is created with the same stats as sample22.txt, except for the change and birth time as below.

5. Copy a File in Linux Using the * Option

In Linux, the * option is used as a wildcard character that allows you to copy multiple files that match a pattern using the cp command. * matches zero or more characters and helps in copying many files without listing them all manually.

Below is the syntax of it:

cp *<file_format> destination_directory/
  • file_format can be .txt, .pdf, and so on.

Example:

 Copy Files Using Wildcards in Linux

The above command cp *.txt temp copies all the files present in the current directory (intellipaat) to the temp directory.

6. Copy a File in Linux Using the -v Option

The -v option in the cp command stands for verbose, meaning to display a message for each file being copied, which makes it easier to see what is happening during each operation.

Below is the syntax of it:

cp -v source_file destination_file

Example:

Copy a File in Linux Using the -v Option

The command cp -v sample22.txt new_sample.txt makes a copy of sample22.txt as new_sample.txt, showing a change that has occurred.

7. Copy a File in Linux Using the -n Option

The -n option of the cp command stands for no clobber and prevents the file from overriding an existing file at the destination. It is used to avoid accidental overwrites and with the scripts that should not modify existing files.

Example:

Copy a File in Linux Using the n Option

The command cp -n sample22.txt temp/new_sample.txt makes a copy of the file sample22.txt from the current directory to the temp folder, giving a warning as above. The warning is not standardized across all systems and encourages portable scripting, i.e., writing commands that will work reliably on all platforms.

Note: Not all systems display a warning when using -n. However, this option supports portable scripting by avoiding unintentional overwrites.”

The scp Command for Secure Remote Copying

The scp command in Linux is used to securely copy files and directories between two systems, i.e., between a local remote host and a remote host, or between two remote hosts. It transfers data over SSH (Secure Shell), by encrypting both files and passwords. Since it uses SSH, it makes sure that the data transferred provides the following properties:

  • Confidentiality
  • Authentication
  • Integrity

scp has the following features:

  • It is secure, as all traffic is encrypted.
  • It is fast because files are directly transferred, without any intermediate storage, but less flexible to use for beginners.
  • It can be used within Linux, macOS, and Windows, hence cross-platform.

scp command uses the following syntax,

scp [options] source destination

options refer to the flags that modify how the command behaves.

Example:

 scp Command for Secure Remote Copying

The above command takes the file sample1.txt from your local machine and copies it to the remote user [email protected], and /home/akshat/intellipaat2 is the destination path on the remote machine where the file should be saved.

Unlock Your Future in Linux
Start Your Linux Journey Today!
quiz-icon

Conclusion

From the above article, we learnt that the cp command in Linux is one of the most widely used commands for daily purposes. It can be used to copy a single or multiple files and directories from one location to another. There are many options present in the cp command that make it more important for the user, such as the -i option, which prompts before overwriting existing files, and so on, as discussed above. The scp command securely copies files between local and remote systems using SSH.

If you want to learn more about the cp command in Linux in detail, you can refer to our Linux Course.

Copy Command in Linux – FAQs

Q1. What is the copy command in Linux?

The cp command is used to copy files or directories in Linux.

Q2. How do I copy a file on Linux?

To copy a file in Linux, use cp source.txt destination.txt.

Q3. How to copy a directory in Linux?

To copy a directory in Linux, use cp -r source_dir/ target_dir/.

Q4. How to copy a shortcut in Linux?

To copy a shortcut in Linux, use cp -P shortcut shortcut_copy.

Q5. How to use the cut command in Linux?

To use the cut command in Linux, use cut -d’:’ -f1 file.txt to extract specific fields from a file

Full Stack Development