Skip to content

cp command on Linux (copy files)

The cp command in Linux copies the files to other files or to directories. The cp command can copy one file or multiple files.

The most common options are:

  • -d: Preserves links when copying files;
  • -p: Preserves all file attribute information, such as file owner, group, permissions, and date;
  • -R: Copy files recursively. Useful for copying files and directories below the specified directory;
  • -a: Does the same as the combined “-dpr” options;
  • -f: Forces the copy by writing over the destination;
  • -i: Ask the user before copying each file over the destination location;
  • -v: Shows the name of each file copied.

Examples:

Copy file1 to file2 in the same directory:

$ cp file1 file2

Copy file1 to the /tmp directory:

$ cp file1 /tmp

Copy file1 located in the /tmp directory to the local directory. The symbol “.” (dot) specifies the directory as local:

$ cp /tmp/file1.

Copy the entire contents of the /var/log directory, including all subdirectories to the “loglocal” directory:

$ cp —Rf /var/log/ loglocal