Video summary
The `cp` command is a fundamental utility in Linux used to create copies of files or directories, and this video outlines its primary functions including renaming files, moving multiple files into a directory, preventing accidental overwrites, and duplicating entire folder structures. The simplest application involves copying a single source file to a new destination with a different name, such as creating `cash_copy.txt` from `cash.txt`. In this process, the original file remains untouched while a new file with identical content and size is generated in the target location, effectively allowing users to preserve data or create backups without altering the source.
When handling multiple files, the command syntax allows users to specify several source filenames followed by the name of an existing directory where they should be placed. It is crucial to note that the destination directory must already exist; attempting to copy files into a non-existent folder will result in an error message indicating that the file or directory cannot be found. To resolve this, the user must first create the target directory using commands like `mkdir` before executing the copy operation, ensuring that the shell has a valid location to store the incoming files.
A significant safety feature discussed is the interactive mode, which prevents destructive overwriting of existing files. By default, if a command attempts to write to a file that already exists, Linux will silently overwrite it without warning, potentially leading to data loss. To avoid this, users can employ the `-i` or `--interactive` flag, which prompts the system to ask for confirmation before replacing any existing file. This allows the user to review each instance and choose whether to proceed with the overwrite or decline by typing 'N', thereby maintaining control over their file integrity during batch operations.
Finally, the video explains how to copy entire directories using the recursive option, represented by `-r` or `--recursive`. Without this flag, attempting to copy a directory results in an error because the command only handles individual files. When recursion is enabled, the system copies all contents, including subdirectories and their nested files, from the source to the destination. The behavior differs depending on whether the target directory exists; if it does not, the shell creates it automatically, but if it does exist, the source directory's contents are merged into it, which can lead to duplicate structures if not monitored carefully. To visualize these complex file relationships, the `tree` command is recommended as a helpful tool to display the hierarchical structure of directories and files clearly.
Read the full video transcript
The CP command makes a copy of a file or
files.
Here are the uses of CP that we will
investigate in this video.
Copying a single file to a new name.
Copying multiple files to a directory.
How to avoid overwriting files.
And copying one directory to another.
Here's the simplest use of copy. To copy
one file to a new name.
The general pattern is CP.
The name of the source file, the
original.
And the destination file name or target
file name.
Here is our current directory.
Let's do an LS -L to see its contents.
We have a folder named animals and three
text files.
Let's see what's in each of those text
files.
In cash.txt.
We have a cash balance.
In credit.txt.
We have a credit card balance.
And in debit.txt.
That's how much money we have available
on that card.
Now, let's make a copy of cash.txt.
Which is our original, the source.
And the destination will be a new file
called cash_copy.txt.
And you can see that it appears here.
And if we do another LS -L.
We'll see that it has exactly the same
size as cash.txt.
And if we
display them both,
they're identical.
Our next task is to copy one or more
files to a directory.
The pattern is cp,
one or more source files, that's what
the dot dot dot stands for,
and then a target directory where those
files ought to go.
When you copy files to a directory, the
directory must already exist.
Let's clear the screen and show our
files again.
And if I try to copy
cash.txt,
credit.txt,
and debit.txt
to directory called money,
I'll get an error message that says
there's no such file or directory.
So, let's create that directory.
And you can see it shows up here,
and also in our ls -l listing.
Now, the copy will do what I want. And
rather than retype the whole thing, I'm
going to use the up arrow to page back
through my previous commands,
and then try that.
And if we now do an ls
you'll see it has those three files in
it.
And that's reflected here.
And that's how you copy multiple files
into a directory.
The next thing we need to learn is how
to avoid overriding files. And to do
that, we use the interactive option.
It has a long option form {dash} {dash}
interactive when you're copying a single
file to a new name.
And you can use the short form {dash} I
when copying a single file.
You can use the long form or the short
form when you're copying multiple files
into a target directory.
Now, the question is, why do we need
this?
The reason we need this is because
copying is destructive.
If I were to accidentally copy
credit.text
to cashcopy.txt
Linux would overwrite the existing
cash_copy.txt
file without giving me any warning.
In fact, when I show what's inside of
cashcopy
.txt
it doesn't have what I want.
How do we avoid problems like this?
We avoid them by having Linux warn us
when we're about to overwrite an
existing file.
Let me put things back the way they were
originally.
And this time I'm going to use an
interactive copy
to copy cash.txt
to cash_copy.txt
And this time it's going to ask us, are
you sure you want to overwrite the one
that's already there? In this case, I'm
sure, so I'll type Y for yes. Use Y for
yes and anything else for no.
And now, if I look at what's in cash.txt
Excuse me, cashcopy.txt
It has the correct contents.
Let's say I do something like this using
the short form for the option,
copy interactively debit.txt
to cash_copy.txt.
Again, it asks, do you want to overwrite
it? And this time, oh, wait a minute,
that's the wrong one. I'm going to type
N for no.
And now, when I look at cash copy.txt,
it still has the cash balance.
This also works when moving multiple
source files to directory.
So, for example, I can say cp -i
star.txt
to the money folder.
And it asks, do I want to overwrite
money/cash.txt?
And I'll say no.
Yes.
And no.
Finally, let's see how to copy a
directory to another directory.
Here is the animals directory.
And let me expand it in the tree view.
It turns out that all three of those
animals are mammals. So, we could try
this.
cp animals mammals
And it says, -r not specified, omitting
directory animals. So, nothing changed.
It's an error.
What we need to use is the recursive
option.
And let me explain what that is.
When you're copying from a source
directory to with
directory, you have to either use the
long option {dash} {dash} recursive
or the short option {dash} R or {dash}
capital R.
The recursive option will copy all the
files and subdirectories and their files
and subdirectories
from the source to the destination,
which is normally what you want when you
copy an entire directory. You want the
entire contents of the source directory
to go to the entire contents of the
destination.
Recursive copying works differently
depending on whether the target
directory exists or not.
If the target directory doesn't exist,
the shell will create the target
directory
and then copy all the files and
subdirectories from the original source
directory to the target.
If the target directory already does
exist,
it will copy the source directory and
all of its files and subdirectories to
the target directory.
Let me show you this in action.
I'm going to say recursively
copy the animals directory
to a new directory called mammals.
And now we have a new directory and
inside of it are the three files that
came from animals.
Now that the mammals directory does
exist, watch what happens when I do the
command again.
Now it has taken a copy of the entire
animals directory and put it into the
mammals directory.
If you don't pay attention to this
difference between target directories
that don't exist and do exist, you may
end up having multiple copies of a
directory in places you didn't expect
them.
And again from the command line,
we can say ls -l animals.
And we can say ls -l mammals.
And inside that, we can say ls -l
mammals
/animals.
Now, if you see things this way, it's a
little bit difficult to tell how the
files are structured.
Whereas when you see this file tree here
over at the left, it's pretty clear to
see what's going on.
Gosh, wouldn't it be nice if I could do
that from the command line?
Turns out you can do that.
Let's clear things out here. And what I
can do is I can say
tree
mammals.
And that will show the file tree for the
mammals folder.
Or I could say tree dot, which will show
me everything in the current directory.
That's what dot stands for.
As a file tree.
The tree command is very useful to help
you get a picture of what's really going
on in your file structure.
And those are the basics of using the cp
command for copying files.