等待—不是要安装软件吗？ (Wait—It’s Not For Installing Software?)
The install command might have the most misleading name of any of the Linux commands. It doesn’t actually install any software. If you’re trying to install a software package from the command line in Ubuntu or another Debian-based distribution use the apt-get command. On other Linux distributions, use your Linux distribution’s package management tool instead—for example, dnf on Fedora or zypper on openSUSE.
The install command probably won’t be used every day. It’s useful, but only for certain situations. One scenario where install comes into its own is software development. Let’s say you’re programming a new utility. You’ll need to do testing outside of the development environment. To do that you need to copy the new program files to a test directory. The test directory might need to be created, and you need to set the correct permissions and ownership for the files.
Because development is an iterative activity, you can end up doing this sequence of actions many, many times. The install command does all the heavy lifting for you. Finally, when your new utility is ready to be deployed, you can use install to copy it with the correct permissions to its final working location.
A programmer is working on just such a new utility, called ana. It consists of an executable binary file and a database. After testing, it must be copied to /usr/local/bin to make it available for all users of the Linux system. You’ll need to substitute the filenames and directory paths in our example for the files and paths you’re using on your computer when you use install.
Until it is ready for release it will be tested in a directory called ~/test/ana. Members of the geek group will have read and execute permissions. Other users will have read and execute permissions also. The install command uses the same numeric representation for permissions as chmod does. Our programmer has decided that the permissions must be set to:
Our fictional programmer’s working directory is ~/work. He has written the program, compiled it, and produced a binary called ana. He already created the database file that ana works with, Words.db. So both files are ready for testing. Let’s take a look at them:
The ana utility he has just written creates anagrams out of a phrase provided on the command line. Verification testing is quite straightforward.
Our programmer has invoked ana with the phrase “biscuit” and all seems well. He now wants to copy these two files to the ~/test/ana directory to see if the new utility functions correctly away from the development environment. He issues the following command:
Listing the files in ~/test/ana confirms they have been copied over correctly.
The next step is to test the ana utility by invoking it in the ~/test/ana directory.
The utility operates as expected, which is great. However, the permissions are not correct. The requirement is to set members of the group geek to have read and execute permissions, and for other users to have execute only.
We can address both of those issues quite simply with the following command. Note the use of sudo to run the command with root permissions. The -o and -g and options require this. We’ll be asked for our password when we issue the command.
We set the owner of the file to be dave using the -o (owner) option.
我们使用-o (所有者)选项将文件的所有者设置为dave 。
The -g (group) option requires the name of a group. This becomes the owner group of the files. The group we are going to use is called geek.
-g (组)选项需要一个组的名称。 这将成为文件的所有者组。 我们将要使用的组称为geek 。
The -m (mode) option sets the file modes for the files, using the standard chmod numerical syntax.
We no longer need to use the -D (create directories) option, because we know the test directory already exists. We’ve also omitted the -v (verbose) option. Listing the files in our ~/test/ana directory shows us the file details:
This confirms that all our requirements have been met.
The files have been copied across to the testing directory. 文件已复制到测试目录中。
The permissions have been set correctly. 权限已正确设置。
dave is the owner of the files.
The geek group is the owner group of the two files.
Backup copies have been made of each file, called ana.bak and Words.db.bak. 已经为每个文件ana.bak和Words.db.bak制作了备份副本。
All that was achieved through the use of one command. Neat.
Our programmer makes some final changes to the utility and re-compiles. The files that have changed need to be copied over to the ~/test/ana directory from the ~/work directory. We can do this by using the -C (compare) option. If the source file and target file are the same, the source file is not copied.
sudo install -C -b -S .bak -o dave -g geek -m 751 ana Words.db -t ~/test/ana
Listing the files in the target directory shows us that the file size of the ana file has changed. It is bigger than the ana.bakfile. The timestamp on ana has also changed. These changes are because the new version of the file has been copied here.
The file size and timestamp of the Words.db file have not changed. No changes were made to the Words.db file, so it was not copied over. On a project with many files the -C (compare) option can save a lot of time and hard drive churn, by only copying those files that have been changed.
The programmer has again tested that the ana utility continues to operate.
It is time to use install to copy the files to the /usr/local/bin directory. This will make the new utility available for all users of this Linux computer. We know that /usr/local/bin exists, so we don’t need to create that directory. We can use a modified version of our last command.
We’ve changed the target directory to be /usr/local/bin. We’ve removed the -C (compare) option because there are no copies of these files in the target directory yet, so there is nothing to compare against. Likewise, there is nothing to back up, so we can remove the -b (backup) option and the -S (suffix) option.
We mentioned that install can strip out redundant symbol tables and other baggage from within the binary file, to reduce it in size. Let’s do that now. Note that the command below does not include Words.db. This is because Words.db is a database file, not a binary executable. To copy and shrink the binary file ana we can use the following command. We have added the -s (shrink) option with a lower case “s.” We’ve added back in the -b (backup) option and the -S (suffix) option, with an uppercase “S.”
The the install command caters to a pretty niche use. For many people it won’t be used day in and day out, or possibly from month to month. Despite that, the install command is a good tool to be familiar with and to have in your arsenal of tricks. For those occasions when you need it, it rewards your learning curve with boosts in efficiency, simplicity and simply fewer keystrokes.
Sometimes a simple cp -a command is a very painful and slow process. It's true that -v (verbose) option can give you some information on the details of the copy process, but not normally the progress of it. In fact, cp -a is a quite slow process that sometimes is faster (and safer) implemented by tar, for example:
$ tar cf - . | (cd /dst; tar xvf -)
Usually faster, and more verbose. Another commands such as pv can help you too, to monitor the progress of a copy between two directories, for example:
$ tar cf - . | pv | (cd /dst; tar xf -)
2,06GB 0:00:09 [ 194MB/s] [ <=> ]
But copying several gigabytes/terabytes of data and many files between quite old NFS disks is painful via cp. Let's see two alternatives for:
Monitoring the progress of the copy and the copied files.
Skipping to next file before an error (gcp)
Syncing directories (rsync)
Copying files via network (rsync)
One of the better commands for doing copies is rsync, that allows you to synchronize two directories, and in this sense src/ can have live data, that incrementally is synced to dst/ in several executions of the command
Jake Bugg - Jake Bugg Album 2012/
Jake Bugg - Jake Bugg Album 2012/01 - Lighting Bolt.mp3
1,913,897,967 15% 22.79MB/s 0:01:20 (xfr#277, ir-chk=1019/1825)
Jake Bugg - Jake Bugg Album 2012/05 - Simple As This.mp3
1,936,698,070 15% 22.80MB/s 0:01:21 (xfr#281, ir-chk=1015/1825)
You can also use it with -n option to perform a dry run (this is more used than the skype test call), that checks and lists the differences between the two given directories. You can use it too with "-e ssh" user@host:dst/ or without --info option in older versions of rsync. It is slower for copying but it does a lot of useful things such syncing, checkings md5sums.... You will remember rsync if something goes bad.
Another fantastic command for copy is gcp. Besides of progress estimation, gcp does not copy when the file exists, skips to the next file if occurs an error, and all the fails are written to a journal file.
In an Alfresco context, many simple migrations (or restoring processes) are tracked via CIFS or Webdav drives. In these cases the above commands are useful. Even they can be useful, if you are doing a local copy in an Alfresco instance, for performing a later Filesystem Bulk process in Alfresco. From a system administrator point of view, when restoring huge contentstores or Lucene / SOLR indices, or moving backups, these commands can save you so much time.
Another day we took some time in alternatives for scp copies between two machines.
Some useful links for reading and just patience for copying: