Chapter 2. Linux Key Commands

Cp (Copy File and Directory)

Cp (Copy File and Directory)
Tag:

The cp (CoPy) command is used to copy a file or directory. You can specify the destination directory to create a copy of the file or directory. You can also use the -r option to create a copy of a directory.

Copy a file with the same name

There are two key arguments in the cp command.

  • 1st argument: source file path
  • 2nd argument: destination directory path (existing directory path)

For example, if you want to make a copy of file_a under your working directory to dir_a under your working directory, run the command below.

Command Line - INPUT
cp file_a dir_a

Copy a file with a different name

To make a copy with a new name, you can add the new file name in the 2nd argument after the directory path.

For example, if you want to make a copy of file_a with a new name file_a_cp to dir_a, run the command below.

Command Line - INPUT
cp file_a dir_a/file_a_cp

Copy a directory

To make a copy of a directory, use the -r option.

For example, if you want to make a copy of dir_a located under dir_b with a new name dir_a_cp, run the command below.

Command Line - INPUT
cp -r dir_a dir_b/dir_a_cp

The cp (CoPy) command is used to copy a file or directory. You can specify the destination directory to create a copy of the file or directory. You can also use the -r option to create a copy of a directory.

Copy a file with the same name

There are two key arguments in the cp command.

  • 1st argument: source file path
  • 2nd argument: destination directory path (existing directory path)

For example, if you want to make a copy of file_a under your working directory to dir_a under your working directory, run the command below.

Command Line - INPUT
cp file_a dir_a

Copy a file with a different name

To make a copy with a new name, you can add the new file name in the 2nd argument after the directory path.

For example, if you want to make a copy of file_a with a new name file_a_cp to dir_a, run the command below.

Command Line - INPUT
cp file_a dir_a/file_a_cp

Copy a directory

To make a copy of a directory, use the -r option.

For example, if you want to make a copy of dir_a located under dir_b with a new name dir_a_cp, run the command below.

Command Line - INPUT
cp -r dir_a dir_b/dir_a_cp
Tag: