EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Copy Command in Linux

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Copy Command in Linux

Copy Command in Linux

Introduction to Copy Command in Linux

Cp command is an abbreviated form of “Copy”.As the name suggests, we can replication either the file or the directory in a particular ecosystem like Unix, Linux, Windows or even Mac Operating system.

Copy Command(cp) is based on a command-line terminal used for copying files/directories. This command creates an exact image of a file/directory with the same/different file name on the disk.cp command takes at least two arguments as input.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Nowadays its very command to use cp for replicating the files and rysnc command for copying the directories. Copying of file is different from moving a file from source to destination.

Prerequisite

To copy a file using cp command is that the user must have permission for source and target files or directories.

Syntax

1. cp source destination

2. cp source Target_Directory

3. cp source1 source2 source3 sourceN Target_Directory

4. cp [options] source destinations

5. cp [option] source directory

where the first & second syntax we will copy source file to destination files or directories

Moreover, the third syntax we can copy multiple source files to target directory and fourth and fifth syntax is with options that we can use for different purposes.

Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,494 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Options

1. cp -a: This option is used to archive the existing files in the directory for retention purpose.

2. cp -f:  This option forcefully copy the files even it may remove the target file if needed. It is applicable if the file is already in use.

3. cp -i: This option stands for interactive mode, which means that it will ask the user to overwrite the file by prompt.

4. cp -l: This option is used to link file with other existing ones instead of copying it.

5. cp -L: It is will create a symbolic link for the file.

6. cp -n: This Option is used not to overwrite any existing file.

7. cp -R: This option means recursive copy means that it will copy all files with a cascading directory including hidden file.

8. cp -u: This means update, copy when the source file is new than the destination file.

9. cp -v: This option stands for verbose which means that will it print all the process which happens on a file while copying.

Examples of Copy Command in Linux

Now let us discuss each option with an example for a better understanding of the concept.

1. Copying file to a target directory

Suppose we want to copy the /application/Praveen/file.txt file to /application/Praveen/Backup directory, so need to run below:

Command :

cp /application/Praveen/file.txt /application/Praveen/Backup

Output :

Copy Command in Linux output 1

The above output shows that the file has been copied at the target directory.

2. Multiple file copy at the same time

Command :

cp /application/Praveen/file1.txt /application/Praveen/file2.txt /application/Praveen/Backup/

Output :

Copy Command in Linux output 2

3. Copying the file in interactive mode (-i)

If we want to copy the file in interactive mode, then we will use the option “-i”.Interactive Mode work if the same file already exists in the target directory already.

Command :

cp -i /application/Praveen/file1.txt /application/Praveen/Backup/

Output :

Copy Command in Linux output 3

Note: We have to manually type ‘y’ to start copy operation.

4. Copying File with Verbose mode enabled

We will use “-v” option for this.

Command :

cp -v /application/Praveen/file1.txt /application/Praveen/Backup/

Output :

Copy Command in Linux output 4

If we want to use both verbose and interactive mode then use option “-iv”

Command :

cp -iv /application/Praveen/file1.txt /application/Praveen/Backup/

Output :

Copy Command in Linux output 4.2

5. Copying a folder or directory (-R or -r)

In order to copy a directory from one to another place, we will use -R or -r options

Command :

cp -r /application/Praveen/Testing /application/Praveen/Backup/

Output :

Copy Command in Linux output 5.1

The above output shows the files and directory will be recursive.

Let us verify the contents :

Source :

Copy Command in Linux output 5.2

Target:

Copy Command in Linux output 5.3

6. Archiving of files and directory

We will use the option “-a” instead of “-r” or “-R” options

Command :

cp -a /application/Praveen/Testing /application/Praveen/Backup/

Output :

output 6

7. Copying of file only when new source arrives

There are cases where we want to copy files only when the source file is newer than the target by using the option “-u”. We have added file2.txt at the source.

Command :

cp -v -u /application/Praveen/Testing/file*.txt /application/Praveen/Backup/Testing/

Output :

output 7

Only file2.txt has been updated in the target directory.

8. If not to overwrite the existing file

There may be the case where we do not want to overwrite the existing file in the target.

In this case use “-n” option

Command:

cp /application/Praveen/Testing/file*.txt /application/Praveen/Backup/Testing/

Output: Without using -n option

output 8

Command :

cp -n /application/Praveen/Testing/file*.txt /application/Praveen/Backup/Testing/

Output :

output 8.2

9. Creation of symbolic link of a file

In case we need to create a symbolic link instead of copying then we will use “-s” option.

Command :

cp -s /application/Praveen/Testing/file2.txt /application/Praveen/Backup/Testing/

Output :

output 9

In the above output, we can see that file2.txt colored and pointing to the link.

Also, the permission is “lrwxrwxrwx”, where l stands for a symbolic link.

10. Creation of Hard link of a file

There are cases where we need to have a hard link instead of the symbolic link and copying of file, so we will achieve this by the option “-l”.

In Hard link inode, no. of source and link file will be the same.

Command :

cp -l /application/Praveen/Testing/file.txt /application/Praveen/Backup/Testing/

Target file inode :

output 10.1

Source file  inode :

output 10.2

We can see that the inode is “131081” for both source and target files.

11. Creation of backup file of existing destination

As we know the by default cp command overwrites the file if exists. If we need to take the backup then we have to use “–backup” option, where we have to give the path at which backup to be taken.

Command :

cp --backup=simple -v /application/Praveen/Testing/file.txt /application/Praveen/Backup/Testing/file.txt

Output: It is created the same file with the extension(~) on the target path.

12. Copying files/directory forcefully(-f)

There are cases where the existing destination file cannot be opened and removed and if we want to copy the file in place of the existing destination file, then the “-f” option can be used.

Command :

cp -f  /application/Praveen/Testing/file.txt /application/Praveen/Backup/Testing/

Recommended Articles

This is a guide to Copy Command in Linux. Here we discuss the examples of Copy Command in Linux and its different applications in the Linux system. You may also have a look at the following articles to learn more –

  1. du Command in Linux
  2. chmod Command in Linux
  3. SCP Command in Linux
  4. mv command in Linux

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Kali Linux Training Learn More