Linux install Command: Copy Files, Set Modes, Create Paths

Why Use install Instead of cp

Unlike standard file-copying utilities, the Linux install command handles copying files, assigning permission bits, setting ownership, and building out parent directory trees in a single atomic invocation. Throughout this Ubuntu hands-on guide, we contrast install with cp and walk through production-ready flags—including -m, -d, -D, -b, and -C—commonly found in build pipelines and deployment scripts.

Core Behavior and How It Differs from cp

To give ourselves an isolated sandbox for testing, start by clearing any leftover lab folders and provisioning fresh source and destination directories with rm -rf /tmp/install_lab && mkdir -p /tmp/install_lab/src /tmp/install_lab/bin.

Provisioning a clean sandbox directory tree under /tmp/install_lab

Next, generate a minimal shell script inside the source directory using echo and standard output redirection so we have an artifact to deploy.

Creating a sample deploy.sh script inside the source folder

Before introducing install, copy /tmp/install_lab/src/deploy.sh over to /tmp/install_lab/bin/deploy_cp.sh with the traditional cp utility to establish a baseline.
Notice that cp operates silently by default—unless you explicitly pass a verbose flag, nothing appears on stdout.

Copying deploy.sh silently using the standard cp command

Where cp merely duplicates bytes, install automatically grants executable permissions (755) to copied files by default and optionally configures ownership or creates missing directories on the fly.
Adding -v produces verbose feedback in the terminal, such as '/tmp/install_lab/src/deploy.sh' -> '/tmp/install_lab/bin/deploy_inst.sh'.
⚙️ [Essential Flags at a Glance]

-v : Prints each file transfer or directory creation step to standard output.
-m : Overrides the default permissions with an explicit mode such as 644 or 755.
-d : Treats all arguments as directories and constructs any missing parent folders along the path.
-D : Creates missing parent directories leading up to the target file before copying it into place.
-o : Assigns file or directory ownership to a specific user.
-g : Assigns the target’s group ownership.
-p : Preserves the original access and modification timestamps on the installed copy.

Deploying deploy.sh with install -v to display copy progress

Run ls -l across both directories to inspect the resulting permission bits side by side.
Both the original deploy.sh and the cp-created deploy_cp.sh retain standard non-executable permissions (-rw-r--r--, or 644).
By contrast, deploy_inst.sh—placed there by install—arrives ready to run with -rwxr-xr-x (755) permissions applied automatically.

Comparing file permissions between cp and install outputs via ls -l

Setting Permissions and Ownership in One Call (-m, -o, -g)

Executable 755 permissions are rarely appropriate for configuration files holding secrets. Use echo to write a sample credential file at /tmp/install_lab/src/app.conf for testing tighter access controls.

Creating a mock configuration file containing a demo API key

Passing -m 600 overrides the 755 default, ensuring /tmp/install_lab/bin/app.conf is readable and writable exclusively by its owner.
Pairing it with -v confirms the operation in stdout as '/tmp/install_lab/src/app.conf' -> '/tmp/install_lab/bin/app.conf'.

Installing app.conf with strict 600 permissions

You can also lock down both permissions and ownership simultaneously by combining -m 750 with -o and -g.
Here, $(id -un) and $(id -gn) dynamically supply the active account’s username and primary group, placing /tmp/install_lab/bin/deploy_restricted.sh with mode 750.

Setting mode 750 alongside owner and group in a single install command

Verify the metadata using stat formatted with -c "%n | mode=%a | owner=%U:%G" to inspect paths, octal modes, and user/group pairs.
As the output confirms, app.conf carries mode=600, deploy_restricted.sh carries mode=750, and both belong to the executing root:root account.

Verifying octal modes and ownership with stat

Building Directory Trees and Nested Targets (-d, -D)

When preparing an application layout, combining -d with -m 755 lets you create /tmp/install_lab/opt/myapp/logs and /tmp/install_lab/opt/myapp/config in one shot.
Because -d works recursively like mkdir -p, the -v output traces every intermediate directory created along the way—first opt, then opt/myapp, and finally logs and config.

Creating nested logs and config directories with install -d

To see how -D handles nested file destinations, create a small configuration file at /tmp/install_lab/src/server.cfg using echo.

Creating server.cfg to test nested path installation

Unlike -d (which creates directories alone), -D automatically provisions any missing parent folders—such as /tmp/install_lab/opt/myapp/nested/env—immediately before copying server.cfg into place.
The verbose trace shows nested and nested/env being created first, followed by the file transfer locked to mode 644 via -m 644.

Installing server.cfg into a non-existent nested path with -D

Inspect the resulting directories and installed config file with ls -ld and ls -l.
Both config and logs reflect drwxr-xr-x (755), while server.cfg displays -rw-r--r-- (644), matching our exact specifications.

Checking permissions on newly created directories and server.cfg

Automating Production Deployments (-p, -b, -C, -t)

For release tracking or cache validation, you often need the installed copy to keep its original build timestamp. Create /tmp/install_lab/src/release.txt with echo and backdate its modification time to 2025-01-01 00:00:00 via touch -d.

Creating release.txt and setting a custom timestamp with touch -d

Adding -p instructs install to preserve the source file’s original access and modification timestamps rather than stamping the current system time.
Combining -p with -m 644 and -v writes the file to /tmp/install_lab/opt/myapp/release.txt and echoes the copy operation to the console.

Copying release.txt while preserving its timestamps using -p

Now overwrite /tmp/install_lab/src/release.txt with version=2.0 using echo so we can demonstrate safe backups and idempotent checks.

Updating release.txt content to version=2.0

When overwriting an existing target, -b safeguards the previous version by renaming it before installing the new file, and --suffix=.bak customizes the backup extension.
In the terminal output, install reports creating (backup: '/tmp/install_lab/opt/myapp/release.txt.bak') right alongside the fresh copy.

Backing up an existing file during installation with -b and –suffix=.bak

In idempotent scripts, -C compares source and destination contents as well as permissions, skipping the write altogether if nothing has changed.
Because the previous command already deployed identical file contents with mode 644, re-running with -C performs no copy and prints nothing at all—even with -v enabled.

Skipping redundant copies with the idempotent -C flag

Finally, generate two helper scripts—/tmp/install_lab/src/a.sh and /tmp/install_lab/src/b.sh—with echo to test batch installation into a shared target directory.

Creating two helper scripts a.sh and b.sh for batch deployment

Specifying the destination directory upfront after -t lets you pass multiple source files at the end of the command, making it ideal for shell expansions or globbing.
Here, a.sh and b.sh are deployed sequentially into /tmp/install_lab/bin with 755 permissions.

Batch-installing multiple files into /tmp/install_lab/bin using -t

Run ls -la across /tmp/install_lab/opt/myapp/ and /tmp/install_lab/bin/ to review the final state of our lab environment.
The newly added a.sh and b.sh files sit inside /tmp/install_lab/bin/ with -rwxr-xr-x (755) permissions, while every file deployed in earlier steps—including the .bak backup and timestamped release.txt—remains intact with its intended mode.

Reviewing the final file layout and permissions across all lab directories

Rather than chaining cp, chmod, and mkdir across multiple lines in Makefiles or CI/CD deployment scripts, install consolidates file placement, permissions, ownership, backups, and directory creation into one clean, reliable command.