How is install different from cp?

9,190

Solution 1

To "install" a binary compiled from source the best-practice would be to put it under the directory:

/usr/local/bin

On some systems that path is already in your PATH variable, if not you can add it by adapting the PATH variable in one of your profile configuration files ~/.bashrc ~/.profile

PATH=${PATH}:/usr/local/bin

dd is a low level copy tool that is mostly used to copy exactly sized blocks of the source which could be for example a file or a device.

cp is the common command to copy files and directories also recursively with the option -r and by preserving the permissions with the option -p.

install is mostly similar to cp but provides additionally the option to set the destination file properties directly without having to use chmod separately.

cp your files to /usr/local/bin and adapt the PATH variable if needed. That's what I would do.

Solution 2

Install copies files with the default mode 755.

Install can be configured to set the owner or group of a file and/or the mode of a file.

Install can be configured to backup the original file before it is replaced.

Solution 3

The primary difference between install and cp is that if the destination file already exists install unlinks it first.

This difference is not pointed out in the manual pages. The things listed in the other answers also matter -- both programs have different options, and also GNU install has different options than BSD install so portable Makefiles are limited to a common subset.

Why unlinking (which can be also done by rm before cp) matters? If you have a file with two hardlinks and modify it using one of the hardlinks, it's modified also in the other place on the filesystem. But if you remove one of the hardlinks first and replace it with modified file, the other place keeps the original version.

More probable scenario is that you update a program or a library while it is in use. If the binary is unlinked first, it won't affect the running program. Here is a nice post with more details: http://en.chys.info/2009/05/install-vs-cp-and-mmap/

Share:
9,190

Related videos on Youtube

tarabyte
Author by

tarabyte

Updated on September 18, 2022

Comments

  • tarabyte
    tarabyte almost 2 years

    How is install different from a simple copy, cp or dd? I just compiled a little utility and want to add it to /usr/sbin so it becomes available via my PATH variable. Why use one vs the other?

  • cakuzo
    cakuzo almost 9 years
    If you're interested in why you should put it under /usr/local/ there is a pretty good answer here: unix.stackexchange.com/questions/8656/…
  • Nate Eldredge
    Nate Eldredge almost 9 years
    Assuming you're using the GNU versions, cp supports backups as well.
  • SiBrit
    SiBrit almost 9 years
    In fact if you look at man 1 install you will find that there are a good many options that can be used when installing a file. For example, you can check if the file already exists, if so you can check if they are duplicate files and abort the install if the file is already installed. Configure mode, ownership and many other features all with a single command.
  • Fonzie
    Fonzie almost 6 years
  • solr
    solr about 2 years
    Upvoted -- best answer to this thread hands down