cp command on Linux (copy files) [Basic Guide]

linux copy files using cp command

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

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?