Let's move on and see how to copy files and directories using the cp command, which stands for copy. 1
Cp has three principal modes of operation, depending on the number and the type of arguments passed. 2
If the command has two arguments of type files, it copies the contents of the first file to the 3
second file. 4
If the second file does not exist, cp will create it. 5
But if it already exists, cp will overwrite the destination file. 6
I'm copying the password file from /etc/directly to another file in the current working directory, so 7
cp /etc/passwd 8
./users.txt the destination file users.txt that doesn't exist, so it will created. 9
This is the file in the current working directory. 10
Now I'm trying to copy another file to the same destination. 11
cp 12
etc/group 13
users.txt 14
And cat users.txt There's another content. 15
The users.txt file already exists so it has overwritten the file without prompting. 16
To show files that are being copied pass the -v option to the cp command. This prints the file and 17
folders that are being copied to standard output. I'm running the last command again, this time passing 18
the -v option. 19
As usual, -v comes from verbose. 20
And if you wanted to prompt before overwriting add the -i option. 21
Let's run it again without any option. 22
The users.txt file already exists and it’s not prompting, this is the default behavior, but if 23
I add -i 24
it will prompt. 25
For beginners, what is the most important and difficult at the same time is to use the correct path for both 26
the source and the destination. 27
Always use the tab key for auto completion. 28
The second mode of operation is when cp has one or more arguments of type file, which is the source 29
and the last argument of typed directory, which is the destination. In this case, it copies the source 30
files to the destination directory, creating any files that do not already exist in the destination. 31
Let's see an example: cp learning_linux.txt logs users.txt and the destination. 32
The destination will be the directory called first. 33
It will copy these three files to the destination directory. 34
add the - i option, which comes from interactive, if you wanted to prompt before overwriting the 35
destination file, if it already exists. 36
It's not prompting and now it will prompt. 37
And the third and the last mode of operation is when all arguments are of type directory. 38
In this case,cp copies all files in the source directory to the destination directoy creating 39
any files or directories needed. 40
This requires -r, an option that indicates the recursive copying of directories. 41
Let's see an example sudo cp -r/etc ~ 42
/desktop The next command will be run as root because I'll copy the /etc/ directory to my Desktop. 43
A non-privileged user is not allowed to read some of the files in the /etc directory: 44
Look how a directory called etc has appeared on my desktop. 45
And of course, without the -r option, I would have gotten an error. 46
It's not allowed to copy a directory without specifying this option. 47
Let me show you one last example. 48
Consider this file called users.txt, which belongs to the user student. 49
Student is the file's owner and another user that has read access wants to copy that file; I'll copy the file 50
as root. 51
sudo cp users.txt u.txt 52
ls-l this is the new file. 53
What do you notice? When copying a file 54
the user that runs the cp command becomes the files owner and that's not good in some cases. To preserve 55
file attributes, permissions, group and user ownership pass the -p option, which stands for a 56
preserve. 57
By default, the permissions, ownership and timestamps are not preserved. 58
I'll copy that file again this time adding the -p option. 59
Let's say u1 and cp-p 60
Pay attention to these three files: the permissions, owner and timestamps were preserved for u1.txt 61
because I've used the -p option.