Linux install Command
install is a multifunction file installation tool in Linux that combines file copying, permission setting, and directory creation into one. Compared with the simple cp command,install it provides finer control, making it especially suitable for program installation and file deployment in scripts.
Basic syntax of the install command
install [选项]... 源文件 目标文件
install [选项]... 源文件... 目标目录
install -d [选项]... 目录...
Description of common options and parameters
| Option | Description |
|---|---|
-b or --backup |
Back up existing files before overwriting |
-C or --compare |
Compare source and destination files and copy only when they differ |
-D |
Create all parent directories of the destination directory |
-d or --directory |
Create directories instead of copying files |
-g or --group=GROUP |
Set the group ownership of a file |
-m or --mode=MODE |
Set the file permission mode (such as 755) |
-o or --owner=OWNER |
Set the file owner |
-p or --preserve-timestamps |
Preserve the access/modification times of the source file |
-S or --suffix=SUFFIX |
Override the default backup suffix (default is ~) |
-t or --target-directory=DIRECTORY |
Copy all source files to the specified directory |
-v or --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. Create directories
Example
Create a single directory
install
-d
/
path
/
to
/
newdir
Create multi-level directories
install
-d
/
path
/
to
/
newdir
/
subdir
3. Set file permissions and attributes
Example
Copy a file and set its permissions to 755
install
-m
755
script.sh
/
usr
/
local
/
bin
/
Copy a file and set its 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 files differ
install
-C
updated.conf
/
etc
/
5. Batch-install files
Example
Install all .sh files to /usr/local/bin
install
-m
755
.sh
-t
/
usr
/
local
/
bin
/
Comparison of the install and cp commands
| Feature | install | cp |
|---|---|---|
| Main Use | Program installation and deployment | General-purpose file copying |
| Permission Control | Set permissions while copying | Preserve the original permissions or use umask |
| Directory creation | Automatically create the target directory | Must be used together with mkdir |
| Owner settings | Supports setting the owner and group | Not supported |
| Backup feature | Built-in backup option | Requires additional commands |
| Comparison feature | Supports copying only after comparison | Not supported |
Practical Application Scenarios
1. Install a custom script
Example
#!/bin/bash
Example of installing a script
install
-D
-m
755
myscript.sh
/
usr
/
local
/
bin
/
myscript
install
-D
-m
644
myscript.man
/
usr
/
share
/
man
/
man1
/
myscript.1
2. Deploy a configuration file
Example
Safely deploy a configuration file, keeping the original as a backup
install
-b
-m
600
-o
root
-g
root new_config.conf
/
etc
/
service
/
config.conf
3. Use 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
- Use
-DWith this option, only the last path component is created - Set owner (
-o) and group (-g) usually requires root privileges - By default,
installIt overwrites the target file without any warning - For symbolic links,
installit copies the file the link points to rather than the link itself
Frequently Asked Questions
Q: Which command is better, install or cp?
A: It depends on the use case.install install is more suitable for program installation and system deployment,cp while cp is more suitable for everyday file copying.
Q: How can I preserve the original file's timestamp?
A: Use -p or --preserve-timestamps option.
Q: Why are the permissions of files copied by install different from those of cp?
A: install install uses stricter permissions by default (755 or 644), while cp cp preserves the original permissions or uses umask.
Summary
install The command is a powerful tool for Linux system administration and software installation, especially suited to use in scripts and build systems. By mastering its rich options, you can achieve precise file deployment and permission control, making it more suitable than the simple cp command for professional installation scenarios.
Linux Command Reference