What is the cp command?

QuestionsQuestions8 SkillsProBasic Files OperationsAug, 05 2025
0223

The cp command in Linux is used to copy files and directories from one location to another. It is a fundamental command-line utility that allows users to duplicate files or move them to different directories while preserving the original files.

Basic Syntax

The basic syntax of the cp command is:

cp [OPTION]... SOURCE DESTINATION
  • SOURCE: The file or directory you want to copy.
  • DESTINATION: The location where you want to copy the source file or directory.

Common Use Cases

  1. Copying a File:
    To copy a file from one location to another:

    cp file1.txt file2.txt

    This command creates a copy of file1.txt named file2.txt in the current directory.

  2. Copying a File to a Different Directory:
    To copy a file into a specific directory:

    cp file1.txt /path/to/directory/
  3. Copying a Directory:
    To copy an entire directory and its contents, you need to use the -r (recursive) option:

    cp -r dir1/ dir2/

    This command copies dir1 and all its contents into dir2.

Important Options

  • -i: Interactive mode; prompts before overwriting files.
  • -u: Copies only when the source file is newer than the destination file or when the destination file does not exist.
  • -v: Verbose mode; shows the progress of the copy operation.
  • -a: Archive mode; preserves file attributes and copies directories recursively.

Summary

The cp command is a versatile tool for file management in Linux, allowing users to easily duplicate files and directories while providing various options to customize its behavior.

If you have any more questions or need further clarification, feel free to ask!

0 Comments

no data
Be the first to share your comment!