Question about cp (copy) command

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
776
Reaction score
633
Credits
4,916
I know that the syntax of the copy command is

Code:
cp [OPTION]… [-T] SOURCE TARGET

but I wonder - is it possible to copy several directories with it? For instance, if I have two source directories, like etc/ and usr/, and I wanna copy both of them to root, what will happen if I type this:

Code:
sudo cp -rv etc/ usr/ /

Will that copy both of them to root or will it copy etc/ into usr/ instead of into root?
 


Your destination ( / ) already has folders named etc and usr, so your example will only work if you are copying different etc and usr folders from another location. But if you do that, it should copy both folders to / as long as you execute the command from the folder where they are located.

If you run your example command from the root ( / ) location, it will probably fail and report that the folders are the same (it won't copy onto itself). That's what my Fedora does, but your Arch may be different.

I'm not clear if you mean /etc and /usr instead. If you do, and you change the command to give the proper path, then it will probably fail because it will not copy an existing folder onto itself, as I just said above, but you eliminate the confusion of where etc and usr are located.
 
The short answer is yes. You can copy multiple directories to a directory in the way you’ve described.
But you need to be careful if you’re copying local directories called etc/ and usr/ to the root directory / as it already contains directories called /etc/ and /var/.

If memory serves, cp will recursively copy anything in the etc/ and var/ directories to overwrite /etc/ and /var/.

But the danger is that files in /path/to/var and /path/to/etc/ might already exist in /etc/ and /var/.
And if that happens, any existing files in /etc/ and /var/ will be overwritten. Depending on where the files came from, they could also have different permissions.

So you need to be careful if you’re doing anything like that in the root file-system!

If you’re in your home directory somewhere - you still need to be a little careful/mindful, so you don’t accidentally overwrite newer versions of files with older versions. But you aren’t going to utterly bork your system - so it doesn’t matter as much as it does in the root file-system!

But there’s nothing stopping you from specifying any number of directories (up to the maximum argument length for Bash):
This:
Bash:
cp -r /path/to/somedirectory /path/to/otherdirectory /path/to/anotherdirectory /path/to/destination/
Will recursively copy three directories into another directory.
 
I know what the destination contains. If you must know, the thing I'm copying is a program that runs on qt4. The usr directory contains everything it needs to run properly, as if it has been installed through the package manager. It's an English-Bulgarian dictionary.

oqGN4Hz.png


The bash script contains these commands:
Code:
g openssl-1.1 && sudo cp -rv home/ / && sudo cp -rv usr/ /
because I wasn't sure if it will work if I execute the command as described in the first post.

And I'm writing it (here) usr/ instead of /usr bc of an old habit. Sometimes bash gets confused if you type "cp -rv /usr /" and says that /usr is not a file but a directory. So in order to avoid that error, you have to place the slash at the end of the word, like this: usr/ . That way bash knows it's a directory and doesn't return nonsense errors.
 


Top