The use and difference between the install and cp commands in linux

  • 2020-05-17 07:29:33
  • OfStack

preface

install and cp are similar in that you can copy files/directories to a specified location. However, install allows you to control the properties of the target file. install is commonly used in the makefile of a program (and also in spec of RPM) to copy the program to the target (installation) directory.

The main usage of install is as follows:

install [OPTION]… SOURCE… DIRECTORY

At this point, DIRECTORY must exist or be treated as a new file

install [OPTION]… -t DIRECTORY SOURCE…

install [OPTION]… -d DIRECTORY…

If the directory does not exist, create it

-b: backup every existing destination file;

-D: create all directories before the destination, then copy the source to the destination

-g: set the group to which you belong;

-m: set your own permissions instead of the default rwxr-xr-x

-o: set the owner yourself

-p: takes the modification time of the source file as the file attribute of the corresponding destination

Such as:


@install -d /usr/bin
@install -p -D -m 0755 targets /usr/bin
 The equivalent of 
@mkdir -p /usr/bin
@cp targets /usr/bin
@chmod 755 /usr/bin/targets
@touch /usr/bin/tagets <----  Update the file timestamp 
<----@ The prefix means that the output is not in the console. 

install and cp accomplish the same task by copying files. The differences between them are as follows:

1. The most important point is that if the target file exists, cp will first empty the file and then write a new file into it, while install will first delete the original file and then write to the new file. This is because writing to a file in use can cause problems, such as failing to write to an executing file, or writing a new file to a file handle that is already being written to. This can be avoided by using install to delete and then write (which generates a new file handle).

2. The install command handles file permissions properly. For example, install-c will set the permissions of the target file to rwxr-xr-x;

3. The install command prints out more and more appropriate debug information and handles SElinux context issues automatically.

conclusion

The above is the article all day oh function, hope the content of this article to everyone's study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: