56

I've seen the install command used in a lot of Makefiles, and its existence and usage are kind of confusing. From the manpages, it seems like a knockoff of cp with less features, but I assume it wouldn't be used unless it had some advantage over cp. What's the deal?

azernik
  • 681

4 Answers4

57

install not only copies files but also changes its ownership and permissions and optionally removes debugging symbols from executables. It combines cp with chown, chmod and strip. It's a convenient higher-level tool to that accomplishes a common sequence of elementary tasks.

An advantage of install over cp for installing executables is that if the target already exists, it removes the target file and creates a new one. This gets rid of any current properties such as access control lists and capabilities, which can be seen both as an upside and as a downside. When updating executables, if there are running instances of this executable, they keep running unaffected. In contrast, cp updates the file in place if there is one. On most Unix variants, this fails with the error EBUSY¹ if the target is a running executable; on some it can cause the target to crash because it loads code sections dynamically and modifying the file causes nonsensical code to be loaded.

install is a BSD command (added in 4.2BSD, i.e. in the early 1980s). It has not been adopted by POSIX.

¹ “Text file busy”. In this context, “text file” means “binary executable file”, for obscure historical reasons.

10

It provides a standardized way of manipulating a file's or directory's ownership and permissions while copying the file or creating the directory, in a single command.

  • 9
    It's not atomic. Atomic means there are no intermediate states visible to other processes. install does a copy followed by chown and chmod, all separate syscalls, and doesn't use rename-into-place (at least as of coreutils 8.13 the GNU version doesn't). If you assume it all happens atomically, you might get a nasty surprise. –  Oct 14 '13 at 00:55
3

With install command we can Copy file with desire permissions

Example which mostly use while setting up ldap

install -o ldap -g ldap /etc/openldap/DB_CONFIG_EXAMPLE  /var/lib/ldap/DB_CONFIG

This save us doing chown ldap. /var/lib/ldap/DB_CONFIG, if you copied using cp then you also need to chown in this scenario

Rahul Patil
  • 24,711
-1

See the man page for install:

$ man install

excerpt

SYNOPSIS
       install [OPTION]... [-T] SOURCE DEST
       install [OPTION]... SOURCE... DIRECTORY
       install [OPTION]... -t DIRECTORY SOURCE...
       install [OPTION]... -d DIRECTORY...


DESCRIPTION
       This  install  program  copies  files  (often just compiled) into 
       destination locations you choose.  If you want to download and
       install a ready-to-use package on a GNU/Linux system, you should instead 
       be using a package manager like yum(1) or apt-get(1).

       In the first three forms, copy SOURCE to DEST or multiple SOURCE(s) to 
       the existing DIRECTORY, while  setting  permission  modes and 
       owner/group.  In the 4th form, create all components of the given 
       DIRECTORY(ies).

       Mandatory arguments to long options are mandatory for short options too.

Other useful things such as installing with specific ownership, permissions, and preserving the original files timestamps can also be achieved through the use of install.

   -g, --group=GROUP
          set group ownership, instead of process' current group

   -m, --mode=MODE
          set permission mode (as in chmod), instead of rwxr-xr-x

   -o, --owner=OWNER
          set ownership (super-user only)

   -p, --preserve-timestamps
          apply access/modification times of SOURCE files to corresponding 
          destination files

References

slm
  • 369,824
  • 5
    As I said in the question, I read the main page; it left me unenlightened as to what this adds to the functionality of cp. – azernik Oct 12 '13 at 22:01
  • slm highlighted the specific functionality that sets the two apart: you can set specific owner, group, and mode for the destination file with install, rather than just preserving existing permissions as with cp – Joshua Miller Oct 13 '13 at 03:33