How Linux distinguishes between install command and cp command

  
                

In many Linux commands, the install command and the cp command can copy files, but there are some differences in usage. Many people don't know how to choose when they use them. The following small series teaches you how to distinguish between the install command and the cp command.

The main difference between them is as follows:

1, the most important point, if the target file exists, cp will be cleared after the first file inside to write the new file, and install The original file will be deleted and then written to the new file. This is because writing to the file being used may cause some problems. For example, writing a file that is being executed may fail. For example, writing a new file to a file handle that has been continuously written may result in an error. file. And using install to delete and then write (will generate a new file handle) to install to avoid these problems;

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

3, the install command can print more suitable debug information, and will automatically handle the SElinux context.

------------------------------------ Split line ------ ------------------------------

At the time of compiling LFS 6, I still couldn’t understand install. What is the difference between a command and cp and chmod, chgrp?

After working, I realized that a running process can't just do cp, and often prompts "text busy". The advice given by the seniors of the operation and maintenance department is to use mv instead of cp. Today, it seems that the predecessors don't seem to Know the install command.

Now let's briefly introduce the install command.

install copy the file list and also set the file's properties (including owner, group), usually used in Makefiles To copy the program to the specified directory.

Common usages have the following three forms:

1: install -d [option] DIRECTORY [DIRECTORY..] Support multiple. Similar to mkdir -p supports recursion.

For example: install -da/b/ce/f The result is the same as mkdir -pa/b/ce/f.

2: install [option] SOURCE DEST< Br>

Copy the SOURCE file (test cannot be a directory) to the DEST file.

install a/ec The result is similar to cp a/ec # Note c must be a file.

Useful options -D

inst All -D xa/b/c # The effect is similar to mkdir -pa/b && cp xa/b/c

3: install [option] SOURCE [SOURCE..] DIRECTORY

Copy multiple SOURCE files to the destination directory.

install a/* d where d is the directory.

Useful options

-b : Automatic backup.

-m : Set permissions for the installation file

-p : Keep the timestamps of the file. That is, the timestaamps of the file are the same as the source file. When we want to use the mtime of the installation file to trace the build of the file. Time instead of installation time.

-s : Strip the symbol tables from installed binary executables.

-S : The suffix of the backup file.

install -S .bak new old #old The file is automatically mv is old.bak.

-v: verbose , prints the details of the install file.

`-c‘

Ignored; for compatibility with old Unix versions of `install’. # is compatible with the old version of Unix.

-C: (uppercase)

Install the file, but if the target file is the same as the source file (the judgment method needs to look at the code confirmation), skip it. The advantage of this is to be able to maintain the same file mtime.

The above is the introduction of the Linux install command and the cp command. The biggest difference is that when the copy target file exists, install can be deleted and used. In case the copy fails.

Copyright © Windows knowledge All Rights Reserved