Categories: File Management

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

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.

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.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.