Overview
The install command is used to copy files while simultaneously setting their access permissions (mode), owner, and group.
While the name suggests it is only for software installation, it is effectively a high-functioning copy command. It eliminates the need to run chmod or chown after a cp command, making it a standard tool in shell scripts, Makefiles, and server provisioning automation.
Specifications (Arguments and Options)
Syntax
Deploying files (Basic):
install [options] [source] [destination]
Creating directories:
install -d [options] [directory_name...]
Main Options
Options for permission settings and directory creation are the most critical.
| Option | Description |
-m [mode] | Specifies the file permissions numerically (e.g., 755, 644). Default is often 755. |
-o [owner] | Specifies the file owner (may require root privileges). |
-g [group] | Specifies the owner group of the file. |
-d | Creates the specified directory (equivalent to mkdir -p). |
-D | Automatically creates missing parent directories of the destination before copying. |
-t [dir] | Specifies the target directory for deploying multiple files at once. |
-b | Creates a backup of the destination if a file with the same name exists (appends ~). |
-p | Preserves the access/modification times of the source file. |
-v | Verbose mode; explains what is being done. |
Basic Usage
Deploying a Script to a Path-accessible Location
The most common use case is moving a script to /usr/local/bin while ensuring it has execution permissions (755).
# Deploy myscript.sh to /usr/local/bin/myscript and set permissions to 755
sudo install -m 755 myscript.sh /usr/local/bin/myscript
Execution Result Example:
(No output is shown, but checking with ls reveals:)
ls -l /usr/local/bin/myscript
-rwxr-xr-x 1 root root 1500 Jan 16 10:00 /usr/local/bin/myscript
Unlike cp, even if the source file is rw-r--r--, the destination will strictly follow the specified -m 755.
Practical Commands
1. Securely Deploying Private Keys
Sensitive files like SSH private keys must be deployed with -m 600 (readable/writable only by the owner). install handles this securely in one step.
# Deploy a private key to the .ssh directory and force 600 permissions
install -m 600 id_ed25519 ~/.ssh/id_ed25519
2. Creating Missing Parent Directories (-D)
While the cp command fails if the destination path does not exist, install -D automatically creates any missing parent directories.
# Automatically create /opt/app/conf if missing and deploy config.yml
sudo install -D -m 644 config.yml /opt/app/conf/config.yml
3. Updating Configuration Files with Backups
When overwriting existing configuration files, it is safer to keep the old file as a backup.
# Overwrite nginx.conf while preserving the old version as nginx.conf~
sudo install -b -m 644 nginx.conf /etc/nginx/nginx.conf
4. Deploying Multiple Files to a Directory
Use the -t option to install multiple scripts or libraries into a directory simultaneously.
# Deploy all .sh files in the current directory to /opt/scripts/
sudo install -m 755 -t /opt/scripts/ *.sh
Customization Points
- Permission Settings (-m):
- Executable Scripts:
755(rwxr-xr-x) - Standard Config Files:
644(rw-r–r–) - Sensitive Data (Keys/Passwords):
600(rw——-)
- Executable Scripts:
- Changing Ownership (-o, -g): If a web server configuration needs to be owned by the
www-datauser, specify-o www-data -g www-data.
Important Notes
- No Recursive Copying: The
installcommand does not have a recursive directory copy function likecp -r. Usecporrsyncfor tree-based duplication. - Default Permissions: If
-mis omitted, many environments apply755by default. If you are deploying plain text files, you should explicitly specify-m 644to avoid security risks. - Overwrite Behavior:
installperforms a forced overwrite (equivalent tocp -f) by default. Use-b(backup) if you want to avoid losing existing data without a trace.
Advanced Application
Creating Directories Only
Similar to mkdir -p, you can create deep directory hierarchies while strictly defining their permissions.
# Create a temporary directory accessible only by the owner
install -d -m 700 /tmp/my_secure_workspace
Conclusion
The install command is a professional tool that simplifies and secures server construction and environment setup.
Constraint: Not suitable for copying directory structures recursively.If you are currently using a two-step “copy then chmod” process, it is highly recommended to replace it with a single install command.
Best Use Cases: Deploying executables, deploying configuration files, and use within Makefiles or setup scripts.
Key Adjustments: Choose the appropriate mode (644, 755, 600) based on the file type.
