While working with Linux, you will commonly need to copy files as well.
The CP command is helping you to do so.
So how does it work?
Well, you use cpdir sumfile to sumdir to copy a file.
Now if the destination file is sumdir, then you probably want to
copy to copy it to a directory with the name sumdir.
But if you don't have sumdir currently existing, it will create some file to a file with the name sumdir.
Confusing? Yeah. And if you don't want that to happen, then you might better have the
command fill if sumdir doesn't exist and do so by putting a slash behind somdir.
Now when you are using CP you you might use CP A. You remember the A
option. In ls it includes hidden files as well. In CP it is doing the same.
So CP A copies all files including hidden.
But you need to use the dot star wealth card to make it complete.
And that means that your command is going to look like CP A tilde to tmp.
Now personally I wouldn't copy hidden files that way. You are probably better off using the TAR
utility which we will cover later. It's a much better solution to create archives of complete directories.
Also convenient is CP uppercase r
which will copy recursively including subdirectories.
Let me show you.
Okay, first I'm going to use CP etchosts to dot.
Hey, what is dot? Dot is current directory. So it copies the file hosts into the current directory.
And LS is showing that we now have a file with the name hosts.
Next I'm going to use CP uppercase r tmp to dot which is doing what? Well,
it's giving a couple of access messages on directories that I don't have permissions to.
But it will also copy the TMP directory and into the current directory.
Now let me copy CP etc hosts to newdir.
What do I want to do? Well, I want to copy the file etchost to newdir. And what do you think of that?
Well, LS L is showing that it has created a file with the name newdir
and it has the exact same size as the host file that we copied previously.
So why is that? Well, based on the name newdir, my shell cannot know that that it's actually
into a directory that I want to copy the file and that is why it created the file.
So if you really want it to be copied to a directory, then you would use
CP et host mydear put a slash behind the name of the directory and in case
the directory doesn't exist, then it will give you an error message, not a directory.
And that is exactly what we wanted here.
So the solution mkdir to create a directory and then
you can try it again and then it would work.
So this is about all that you should know about cp. Let's move on to the next video.