/ factorpad.com / tech / linux-essentials / cp-command.html
An ad-free and cookie-free website.
Beginner
Learn to copy files and directories at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux cp Command Summary with Examples (4:06)
Our twenty-third word, or command to memorize is cp
from the category
Navigation.
cp
allows you to copy files or
directories.
-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.
-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.
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.
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.
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.
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.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
We'd love to have you join our growing YouTube Channel. Get notifications of updates through @factorpad on Twitter, or from our email list.
mkdir
command,
hit Back.mv
command, click Next./ factorpad.com / tech / linux-essentials / cp-command.html
A newly-updated free resource. Connect and refer a friend today.