Linux install Command

Linux 命令大全Linux Command Encyclopedia


installis a multi-functional file installation tool in the Linux system, combining file copying, permission setting, directory creation and other functions into one. Compared with the simplecpcommand,installit provides more fine-grained control, especially suitable for program installation and file deployment in scripts.


install Command Basic Syntax

install [选项]... 源文件 目标文件
install [选项]... 源文件... 目标目录
install -d [选项]... 目录...

Description of Common Options and Parameters

Option Description
-bor--backup Back up existing files before overwriting
-Cor--compare Compare source and target files, copy only if they differ
-D Create all parent directories of the target directory
-dor--directory Create directories instead of copying files
-gor--group=GROUP Set the file's group
-mor--mode=MODE Set the file permission mode (e.g., 755)
-oor--owner=OWNER Set the file owner
-por--preserve-timestamps Preserve the source file's access/modification time
-Sor--suffix=SUFFIX Override the default backup suffix (default is ~)
-tor--target-directory=DIRECTORY Copy all source files to the specified directory
-vor--verbose Display detailed operation information

Main Functions and Usage Examples

1. Basic File Copying

Example

# Copy file1 to file2
install file1 file2

# Copy multiple files to the target directory
install file1 file2 file3 /target/directory/

2. Creating Directories

Example

# Create a single directory
install -d /path/to/newdir

# Create a multi-level directory
install -d /path/to/newdir/subdir

3. Setting File Permissions and Attributes

Example

# Copy a file and set permissions to 755
install -m 755 script.sh /usr/local/bin/

# Copy a file and set the owner to root
install -o root -g root config.conf /etc/

4. Backup and Comparison Features

Example

# Back up the target file before overwriting
install -b original.txt backup/

# Copy only when the source file differs
install -C updated.conf /etc/

5. Batch Installing Files

Example

# Install all .sh files to /usr/local/bin
install -m 755 *.sh -t /usr/local/bin/

Comparison Between install and cp Commands

Feature install cp
Primary Use Program installation and deployment General file copying
Permission control Can set permissions during copy Preserves original permissions or uses umask
Directory creation Automatically creates target directory Requires mkdir
Owner setting Supports setting owner and group Not supported
Backup feature Built-in backup option Requires additional commands
Comparison feature Supports copy after comparison Not supported

Practical Application Scenarios

1. Installing Custom Scripts

Example

#!/bin/bash
# Example script installation
install -D -m 755 myscript.sh /usr/local/bin/myscript
install -D -m 644 myscript.man /usr/share/man/man1/myscript.1

2. Deploying Configuration Files

Example

# Safely deploy configuration files, keeping the original configuration as backup
install -b -m 600 -o root -g root new_config.conf /etc/service/config.conf

3. Applications in Build Systems

Example

# Using install in a Makefile
install: myprogram
    install -d $(DESTDIR)/usr/bin
    install -m 755 myprogram $(DESTDIR)/usr/bin
    install -d $(DESTDIR)/usr/share/myprogram
    install -m 644 data/* $(DESTDIR)/usr/share/myprogram

Notes

  1. Using-Dthe option, only the last path component is created
  2. Setting the owner (-o) and group (-g) usually requires root privileges
  3. By default,installoverwrites the target file without warning
  4. For symbolic links,installthe file pointed to by the link is copied rather than the link itself

Frequently Asked Questions

Q: Which is better, the install or cp command?

A: It depends on the usage scenario.installis more suitable for program installation and system deployment,cpis more suitable for daily file copying.

Q: How do I preserve the timestamp of the original file?

A: Use-por--preserve-timestampsoption.

Q: Why are the permissions of files copied by install different from those copied by cp?

A: installBy default, it uses stricter permissions (755 or 644), whilecppreserves original permissions or uses umask.


Summary

installThe command is a powerful tool for Linux system administration and software installation, especially suitable for use in scripts and build systems. By mastering its rich options, precise file deployment and permission control can be achieved. Compared with the simplecpcommand, it is more suitable for professional installation scenarios.


Linux 命令大全Linux Command Encyclopedia

Other Extensions