cp Command in Linux

Learn via video courses
Topics Covered

Overview

The cp command is used to copy files and directories in Linux. It is a commonly used command that can be used for a variety of purposes such as backup, migration, and replication. In this article, we will discuss the syntax, options, and various usages of the cp command in Linux.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Linux cp Command Syntax

The syntax for the cp command is as follows:

Where:

  • options: The various options that can be used with the cp command.
  • source and destination: The source and destination parameters can be in one of the following formats:
    • Copy a file to a new location: cp /path/to/source/file /path/to/destination
    • Copy a directory to a new location: cp -r /path/to/source/directory /path/to/destination
    • Copy a file to an existing directory: cp /path/to/source/file /path/to/existing/directory/

cp Command Options:

  • -r: Recursively copy the source directory and its contents.
  • -p: Preserve the ownership, permissions, and timestamps of the original file or directory.
  • -f: Force the copy operation, even if the destination file or directory already exists.

Example Usages

  • Copy a file to a new location::

    Explanation: This command will create a new copy of the file located at /path/to/source/file and place it in the location specified by /path/to/destination.

  • Copy a directory to a new location::

    Explanation: This command will recursively copy the entire directory located at /path/to/source/directory to the new location specified by /path/to/destination.

  • Copy a file to an existing directory::

    Explanation: This command will create a new copy of the file located at /path/to/source/file and place it in the existing directory specified by /path/to/existing/directory.

Tips

  • To see the progress of the copy operation, use the 'pv' command in combination with the cp command. For example, pv /path/to/source/file | cp /path/to/destination.

  • To copy files and directories across servers, use the 'scp' command instead of cp.

Conclusion

  • The cp command is a simple and powerful tool for copying files and directories in Linux.

  • It can be used for a variety of purposes such as backup, migration, and replication.

  • By using the various options and formats, you can customize the behavior of the command to suit your needs.

  • Always be careful when using the cp command, as it can overwrite files and directories if used incorrectly.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more