32

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?

jpmc26
  • 254
tarabyte
  • 4,296

3 Answers3

27

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.

cakuzo
  • 386
  • 2
    If you're interested in why you should put it under /usr/local/ there is a pretty good answer here: http://unix.stackexchange.com/questions/8656/usr-bin-vs-usr-local-bin-on-linux – cakuzo Jul 27 '15 at 18:25
  • 1
    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. – OldTimer Aug 05 '15 at 23:06
20

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/

marcin
  • 713
  • 4
    Archived link here: https://web.archive.org/web/20090624104441/http://en.chys.info/2009/05/install-vs-cp-and-mmap/ – Fonzie Aug 09 '18 at 04:31
12

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.

fd0
  • 1,449