在linux里复制文件命令

Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

install is a versatile file-copying command in Linux and macOS. It’s perfect for the power-user looking for efficiency. Read this article to discover how to work smarter—not harder.

install是Linux和macOS中的通用文件复制命令。 对于寻求效率的高级用户而言,它是完美的选择。 阅读本文以发现如何更聪明地工作,而不是更努力。

等待—不是要安装软件吗? (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.

在任何Linux命令中, install命令的名称可能最具误导性。 它实际上并没有安装任何软件。 如果您试图从Ubuntu或其他基于Debian的发行版中的命令行安装软件包,请使用apt-get命令。 在其他Linux发行版上,请改用Linux发行版的程序包管理工具,例如Fedora上的dnf或openSUSE上的zypper

那么安装能做什么? (So What Does install Do?)

In a nutshell  install combines elements from the cp (copy), chown (change owner), chmod (change mode), mkdir (make directory), and strip (strip symbols) commands. It lets you use functions from all of those in one single action.

简而言之, install结合了cp ( 复制 ), chown ( 更改所有者 ), chmod ( 更改模式 ), mkdir ( make目录 )和strip ( 剥离符号 )命令中的元素。 它使您可以在一次操作中使用所有功能。

The install command can:

install命令可以:

  • Copy files like the cp command.

    复制文件,如cp命令。

  • Choose whether to overwrite existing files.选择是否覆盖现有文件。
  • Create the target directory if it does not exist, like mkdir.

    如果目标目录不存在,请创建它,例如mkdir

  • Set the user permission flags of the files, just like the chmod command.

    就像chmod命令一样,设置文件的用户权限标志。

  • Set the owner of the files, just like the chown command.

    设置文件的所有者,就像chown命令一样。

  • Remove non-essential baggage from executable files, just like the strip command.

    就像strip命令一样,从可执行文件中删除不必要的行李。

Despite all that functionality, the install command doesn’t have too many options to contend with.

尽管具有所有这些功能,但是install命令并没有太多可供选择的选项 。

什么时候使用 (When Would You Use It)

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.

install命令可能不会每天使用。 这很有用,但仅适用于某些情况。 install独有的一种情况是软件开发。 假设您正在编写一个新实用程序。 您需要在开发环境之外进行测试。 为此,您需要将新程序文件复制到测试目录。 可能需要创建测试目录,并且您需要为文件设置正确的权限和所有权。

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.

由于开发是一项迭代活动,因此您可以多次完成此一系列操作。 install命令为您完成了所有繁重的工作。 最后,当准备好要部署新实用程序时,可以使用install以正确的权限将其复制到其最终工作位置。

一个例子 (An Example)

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.

程序员正在开发一个名为ana的新实用程序。 它由一个可执行的二进制文件和一个数据库组成。 测试之后,必须将其复制到/usr/local/bin以使其对Linux系统的所有用户可用。 使用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:

在准备发布之前,它将在名为~/test/ana的目录中进行测试。 geek组的成员将具有读取和执行权限。 其他用户也将具有读取和执行权限。 install命令使用与chmod相同的数字表示权限。 我们的程序员决定必须将权限设置为:

  • Owner: Read, write, and execute.所有者:读取,写入和执行。
  • Group: Read and execute.组:读取并执行。
  • Others: Execute only.其他:仅执行。

如何使用install命令 (How to Use the install Command)

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:

我们虚构的程序员的工作目录是~/work 。 他编写了程序,对其进行了编译,并生成了一个名为ana的二进制文件。 他已经创建了ana可以使用的数据库文件Words.db 。 因此,两个文件都可以进行测试了。 让我们看看它们:

ls -l ana Words.db

The ana utility he has just written creates anagrams out of a phrase provided on the command line. Verification testing is quite straightforward.

他刚刚编写的ana实用程序使用命令行上提供的短语创建了anagram。 验证测试非常简单。

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:

我们的程序员用“饼干”一词调用了ana ,一切似乎都很好。 现在,他想将这两个文件复制到~/test/ana目录中,以查看新实用程序是否在开发环境之外正常工作。 他发出以下命令:

install -D -v ana Words.db -t ~/test/ana

The options used on the command line were:

命令行上使用的选项是:

  • D: Create directories, including parent directories, if required.

    D :创建目录,包括父目录(如果需要)。

  • v: Verbose, list each directory as it is made and each file copy as it is performed.

    v :详细列出每个目录,并列出每个文件的副本。

  • t: Target directory.

    t :目标目录。

We can see that install creates the ~/test directory, and then creates the ~/test/ana directory. The files are listed one by one as they are copied to the target directory.

我们可以看到install创建了~/test目录,然后创建了~/test/ana目录。 这些文件在复制到目标目录时会一一列出。

Listing the files in ~/test/ana confirms they have been copied over correctly.

~/test/ana列出文件,确认它们已被正确复制。

ls -l

The next step is to test the ana utility by invoking it in the ~/test/ana directory.

下一步是通过在~/test/ana目录中调用ana实用程序来对其进行~/test/ana

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.

该实用程序按预期运行,这很棒。 但是,权限不正确。 要求是将geek组的成员设置为具有读取和执行权限,而其他用户则仅具有执行权限。

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.

我们可以使用以下命令非常简单地解决这两个问题。 注意使用sudo以root权限运行命令。 -o-g和选项需要此选项。 发出命令时,系统会要求我们输入密码。

sudo install -b -S .bak -o dave -g geek -m 751 ana Words.db -t ~/test/ana
  • The -b (backup) option creates backups of the files before they are overwritten.

    -b (备份)选项在覆盖文件之前创建文件备份。

  • The -S (suffix) option defines the suffix for the backup files. If you do not provide a suffix a ~ (tilde) is used. We’re asking install to use a suffix of .bak.

    -S (后缀)选项定义备份文件的后缀。 如果不提供后缀,则使用~ (代字号)。 我们要求install使用.bak后缀。

  • 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.

    -m (模式)选项使用标准的chmod数字语法设置文件的文件模式。

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:

我们不再需要使用-D (创建目录)选项,因为我们知道测试目录已经存在。 我们还省略了-v (详细)选项。 在~/test/ana目录中列出文件会向我们显示文件详细信息:

ls -l

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.

    dave是文件的所有者。

  • The geek group is the owner group of the two files.

    geek组是两个文件的所有者组。

  • 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.

我们的程序员对实用程序进行了一些最终更改,然后重新编译。 需要将已更改的文件从~/work目录复制到~/test/ana目录。 我们可以通过使用-C (比较)选项来实现。 如果源文件和目标文件相同,则不会复制源文件。

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.

列出目标目录中的文件会向我们表明ana文件的文件大小已更改。 它大于ana.bak文件。 ana的时间戳也已更改。 这些更改是因为文件的新版本已在此处复制。

ls -l

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.

Words.db文件的文件大小和时间戳未更改。 没有对Words.db文件进行任何更改,因此未将其复制。 在具有许多文件的项目中, -C (比较)选项通过仅复制那些已更改的文件,可以节省大量时间和硬盘驱动器混乱。

The programmer has again tested that the ana utility continues to operate.

程序员再次测试了ana实用程序是否可以继续运行。

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.

现在该使用install将文件复制到/usr/local/bin目录了。 这将使新实用程序可用于此Linux计算机的所有用户。 我们知道/usr/local/bin存在,所以我们不需要创建该目录。 我们可以使用上一个命令的修改版本。

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.

我们将目标目录更改为/usr/local/bin 。 我们删除了-C (比较)选项,因为目标目录中还没有这些文件的副本,因此没有可比较的内容。 同样,没有要备份的内容,因此我们可以删除-b (备份)选项和-S (后缀)选项。

sudo install -o dave -g geek -m 751 ana Words.db -t /usr/local/bin

We can list that the files have arrived in /usr/local/bin:

我们可以列出文件已经到达/usr/local/bin

ls -l

And as a final test let’s change directory to our home directory and see if we can invoke our new utility from there.

作为最终测试,让我们将目录更改为主目录,看看是否可以从那里调用我们的新实用程序。

Note that we didn’t need to preface the ana command with ./  which means it is running from /usr/local/bin. Mission accomplished.

请注意,我们不需要在ana命令前加上./ ,这意味着它从/usr/local/bin 。 任务完成。

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.”

我们提到安装可以从二进制文件中删除多余的符号表和其他包,以减小其大小。 现在开始吧。 请注意,下面的命令不包括Words.db。 这是因为Words.db是数据库文件,而不是二进制可执行文件。 要复制和收缩的二进制文件ana ,我们可以使用下面的命令。 我们添加了-s(收缩)选项,并使用小写的“ s”。 我们在-b(备份)选项和-S(后缀)选项中重新添加了大写字母“ S”。

sudo install -s -b -S .bak -o dave -g geek -m 751 ana -t /usr/local/bin

Listing the files in /usr/local/bin allows us to compare the size of the ana file with its backup version. The ana file has been reduced to almost 60% of its previous size.

/usr/local/bin列出文件,使我们可以比较ana文件的大小及其备份版本。 ana文件已减少到其先前大小的近60%。

ls -l /usr/local/bin

综上所述 (In Summary)

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.

install命令可满足特定用途。 对于许多人来说,它不会日复一日地使用,或者可能逐月使用。 尽管如此, install命令还是一个熟悉和掌握技巧的好工具。 对于那些您需要的场合,它可以通过提高效率,简化操作和减少击键次数来奖励您的学习曲线。

翻译自: https://www.howtogeek.com/411366/how-to-copy-files-with-the-install-command-on-linux/

在linux里复制文件命令

在linux里复制文件命令_如何在Linux上使用“安装”命令复制文件相关推荐

  1. mac 电脑资源库文件夹_如何在Mac上找到您的资料库文件夹

    mac 电脑资源库文件夹 When fixing problems on a Mac, sometimes you need to roll up your sleeves and directly ...

  2. mac删除文件恢复_如何在Mac上恢复已删除的文件

    mac删除文件恢复 Nearly everyone has accidentally deleted a file before. Whether it's family pictures or im ...

  3. mac 命令行 解压7z文件_如何在Mac上快速压缩和解压文件?Mac上解压和压缩文件的方法...

    苹果mac电脑怎么压缩和解压文件?Mac电脑仅默认支持把文件压缩成zip格式,解压成zip.tar.gz,bz2等格式,有些操作需要安装第三方软件来完成,这篇文章为大家带来几种关于在Mac上解压和压缩 ...

  4. win10 后台运行jar包_如何在Windows10上打开或运行JAR文件

    JAR文件类似于EXE文件,除了它们需要运行Java框架. 想象一下它们作为Windows上的EXE文件,您可以双击启动该程序. 当您运行EXE文件时,它使用Windows操作系统支持. 同样,当您运 ...

  5. 如何在前端删除项目中的文件_如何在macOS上恢复已删除的文件

    有时候不小心删除了我们不打算删除的MacBook上的文件.在MacBook上恢复永久删除的文件非常困难,但这并非不可能.在本文中,我们将指导您如何恢复MacBook上最近恢复的文件或永久删除的文件.从 ...

  6. linux终端删除文件命令_如何在Linux终端中删除文件和目录

    linux终端删除文件命令 Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com T ...

  7. linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹

    linux查找文件夹命令 Most people use a graphical file manager to find files in Linux, such as Nautilus in Gn ...

  8. linux复制整个目录_如何在Linux中复制整个目录?

    linux复制整个目录 Linux provides different tools to copy directories entirely. The most popular and well-k ...

  9. linux yes 命令_如何在Linux上使用yes命令

    linux yes 命令 Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com T ...

最新文章

  1. 阿里云前端周刊 - 第 39 期
  2. UVA_11922 Permutation Transformer 【splay树】
  3. logstic 回归文章链接
  4. QT5 文件读写操作
  5. 又~发现两篇好文章~~
  6. python书籍排行榜前十名_学习Python,这些书你看过几本?
  7. 【数据库】一个 rm -rf 把公司整个数据库删没了
  8. Python机器学习:SVM002最大化margin
  9. iOS中内存管理的问题——堆和栈
  10. python路径怎么查看_:怎么查看python的安装路径
  11. java并发包是谁编写的_0.Java并发包系列开篇
  12. 【vue+cesium】加载三维天地图
  13. 12星座的出生年月日性格_十二星座的出生日期及对应性格
  14. 无源晶振有方向吗?无源贴片晶振贴反会怎样?
  15. B 站顶流何同学对话苹果 CEO 库克
  16. 远程访问openwrt路由器+配置动态DNS
  17. torch.Tensor详解
  18. Java EasyWord导出word文档
  19. uva - 123 - Searching Quickly
  20. C语言中几种输入方式

热门文章

  1. python安装numba_Numba的基本应用
  2. 工具 | 一款小巧好用的代码对比工具
  3. React阻止默认事件
  4. SpringSecurity从入门到关门放狗(一)
  5. 金融行业如何做好智慧网点敏感数据防护?
  6. linux重启网卡命令
  7. 小米最快本周提交上市申请;罗永浩再出新语录;聚美计划回购1亿美元股票丨价值早报...
  8. 数字图像处理4:逆滤波及其变形和维纳滤波
  9. 期货资管系统都有哪些功能?
  10. 评测 osmo_易用能折叠的稳定器,大疆OSMO Mobile3评测