Hello,
Can anyone help me understand the difference between cp , cp -r , cp -a and cp -p command in linux?
Thanks in advance!
Hello,
Can anyone help me understand the difference between cp , cp -r , cp -a and cp -p command in linux?
Thanks in advance!
First of all, cp
is of course the standard copy command. Unfortunately, if you want to copy a folder with subfolders in it, cp
on its own won’t include the contents of the subfolder. So you need the recursive flag -r
, to tell cp
that you want to go down into every subfolder and copy those contents too. Just make a folder with a sub-folder. Place some files in there and try copying some things to see the difference.
The -p
flag serves another case. Imagine you’re copying files belonging to another user. By default, the new files will belong to you. You can prevent that by adding the -p
flag. It will ‘preserve’ the attributes of the original file. You can test this yourself:
# make a file as a non-root user
touch my-file.txt
# copy the file as the superuser
sudo cp my-file.txt copied-file.txt
sudo cp -p my-file.txt copied-file-preserved.txt
Now use ls -l
to check the difference in properties. copied-file.txt
should belong to the root user. copied-file-preserved.txt
should belong to your own user (the one that created the original file.
cp -a
seems to be the same as cp -dRp
. So it combines the -p
, -R
and -d
flags. That last one, -d
, makes sure that links are preserved
I hope that helped somehow. You can try to play around with some of these things to find out for sure