Account
Categories

cp command


cp = copy

Definition:

The cp command copies content from one file to another or to a new file. It can copy files recursively, so if a file is inside a directory, you can use its full path to copy it to another location.


Syntax:

cp [source_file] [destination_file]

Example 1:

cp school_file canteen_file

Output:

Terminal shows nothing.

But if you check canteen_file, you will see it now contains all the content of school_file.

The original school_file content stays safe.

Explanation:

After running the command, all content from school_file is copied into canteen_file without losing any content.

The content of the second file (canteen_file) is not lost; it now contains the copied content from school_file.


Example 2:

If you want to copy directories (folders) along with all their files and subdirectories, you need to use the -r (recursive) option.

cp -r folder1/ folder2/

Output:

No message appears on the terminal.

Explanation:

  1. This copies folder1 and everything inside it to folder2.
  2. Without -r, trying to copy a folder will give an error.