23

What is the difference between install -c and cp? Most installations tend to use install -c, but from the man page it doesn't sound like it does anything different than cp (except maybe the ability to also set permissions). When should I use install -c and when cp?

3 Answers 3

25

One significant difference is that cp truncates the destination file and starts copying data from the source into the destination file.

install, on the other hand, removes the destination file first.

This is significant because if the destination file is already in use, bad things could happen to whomever is using that file in case you cp a new file on top of it. e.g. overwriting an executable that is running might fail. Truncating a data file that an existing process is busy reading/writing to could cause pretty weird behavior. If you just remove the destination file first, as install does, things continue much like normal - the removed file isn't actually removed until all processes close that file.

3
  • 5
    Good answer. But see also cp --remove-destination in GNU coreutils. Jan 2, 2014 at 19:00
  • 1
    I'm a bit shocked that cp's default is to truncate instead of replace the destination file. Isn't that a dangerous default behavior in general? Mar 4, 2022 at 17:22
  • Awesome -- thanks, @PeterEisentraut, I wasn't aware of that flag
    – solr
    Apr 13, 2022 at 16:36
10

Technically, the difference between install -c and cp is that install sets the permissions of the target file to rwxr-xr-x. cp preserves the permissions of the source file minus the umask. These default behaviors are useful in different situations. Obviously, with all the options that both cp and install offer nowadays, the functionalities have converged.

Nowadays, install is commonly used in makefiles, cp everywhere else. This distinction is occasionally useful because some operating systems or installation systems allow you to hook into the install program to register the installed packages. Modern package management systems make this kind of obsolete, but some people still use it. Also, the possibility to set the target file permissions in the same go is very convenient.

4

The install utility, at its base, is a fancy cp. But as a tool specifically do installs it contains a few features that cp doesn't. My /usr/bin/install from GNU coreutils not only copies, but also can change perms/ownership as arg flags (saving chgrp, chown, chmod invocations) an option to strip debug info (saving a strip invocation) and also some mojo for SELinux contexts.

It just provides convenience actions useful for software installs. None are life changing, all are useful, and make your scripts cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.