- In this video, we are going to explore the CP command.
CP is for copy.
And you can use CP of somefile to somedir to copy a file.
This is the basic structure,
and I will show you exactly how that is working.
Now, the thing in this basic structure,
is what exactly is it going to do?
Well, it's going to look for the file somefile in /dir.
And then it creates it to /somedir.
Now the thing is, if /somedir does not exist,
it will copy the file into a file with the name somedir.
Now that might not be what you want to happen.
So if you want the command to fill
if somedir doesn't exist,
then you better use CP /dir/somefile to /somedir/
the slash behind it is telling you
that you really need the destination to be a directory.
And otherwise, it fills.
You can use CP minus A to copy all files,
including hidden files.
But use .* wildcard to include hidden files as well.
So what does that mean?
Well, that means that CP-A ~/.* to TMP.
is how you are going to copy hidden files.
I can tell you that copying hidden files is complicated
and maybe you better want to use the tar utility
which is much better utility to copy hidden files also.
And last thing is CP minus R, which will copy recursively.
Notice that's an uppercase R.
And what is recursively?
Well, that means including subdirectories.
Let me demonstrate.
All right, let's go have a look at some CP demo.
I'm using CP etc passWD.
What is passWD?
That's the user database.
I'm pressing enter.
What do we get?
We get missing destination.
Of course we do, because I did not specify
where I want to copy this file too.
I'm adding a dot to the command.
Dot is the current directory.
So copy etc passWD to the current directory.
There we go.
And LS minus L on passWD is showing
that it has copied the file to the current directory.
Of course, it can only copy files that we can read.
If for instance, I would use etc shadow,
which is a file that I can't read.
I'm getting permission denied.
All right, now there's something else
that I want to show you.
And that is CP, etc passWD.
Why wouldn't I work on the same file name?
To mydir.
Mydir, well, I hope that mydir is a directory.
And this command seems to be doing all right.
But if I use LS minus LD on mydir, what do we see?
We see it created a file with the name mydir.
That's not exactly what I had in mind.
So let me use RM to remove this file.
And let me do it again with one small modification.
CP etc passWD to mydir/
which means that it copies to a directory
with the name mydir in the current directory.
The slash is indicating that it must be a directory.
And if the directory does not exist,
it's giving me an error message.
And that's exactly what I want.
Now, if I do create a directory, I try it again.
Then it is working out all right.
Then we have this thing about hidden files.
You know what, I'm just going to use mydir
in order to do so.
You know what, I'm just going to use mydir
in order to demonstrate.
I'm using CP minus A of ./.* to mydir
which is doing what?
Well, it's giving an error message
because it's trying to copy things into itself.
Please ignore that.
Please ignore this error message as well.
I don't even wanna talk about it right now.
What I do wanna talk about is what it has done on mydir.
And we can see it has copied everything to my dir.
Everything?
Well, that is something that we are only going to figure out
if we include the minus A as well.
Minus A is taking care of all files,
including the hidden files.
But that's in LS.
Now in CP, the minus A needs the wildcard
to be slightly different.
Now in CP minus A, you also need to manipulate the wildcard.
What does that mean?
Let me create a new directory, take two.
And let me use CP minus A ./* to take two.
And let me do an LS minus A on take two.
And there we go, no hidden files.
So if you want to copy the hidden files,
you really need to refer to .* instead of just star.
And that's a little bit confusing.
And that is why I told you you might be better off,
if you really want to archive contents of a directory,
to use the tar utility for the tape archiver.
That's all about CP.
Let's move on.