fbpx cp Command in Linux: Syntax, Flags, and Practical Examples | Certificação Linux

Linux cp Command: Copy Files and Directories with Examples

📅 01/01/2019 ⏱ 2 min ✍️ Uira Ribeiro
Linuxcommands
Linux cp Command: Copy Files and Directories with Examples
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The cp command in Linux copies 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: Copies 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 overwriting the destination;
  • -i: Prompts the user before overwriting each file at the destination location;
  • -v: Shows the name of each copied file.

Examples:

Copies arquivo1 to arquivo2 in the same directory:

$ cp arquivo1 arquivo2

Copies arquivo1 to the /tmp directory:

$ cp arquivo1 /tmp

Copies arquivo1 located in the /tmp directory to the local directory. The "." (dot) symbol specifies the local directory:

$ cp /tmp/arquivo1 .

Copies all the contents of the /var/log directory, including all subdirectories, to the "loglocal" directory:

$ cp –Rf /var/log/ loglocal

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp