mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
translated
This commit is contained in:
parent
04559dae08
commit
6772443615
@ -1,89 +0,0 @@
|
||||
translating---geekpi
|
||||
|
||||
How to Copy a File to Multiple Directories in Linux
|
||||
============================================================
|
||||
|
||||
[While learning Linux][1], it is always the norm for newbies to keep on typing several commands to accomplish a simple task. This is understandable especially when one is just getting accustomed to using the terminal.
|
||||
|
||||
However, as you look forward to becoming a Linux power user, learning what I would refer to as “shortcut commands” can significantly reduce time wasting tendencies.
|
||||
|
||||
In this article, we will explain an easy way, using a single command to copy a file into multiple directories in Linux.
|
||||
|
||||
In Linux, the [cp command][2] is used to copy files from one directory to another, the easiest syntax for using it is as follows:
|
||||
|
||||
```
|
||||
# cp [options….] source(s) destination
|
||||
```
|
||||
|
||||
Alternatively, you can also use the [advanced-copy command][3], which shows a progress bar while copying [large files/folders in Linux][4].
|
||||
|
||||
Consider the commands below, normally, you would type two different commands to copy the same file into two separate directories as follows:
|
||||
|
||||
```
|
||||
# cp -v /home/aaronkilik/bin/sys_info.sh /home/aaronkilik/test
|
||||
# cp -v /home/aaronkilik/bin/sys_info.sh /home/aaronkilik/tmp
|
||||
```
|
||||
[
|
||||
![Copy Files to Multiple Directories](http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories.png)
|
||||
][5]
|
||||
|
||||
Copy Files to Multiple Directories
|
||||
|
||||
Assuming that you want to copy a particular file into up to five or more directories, this means you would have to type five or more cp commands?
|
||||
|
||||
To do away with this problem, you can employ the [echo command][6], a pipe, xargs command together with the cpcommand in the form below:
|
||||
|
||||
```
|
||||
# echo /home/aaronkilik/test/ /home/aaronkilik/tmp | xargs -n 1 cp -v /home/aaronkilik/bin/sys_info.sh
|
||||
```
|
||||
|
||||
In the form above, the paths to the directories (dir1,dir2,dir3…..dirN) are echoed and piped as input to the xargscommand where:
|
||||
|
||||
1. `-n 1` – tells xargs to use at most one argument per command line and send to the cp command.
|
||||
2. `cp` – used to copying a file.
|
||||
3. `-v` – enables verbose mode to show details of the copy operation.
|
||||
|
||||
[
|
||||
![Copy File to Multiple Locations in Linux](http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories-in-Linux.png)
|
||||
][7]
|
||||
|
||||
Copy File to Multiple Locations in Linux
|
||||
|
||||
Try to read through the man pages of `cp`, `echo` and `xargs` commands to find useful and advanced usage information:
|
||||
|
||||
```
|
||||
$ man cp
|
||||
$ man echo
|
||||
$ man xargs
|
||||
```
|
||||
|
||||
That’s all, you can send us questions in relation to the topic or any feedback through the comment form below. You may also want to read about the [progress command][8] which helps to monitor the progress of (cp, mv, dd, [tar][9], etc.) commands that are presently running in Linux.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
|
||||
|
||||
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/copy-file-to-multiple-directories-in-linux/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
[1]:http://www.tecmint.com/free-online-linux-learning-guide-for-beginners/
|
||||
[2]:http://www.tecmint.com/advanced-copy-command-shows-progress-bar-while-copying-files/
|
||||
[3]:http://www.tecmint.com/advanced-copy-command-shows-progress-bar-while-copying-files/
|
||||
[4]:http://www.tecmint.com/find-top-large-directories-and-files-sizes-in-linux/
|
||||
[5]:http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories.png
|
||||
[6]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[7]:http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories-in-Linux.png
|
||||
[8]:http://www.tecmint.com/progress-monitor-check-progress-of-linux-commands/
|
||||
[9]:http://www.tecmint.com/18-tar-command-examples-in-linux/
|
@ -0,0 +1,87 @@
|
||||
如何在Linux中复制文件到多个目录中
|
||||
============================================================
|
||||
|
||||
[在使用Linux中][1],对于新手而言总是会使用几个命令来完成一个简单的任务。对正在熟悉使用终端的人这是很容易理解的行为。
|
||||
|
||||
然而,如果你想要成为一个老手,学习我说的“快捷命令”会显著减少时间浪费。
|
||||
|
||||
在本篇中,我们会用一个简单的方法在Linux中用一个命令来将目录复制到多个文件夹中。
|
||||
|
||||
在Linux中,[cp命令][2]长被用于从一个文件夹中复制文件到另一个中,最简单的语法如下:
|
||||
|
||||
```
|
||||
# cp [options….] source(s) destination
|
||||
```
|
||||
|
||||
另外,你也可以使用[一个高级复制命令][3],它可以在复制[大的文件或文件夹中][4]显示进度条。
|
||||
|
||||
看下下面的命令,通常你会使用两个不同的命令拉将相同的文件复制到不同的文件夹中:
|
||||
|
||||
```
|
||||
# cp -v /home/aaronkilik/bin/sys_info.sh /home/aaronkilik/test
|
||||
# cp -v /home/aaronkilik/bin/sys_info.sh /home/aaronkilik/tmp
|
||||
```
|
||||
[
|
||||
![Copy Files to Multiple Directories](http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories.png)
|
||||
][5]
|
||||
|
||||
复制文件到多个文件夹中
|
||||
|
||||
假设你想要复制一个特定文件到5个或者更多的文件夹中,这意味着你需要输入5次或者更多的cp命令?
|
||||
|
||||
要摆脱这个问题,你可以用cp命令与[echo命令][6]、管道、xargs命令一起使用:
|
||||
|
||||
```
|
||||
# echo /home/aaronkilik/test/ /home/aaronkilik/tmp | xargs -n 1 cp -v /home/aaronkilik/bin/sys_info.sh
|
||||
```
|
||||
|
||||
上面的命令中,目录的路径(dir1、dir2、dir3...dirN)被管道作为输入到xargs命令中,含义是:
|
||||
|
||||
1. `-n 1` - 告诉每个命令最多使用一个参数,并发送到cp命令中。
|
||||
2. `cp` – 用于复制文件。
|
||||
3. `-v` – 启用verbose模式来显示更多复制细节。
|
||||
|
||||
[
|
||||
![Copy File to Multiple Locations in Linux](http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories-in-Linux.png)
|
||||
][7]
|
||||
|
||||
在Linux中复制文件到多个位置中
|
||||
|
||||
尝试阅读`cp`、 `echo` 和 `xargs`的man页面来找出所有有用和高级的用法信息:
|
||||
|
||||
```
|
||||
$ man cp
|
||||
$ man echo
|
||||
$ man xargs
|
||||
```
|
||||
|
||||
就是这样了,你可以在下面的评论区给我们发送主题相关的问题或者反馈。你也可以阅读有关[progress命令][8]来帮助监控运行中的(cp、mv、dd、[tar][9]等等)的进度。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
|
||||
|
||||
Aaron Kili是一个Linux及F.O.S.S热衷者,即将是Linux系统管理员、web开发者,目前是TecMint的内容创作者,他喜欢用电脑工作,并坚信分享知识。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/copy-file-to-multiple-directories-in-linux/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
[1]:http://www.tecmint.com/free-online-linux-learning-guide-for-beginners/
|
||||
[2]:http://www.tecmint.com/advanced-copy-command-shows-progress-bar-while-copying-files/
|
||||
[3]:http://www.tecmint.com/advanced-copy-command-shows-progress-bar-while-copying-files/
|
||||
[4]:http://www.tecmint.com/find-top-large-directories-and-files-sizes-in-linux/
|
||||
[5]:http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories.png
|
||||
[6]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[7]:http://www.tecmint.com/wp-content/uploads/2016/12/Copy-Files-to-Multiple-Directories-in-Linux.png
|
||||
[8]:http://www.tecmint.com/progress-monitor-check-progress-of-linux-commands/
|
||||
[9]:http://www.tecmint.com/18-tar-command-examples-in-linux/
|
Loading…
Reference in New Issue
Block a user