FactorPad
Build a Better Process

Linux cp Command Summary with Examples

Linux cp allows you to copy files or directries.
  1. Purpose - Learn what cp is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with cp.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux cp, 23 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / cp-command.html


An ad-free and cookie-free website.


Examples of the Linux cp Command

Beginner

Learn to copy files and directories at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux cp Command Summary with Examples (4:06)

Video Script

The Command and Why You Need It

Our twenty-third word, or command to memorize is cp from the category Navigation.

cp allows you to copy files or directories.

Common Linux cp Options
-options description
-i, --interactive Prompt before overwriting files
-r, -R, --recursive Copy directories and their contents
-u, --update Copy when source is newer

Recall from the last video (tutorial) we created directories to hold our stuff, now we'll copy files and directories to hold that stuff.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The cp command has 31 options and two arguments, the first is the source and the second is the destination.

Like most commands, help is available with double-dash --help.

The -i, or interactive mode will prompt you before overwriting files, as the dangerous default with cp is to overwrite silently.

The -r recursive option is required to copy directory contents, and then -u only copies the source when it is newer, like a backup would.

So why is cp an important command? Well, cp is used for routine copies, backups and setting up file structures. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go, let's do a quick ls, and then make a copy, cp -i of video20.txt, which is the source, and call it video23.txt, the destination. In the same directory.

$ ls stuff video11.txt video16.txt video19.txt video20.txt video21.txt video23.txt video5.txt $ cp -i video20.txt video23.txt cp: overwrite 'video23.txt'? n $ _

Oh wow, look at that, good thing we used the -i, I forgot, I edited video23.txt already. Now try ls and cat to verify.

$ ls stuff video11.txt video16.txt video19.txt video20.txt video21.txt video23.txt video5.txt $ cat video23.txt # A file for video 23 on cp The basic syntax for cp: cp -option SOURCE DESTINATION

And that is the case. Look at that. So -i helped us in that case otherwise it would have overwritten.

Next, copy that file into the one of the two subdirectories within stuff, from the last video (tutorial), called morestuff and verify.

$ cp video23.txt stuff/morestuff $ ls stuff/morestuff video23.txt

And then last, let's copy that morestuff directory with the file, using the -r option, into another one called yourstuff, and then ls again to verify.

$ cp -r stuff/morestuff stuff/yourstuff $ ls stuff/yourstuff video23.txt

A Final Tip

Okay now you know how to use cp. And you know the syntax for commands, options and arguments.

One last tip about the cp command. Check out the -a option. Copying directories requires it, or -r, but here -a retains all of the permissions.

Okay, thanks for visiting today. I hope this was a helpful introduction to the cp command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

We'd love to have you join our growing YouTube Channel. Get notifications of updates through @factorpad on Twitter, or from our email list.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / cp-command.html


linux cp command
cp linux
linux cp
copy directory
linux copy directory
linux copy
copy file linux
copy recursive
copy interactive
cp command syntax
linux cp summary
linux cp options
linux cp tutorial
linux cp examples
linux copy file
cp command in linux
linux copy
man cp
cp unix
copy folder linux
cp directory
linux cp recursive

A newly-updated free resource. Connect and refer a friend today.