FactorPad
Faster Learning Tutorials

Linux cp command summary with examples

cp allows you to copy files or directories.
  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.
Paul Alan Davis, CFA, October 13, 2016
Updated: July 30, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / cp command


The Linux cp command

Beginner

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

Video Tutorial

Linux cp command summary with examples (4:06)

Videos can also be accessed from the YouTube Playlist.

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.

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.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the mkdir command, hit Back.
  • To learn about how to move and rename files and directories with the mv command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / cp command



 
 
Keywords:
linux cp command
linux copy file
linux copy directory
linux copy recursive
linux copy interactive
linux copy permissions
unix cp
cp directory
copy folder linux
man cp
cp command in linux
linux cp tutorial
linux cp summary
cp command syntax
copy file linux
linux create subdirectory