The *nix install command

Redowan Delowar July 28, 2024
Source

TIL about the install command on *nix systems. A quick GitHub search for the term brought up a ton of matches. I'm surprised I just found out about it now.

Often, in shell scripts I need to:

  • Create a directory hierarchy
  • Copy a config or binary file to the new directory
  • Set permissions on the file

It usually looks like this:

Turns out, the install command in GNU coreutils can do all that in one line:

You can check the file status with:

On my machine, this prints:

The -D flag directs install to create the destination directories if they don't exist, and the -m flag sets file permissions. The result is the same as the three lines of commands before.

It's common for Makefiles in C/C++ projects to install binaries like this:

It copies app_bin to /usr/local/bin, creates the parent directory hierarchy if necessary, and sets permissions on the binary so only the current user has read, write, and execute permissions, while others have read-only access.

You can also set directory permissions:

This creates the directory hierarchy first and then sets the permission. Here's how they look:

Output:

Then you can copy a file to the destination and set file permissions with another install command if needed.

You can also set user or group ownership while copying a file:

This command copies seed.db to the destination, creates the directory if needed, and gives access to the root user and group with the -o and -g flags, respectively.

There are a few other options you can read about in the man pages, but I haven't needed anything beyond the above.

Discussion in the ATmosphere

Loading comments...