mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-10 22:21:11 +08:00
20170705 Two great uses for the cp command (#5916)
* translated * Update 20170705 Two great uses for the cp command.md * Update 20170705 Two great uses for the cp command.md * Update 20170705 Two great uses for the cp command.md
This commit is contained in:
parent
f162d32a50
commit
31e744876b
@ -1,206 +0,0 @@
|
||||
【big_dimple翻译中】
|
||||
Two great uses for the cp command
|
||||
============================================================
|
||||
|
||||
### Linux's copy command makes quick work of making specialized backups.
|
||||
|
||||
|
||||
![Two great uses for the cp command](https://opensource.com/sites/default/files/styles/image-full-size/public/images/life/car-penguin-drive-linux-yellow.png?itok=ueZE5mph "Two great uses for the cp command")
|
||||
Image by :
|
||||
|
||||
Internet Archive [Book][10] [Images][11]. Modified by Opensource.com. CC BY-SA 4.0
|
||||
|
||||
The point-and-click graphical user interface available on Linux is a wonderful thing... but if your favorite interactive development environment consists of the terminal window, Bash, Vim, and your favorite language compiler, then, like me, you use the terminal _a lot_ .
|
||||
|
||||
But even people who generally avoid the terminal can benefit by being more aware of the riches that its environment offers. A case in point – the **cp** command. [According to Wikipedia][12], the **cp** (or copy) command was part of Version 1 of [Unix][13]. Along with a select group of other commands—**ls**, **mv**, **cd**, **pwd**, **mkdir**, **vi**, **sh**, **sed**, and **awk** come to mind—**cp** was one of my first few steps in System V Unix back in 1984\. The most common use of **cp** is to make a copy of a file, as in:
|
||||
|
||||
```
|
||||
cp sourcefile destfile
|
||||
```
|
||||
|
||||
More Linux resources
|
||||
|
||||
* [What is Linux?][1]
|
||||
|
||||
* [What are Linux containers?][2]
|
||||
|
||||
* [Download Now: Linux commands cheat sheet][3]
|
||||
|
||||
* [Advanced Linux commands cheat sheet][4]
|
||||
|
||||
* [Our latest Linux articles][5]
|
||||
|
||||
issued at the command prompt in a terminal session. The above command copies the file named **sourcefile** to the file named **destfile**. If **destfile** doesn't exist before the command is issued, it's created; if it does exist, it's overwritten.
|
||||
|
||||
I don't know how many times I've used this command (maybe I don't want to know), but I do know that I often use it when I'm writing and testing code and I have a working version of something that I want to retain as-is before I move on. So, I have probably typed something like this:
|
||||
|
||||
```
|
||||
cp test1.py test1.bak
|
||||
```
|
||||
|
||||
at a command prompt at least a zillion times over the past 30+ years. Alternatively, I might have decided to move on to version 2 of my test program, in which case I may have typed:
|
||||
|
||||
```
|
||||
cp test1.py test2.py
|
||||
```
|
||||
|
||||
to accomplish the first step of that move.
|
||||
|
||||
This is such a common and simple thing to do that I have rarely ever looked at the reference documentation for **cp**. But, while backing up my Pictures folder (using the Files application in my GUI environment), I started thinking, "I wonder if there is an option to have **cp** copy over only new files or those that have changed?" And sure enough, there is!
|
||||
|
||||
### Great use #1: Updating a second copy of a folder
|
||||
|
||||
Let's say I have a folder on my computer that contains a collection of files. Furthermore, let's say that from time to time I put a new file into that collection. Finally, let's say that from time to time I might edit one of those files in some way. An example of such a collection might be the photos I download from my cellphone or my music files.
|
||||
|
||||
Assuming that this collection of files has some enduring value to me, I might occasionally want to make a copy of it—a kind of "snapshot" of it—to preserve it on some other media. Of course, there are many utility programs that exist for doing backups, but maybe I want to have this exact structure duplicated on a removable device that I generally store offline or even connect to another computer.
|
||||
|
||||
The **cp** command offers a dead-easy way to do this. Here's an example.
|
||||
|
||||
In my **Pictures** folder, I have a sub-folder called **Misc**. For illustrative purposes, I'm going to make a copy of it on a USB memory stick. Here we go!
|
||||
|
||||
```
|
||||
me@desktop:~/Pictures$ cp -r Misc /media/clh/4388-D5FE
|
||||
me@desktop:~/Pictures$
|
||||
```
|
||||
|
||||
The above lines are copied as-is from my terminal window. For those who might not be fully comfortable with that environment, it's worth noting that **me @mydesktop:~/Pictures$** is the command prompt provided by the terminal before every command is entered and executed. It identifies the user (**me**), the computer (**mydesktop**), and the current working directory, in this case, **~/Pictures**, which is shorthand for **/home/me/Pictures**, that is, the **Pictures** folder in my home directory.
|
||||
|
||||
The command I've entered and executed, **cp -r Misc /media/clh/4388-D5FE**, copies the folder **Misc** and all its contents (the **-r**, or "recursive," option indicates the contents as well as the folder or file itself) into the folder **/media/clh/4388-D5FE**, which is where my USB stick is mounted.
|
||||
|
||||
Executing the command returned me to the original prompt. Like with most commands inherited from Unix, if the command executes without detecting any kind of anomalous result, it won't print out a message like "execution succeeded" before terminating. People who would like more feedback can use the **-v** option to make execution "verbose."
|
||||
|
||||
Below is an image of my new copy of **Misc** on the USB drive. There are nine JPEG files in the directory.
|
||||
|
||||
### [cp1_file_structure.png][6]
|
||||
|
||||
![Image of the new copy of Misc on the USB drive](https://opensource.com/sites/default/files/u128651/cp1_file_structure.png "Image of the new copy of Misc on the USB drive")
|
||||
|
||||
Suppose I add a few new files to the master copy of the directory **~/Pictures/Misc**, so now it looks like this:
|
||||
|
||||
### [cp2_new_files.png][7]
|
||||
|
||||
![New files added to the master directory](https://opensource.com/sites/default/files/u128651/cp2_new_files.png "New files added to the master directory")
|
||||
|
||||
Now I want to copy over only the new files to my memory stick. For this I'll use the "update" and "verbose" options to **cp**:
|
||||
|
||||
```
|
||||
me@desktop:~/Pictures$ cp -r -u -v Misc /media/clh/4388-D5FE
|
||||
'Misc/asunder.png' -> '/media/clh/4388-D5FE/Misc/asunder.png'
|
||||
'Misc/editing tags guayadeque.png' -> '/media/clh/4388-D5FE/Misc/editing tags guayadeque.png'
|
||||
'Misc/misc on usb.png' -> '/media/clh/4388-D5FE/Misc/misc on usb.png'
|
||||
me@desktop:~/Pictures$
|
||||
```
|
||||
|
||||
The first line above shows the **cp** command and its options (**-r** for "recursive", **-u** for "update," and **-v** for "verbose"). The next three lines show the files that are copied across. The last line shows the command prompt again.
|
||||
|
||||
Generally speaking, options such as **-r** can also be given in a more verbose fashion, such as **--recursive**. In brief form, they can also be combined, such as **-ruv**.
|
||||
|
||||
### Great use #2 – Making versioned backups
|
||||
|
||||
Returning to my initial example of making periodic backups of working versions of code in development, another really useful **cp** option I discovered while learning about update is backup.
|
||||
|
||||
Suppose I'm setting out to write a really useful Python program. Being a fan of iterative development, I might do so by getting a simple version of the program working first, then successively adding more functionality to it until it does the job. Let's say my first version just prints the string "hello world" using the Python print command. This is a one-line program that looks like this:
|
||||
|
||||
```
|
||||
print 'hello world'
|
||||
```
|
||||
|
||||
and I've put that string in the file **test1.py**. I can run it from the command line as follows:
|
||||
|
||||
```
|
||||
me@desktop:~/Test$ python test1.py
|
||||
hello world
|
||||
me@desktop:~/Test$
|
||||
```
|
||||
|
||||
Now that the program is working, I want to make a backup of it before adding the next component. I decide to use the backup option with numbering, as follows:
|
||||
|
||||
```
|
||||
clh@vancouver:~/Test$ cp --force --backup=numbered test1.py test1.py
|
||||
clh@vancouver:~/Test$ ls
|
||||
test1.py test1.py.~1~
|
||||
clh@vancouver:~/Test$
|
||||
```
|
||||
|
||||
So, what does this all mean?
|
||||
|
||||
First, the **--backup=numbered** option says, "I want to do a backup, and I want successive backups to be numbered." So the first backup will be number 1, the second 2, and so on.
|
||||
|
||||
Second, note that the source file and destination file are the same. Normally, if we try to use the **cp** command to copy a file onto itself, we will receive a message like:
|
||||
|
||||
```
|
||||
cp: 'test1.py' and 'test1.py' are the same file
|
||||
```
|
||||
|
||||
In the special case where we are doing a backup and we want the same source and destination, we use the **--force** option.
|
||||
|
||||
Third, I used the **ls** (or "list") command to show that we now have a file called **test1.py**, which is the original, and another called **test1.py.~1~**, which is the backup file.
|
||||
|
||||
Suppose now that the second bit of functionality I want to add to the program is another print statement that prints the string "Kilroy was here." Now the program in file **test1.py**looks like this:
|
||||
|
||||
```
|
||||
print 'hello world'
|
||||
print 'Kilroy was here'
|
||||
```
|
||||
|
||||
See how simple Python programming is? Anyway, if I again execute the backup step, here's what happens:
|
||||
|
||||
```
|
||||
clh@vancouver:~/Test$ cp --force --backup=numbered test1.py test1.py
|
||||
clh@vancouver:~/Test$ ls
|
||||
test1.py test1.py.~1~ test1.py.~2~
|
||||
clh@vancouver:~/Test$
|
||||
```
|
||||
|
||||
Now we have two backup files: **test1.py.~1~**, which contains the original one-line program, and **test1.py.~2~**, which contains the two-line program, and I can move on to adding and testing some more functionality.
|
||||
|
||||
This is such a useful thing to me that I am considering making a shell function to make it simpler.
|
||||
|
||||
### Three points to wrap this up
|
||||
|
||||
First, the Linux manual pages, installed by default on most desktop and server distros, provide details and occasionally useful examples of commands like **cp**. At the terminal, enter the command:
|
||||
|
||||
```
|
||||
man cp
|
||||
```
|
||||
|
||||
Such explanations can be dense and obscure to users just trying to learn how to use a command in the first place. For those inclined to persevere nevertheless, I suggest creating a test directory and files and trying the command and options out there.
|
||||
|
||||
Second, if a tutorial is of greater interest, the search string "linux shell tutorial" typed into your favorite search engine brings up a lot of interesting and useful resources.
|
||||
|
||||
Third, if you're wondering, "Why bother when the GUI typically offers the same functionality with point-and-click ease?" I have two responses. The first is that "point-and-click" isn't always that easy, especially when it disrupts another workflow and requires a lot of points and a lot of clicks to make it work. The second is that repetitive tasks can often be easily streamlined through the use of shell scripts, shell functions, and shell aliases.
|
||||
|
||||
Are you using the **cp** command in new or interesting ways? Let us know about them in the comments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
Chris Hermansen - Engaged in computing since graduating from the University of British Columbia in 1978, I have been a full-time Linux user since 2005 and a full-time Solaris, SunOS and UNIX System V user before that. On the technical side of things, I have spent a great deal of my career doing data analysis; especially spatial data analysis. I have a substantial amount of programming experience in relation to data analysis, using awk, Python, PostgreSQL, PostGIS and lately Groovy.
|
||||
|
||||
|
||||
via: https://opensource.com/article/17/7/two-great-uses-cp-command
|
||||
|
||||
作者:[ Chris Hermansen ][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/clhermansen
|
||||
[1]:https://opensource.com/resources/what-is-linux?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[2]:https://opensource.com/resources/what-are-linux-containers?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[3]:https://developers.redhat.com/promotions/linux-cheatsheet/?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[4]:https://developers.redhat.com/cheat-sheet/advanced-linux-commands-cheatsheet?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[5]:https://opensource.com/tags/linux?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[6]:https://opensource.com/file/360601
|
||||
[7]:https://opensource.com/file/360606
|
||||
[8]:https://opensource.com/article/17/7/two-great-uses-cp-command?rate=87TiE9faHZRes_f4Gj3yQZXhZ-x7XovYhnhjrk3SdiM
|
||||
[9]:https://opensource.com/user/37806/feed
|
||||
[10]:https://www.flickr.com/photos/internetarchivebookimages/14746482994/in/photolist-ot6zCN-odgbDq-orm48o-otifuv-otdyWa-ouDjnZ-otGT2L-odYVqY-otmff7-otGamG-otnmSg-rxnhoq-orTmKf-otUn6k-otBg1e-Gm6FEf-x4Fh64-otUcGR-wcXsxg-tLTN9R-otrWYV-otnyUE-iaaBKz-ovcPPi-ovokCg-ov4pwM-x8Tdf1-hT5mYr-otb75b-8Zk6XR-vtefQ7-vtehjQ-xhhN9r-vdXhWm-xFBgtQ-vdXdJU-vvTH6R-uyG5rH-vuZChC-xhhGii-vvU5Uv-vvTNpB-vvxqsV-xyN2Ai-vdXcFw-vdXuNC-wBMhes-xxYmxu-vdXxwS-vvU8Zt
|
||||
[11]:https://www.flickr.com/photos/internetarchivebookimages/14774719031/in/photolist-ovAie2-otPK99-xtDX7p-tmxqWf-ow3i43-odd68o-xUPaxW-yHCtWi-wZVsrD-DExW5g-BrzB7b-CmMpC9-oy4hyF-x3UDWA-ow1m4A-x1ij7w-tBdz9a-tQMoRm-wn3tdw-oegTJz-owgrs2-rtpeX1-vNN6g9-owemNT-x3o3pX-wiJyEs-CGCC4W-owg22q-oeT71w-w6PRMn-Ds8gyR-x2Aodm-owoJQm-owtGp9-qVxppC-xM3Gw7-owgV5J-ou9WEs-wihHtF-CRmosE-uk9vB3-wiKdW6-oeGKq3-oeFS4f-x5AZtd-w6PNuv-xgkofr-wZx1gJ-EaYPED-oxCbFP
|
||||
[12]:https://en.wikipedia.org/wiki/Cp_(Unix)
|
||||
[13]:https://en.wikipedia.org/wiki/Unix
|
||||
[14]:https://opensource.com/users/clhermansen
|
||||
[15]:https://opensource.com/users/clhermansen
|
||||
[16]:https://opensource.com/article/17/7/two-great-uses-cp-command#comments
|
188
translated/tech/20170705 Two great uses for the cp command.md
Normal file
188
translated/tech/20170705 Two great uses for the cp command.md
Normal file
@ -0,0 +1,188 @@
|
||||
【big_dimple翻译完成】
|
||||
# cp命令两个高效的用法
|
||||
|
||||
============================================================
|
||||
|
||||
### Linux中高效的备份拷贝命令
|
||||
|
||||
|
||||
![Two great uses for the cp command](https://opensource.com/sites/default/files/styles/image-full-size/public/images/life/car-penguin-drive-linux-yellow.png?itok=ueZE5mph "Two great uses for the cp command")
|
||||
|
||||
图像来自于:Opensource.com
|
||||
|
||||
在Linux上能使用图形化界面是一件很美妙的事...但是如果你像我一样,喜欢的开发交互环境和编译器是终端窗口,Bash 和 Vim,那你将会经常和终端打交道。
|
||||
|
||||
即使是不经常使用终端的人,也能通过终端提供的环境来获取很多有用的知识。举个例子—**cp**命令,通过[Wiki百科][12]的解释,**cp** (或者copy)命令是[Unix][13]系统第一个版本中的一部分。以及其他的一组命令 **ls**, **mv**, **cd**, **pwd**, **mkdir**, **vi**, **sh**, **sed**, 和 **awk** 还有我们所说的 **cp** 都是在 1984 年构造Unix系统第一个版本中最初的几步。**cp** 命令最常见的用法是复制文件。像这样:
|
||||
|
||||
```
|
||||
cp sourcefile destfile
|
||||
```
|
||||
在终端中执行此命令,上述命令将名为 **sourcefile** 的文件复制到名为 **destfile** 的文件中。如果在执行命令之前 **destfile** 文件不存在,那将会创建此文件,如果已经存在,那就会覆盖此文件。
|
||||
|
||||
这个命令我不知道自己用了多少次了(或许我不想知道),但是我知道在我测试代码的时候,我经常用,为了保留当前版本,而且又能继续修改,我会输入这个命令:
|
||||
|
||||
```
|
||||
cp test1.py test1.bak
|
||||
```
|
||||
在过去的30多年里,我使用了无数次这个命令。另外,当我决定编写我的第二版测试程序时,我会输入这个命令:
|
||||
|
||||
```
|
||||
cp test1.py test2.py
|
||||
```
|
||||
这样就完成了修改程序版本的第一步。
|
||||
|
||||
我通常很少查看 **cp** 命令的参考文档,但是当我在备份我的图片文件夹的时候(在GUI环境下使用文件应用),我开始思考“在 **cp** 命令中是否有个参数支持只复制新文件或者是修改过的文件。”果然,真的有!
|
||||
|
||||
### 高效用法1:更新你的文件夹
|
||||
比如说在我的电脑上有一个存放各种文件的文件夹,另外我要不时的往里面添加一些新文件,而且我会不时地修改一些文件,例如我手机里下载的照片或者是音乐。
|
||||
|
||||
假设我收集的这些文件对我而言都很有价值,我有时候会想做个拷贝,就像是“快照”一样将文件保存在其它媒体。当然目前有很多程序都支持备份,但是我想更为精确的将目录结构复制到可移动设备中,方便于我经常使用这些离线设备或者连接到其它电脑上。
|
||||
|
||||
**cp** 命令提供了一个易如反掌的方法。例子如下:
|
||||
|
||||
在我的 **Pictures** 文件夹下,我有这样一个文件夹名字为 **Misc**。为了方便说明,我把文件拷贝到USB存储设备上。让我们开始吧!
|
||||
|
||||
```
|
||||
me@desktop:~/Pictures$ cp -r Misc /media/clh/4388-D5FE
|
||||
me@desktop:~/Pictures$
|
||||
```
|
||||
上面的命令是我从按照终端窗口中完整复制下来的。对于有些人来说这种环境可能不是很舒服,在我们输入命令或者执行命令之前,需要注意的是 **me@mydesktop:~/Pictures** 这个前缀,(**me**)这个是当前用户,(**mydesktop**)这是电脑名称,(**~/Pictures**)这个是当前工作目录,是(**/home/me/Pictures**)完整路径的缩写。
|
||||
|
||||
我输入这个命令并执行后(**cp -r Misc /media/clh/4388-D5FE**),拷贝 **Misc** 目录下所有文件(这个 **-r**参数,全称“recursive”,递归处理,意思为本目录下所有文件及子目录一起处理)到我的USB设备的挂载目录 **/media/clh/4388-D5FE**。
|
||||
|
||||
执行命令后回到之前的提示,大多数命令继承了Unix的特性,在命令执行后,如果没有任何异常什么都不显示,在任务结束之前不会显示像“execution succeeded“这样的提示符。如果想获取更多的反馈,就使用 **-v** 参数让执行结果更详细。
|
||||
|
||||
下图中是我的USB设备中刚刚拷贝过来的文件夹 **Misc** ,里面总共有 9 张图片。
|
||||
|
||||
### [cp1_file_structure.png][6]
|
||||
|
||||
![Image of the new copy of Misc on the USB drive](https://opensource.com/sites/default/files/u128651/cp1_file_structure.png "Image of the new copy of Misc on the USB drive")
|
||||
|
||||
假设我要在原始拷贝路径下 **~/Pictures/Misc** 下添加一些新文件,就像这样:
|
||||
|
||||
### [cp2_new_files.png][7]
|
||||
|
||||
![New files added to the master directory](https://opensource.com/sites/default/files/u128651/cp2_new_files.png "New files added to the master directory")
|
||||
|
||||
现在我想只拷贝新的文件到我的存储设备上,我就使用 **cp** 的“update”和“verbose”选项。
|
||||
|
||||
```
|
||||
me@desktop:~/Pictures$ cp -r -u -v Misc /media/clh/4388-D5FE
|
||||
'Misc/asunder.png' -> '/media/clh/4388-D5FE/Misc/asunder.png'
|
||||
'Misc/editing tags guayadeque.png' -> '/media/clh/4388-D5FE/Misc/editing tags guayadeque.png'
|
||||
'Misc/misc on usb.png' -> '/media/clh/4388-D5FE/Misc/misc on usb.png'
|
||||
me@desktop:~/Pictures$
|
||||
```
|
||||
上面的第一行中是 **cp** 命令和具体的参数(**-r** 是“recursive”, **-u** 是“update”,**-v** 是“verbose”)。接下来的三行显示被复制文件的信息,最后一行显示命令行前缀。
|
||||
|
||||
通常来说,参数 **-r** 也可用用更详细的方式使用 **--recursive**。但是以简短的方式,也可以这么用 **-ruv**。
|
||||
|
||||
###高效用法2:版本备份
|
||||
|
||||
回到一开始的例子中,我在开发的时候定期给我的代码版本进行备份。然后我找到了另一种更好用的 **cp** 参数。
|
||||
|
||||
假设我正在编写一个非常有用的Python程序,作为一个喜欢不断修改代码的开发者,我会在一开始定义一个程序简单版本,然后不停的忘里面添加各种功能直到它能成功的运行起来。比方说我的第一个版本就是用Python程序打印出“hello world”。这只有一行代码的程序就像这样:
|
||||
|
||||
```
|
||||
print 'hello world'
|
||||
```
|
||||
|
||||
然后我将这个代码保存成文件命名为 **test1.py**。我可以这么运行它:
|
||||
|
||||
```
|
||||
me@desktop:~/Test$ python test1.py
|
||||
hello world
|
||||
me@desktop:~/Test$
|
||||
```
|
||||
|
||||
现在程序可以运行了,我想在添加新的内容之前进行备份。我决定使用带编号的备份选项,如下:
|
||||
|
||||
```
|
||||
clh@vancouver:~/Test$ cp --force --backup=numbered test1.py test1.py
|
||||
clh@vancouver:~/Test$ ls
|
||||
test1.py test1.py.~1~
|
||||
clh@vancouver:~/Test$
|
||||
```
|
||||
|
||||
所以,上面的做法是什么意思呢?
|
||||
|
||||
第一,这个 **--backup=numbered** 参数意思为“我要做个备份,而且是带编号的连续备份”。所以一个备份就是 1 号,第二个就是 2 号,等等。
|
||||
|
||||
第二,如果源文件和目标文件名字是一样的。通常我们使用 **cp** 命令去拷贝成自己,会得到这样的报错信息:
|
||||
|
||||
```
|
||||
cp: 'test1.py' and 'test1.py' are the same file
|
||||
```
|
||||
|
||||
在特殊情况下,如果我们想备份的源文件和目标文件名字相同,我们使用 **--force** 参数。
|
||||
|
||||
第三,我使用 **ls** (或者是“list”)命令来显示现在目录下的文件,名字为 **test1.py** 的是原始文件,名字为 **test1.py.~1~** 的是备份文件
|
||||
|
||||
假如现在我要加上第二个功能,在程序里加上另一行代码,可以打印“Kilroy was here.”。现在程序文件 **test1.py** 的内容如下:
|
||||
|
||||
```
|
||||
print 'hello world'
|
||||
print 'Kilroy was here'
|
||||
```
|
||||
|
||||
看到Python代码如此简单了吗?不管怎样,如果我再次执行备份的步骤,结果如下:
|
||||
|
||||
|
||||
```
|
||||
clh@vancouver:~/Test$ cp --force --backup=numbered test1.py test1.py
|
||||
clh@vancouver:~/Test$ ls
|
||||
test1.py test1.py.~1~ test1.py.~2~
|
||||
clh@vancouver:~/Test$
|
||||
```
|
||||
现在我有有两个备份文件: **test1.py.~1~** 包含了一行代码的程序,和 **test1.py.~2~** 包含两行代码的程序。
|
||||
|
||||
这个很好用的功能,让我在进行shell编程的时候变得更为简单。
|
||||
|
||||
###最后总结
|
||||
|
||||
第一,Linux手册页,默认安装在大多数桌面和服务器发行版,它提供了更为详细的使用方法和例子,就像 **cp** 命令,在终端中输入如下命令:
|
||||
|
||||
```
|
||||
man cp
|
||||
```
|
||||
|
||||
对于那些想学习如何使用这些命令,但不清楚如何使用的用户应该首先看一下这些说明,然后我建议创建一个测试目录和文件来尝试使用命令和选项。
|
||||
|
||||
第二,兴趣是最好的老师。在你最喜欢的搜索引擎中搜索“linux shell tutorial”,你会获得很多有趣和有用的资源。
|
||||
|
||||
第三,你是不是在想,“为什么我要用这么麻烦的方法,图形化界面中有相同的功能,只用点击几下岂不是更简单?”,关于这个问题我有两个理由。首先,在我们工作中需要中断其他工作流程以及大量使用点击动作时,点击动作可就不简单了。其次,如果我们要完成流水线般的重复性工作,通过使用shell脚本和shell函数以及shell重命名等功能就能很轻松的实现。
|
||||
|
||||
你还知道关于 **cp** 命令其他更棒的使用方式吗?请在留言中积极回复哦~
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
Chris Hermansen - 1978 年毕业于英国哥伦比亚大学后一直从事计算机相关职业,我从2005年开始一直使用Linux,Solaris, SunOS,在那之前我就是Unix系统管理员了,在技术方面,我花了大量的职业生涯做数据分析,尤其是空间数据分析,我有大量的编程经验与数据分析经验,熟练使用awk,Python,PostgreSQL,PostGIS和Groovy。
|
||||
|
||||
|
||||
原文: https://opensource.com/article/17/7/two-great-uses-cp-command
|
||||
|
||||
作者:[ Chris Hermansen ][a]
|
||||
译者:[bigdimple](https://github.com/bigdimple)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/clhermansen
|
||||
[1]:https://opensource.com/resources/what-is-linux?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[2]:https://opensource.com/resources/what-are-linux-containers?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[3]:https://developers.redhat.com/promotions/linux-cheatsheet/?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[4]:https://developers.redhat.com/cheat-sheet/advanced-linux-commands-cheatsheet?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[5]:https://opensource.com/tags/linux?intcmp=70160000000h1jYAAQ&utm_source=intcallout&utm_campaign=linuxcontent
|
||||
[6]:https://opensource.com/file/360601
|
||||
[7]:https://opensource.com/file/360606
|
||||
[8]:https://opensource.com/article/17/7/two-great-uses-cp-command?rate=87TiE9faHZRes_f4Gj3yQZXhZ-x7XovYhnhjrk3SdiM
|
||||
[9]:https://opensource.com/user/37806/feed
|
||||
[10]:https://www.flickr.com/photos/internetarchivebookimages/14746482994/in/photolist-ot6zCN-odgbDq-orm48o-otifuv-otdyWa-ouDjnZ-otGT2L-odYVqY-otmff7-otGamG-otnmSg-rxnhoq-orTmKf-otUn6k-otBg1e-Gm6FEf-x4Fh64-otUcGR-wcXsxg-tLTN9R-otrWYV-otnyUE-iaaBKz-ovcPPi-ovokCg-ov4pwM-x8Tdf1-hT5mYr-otb75b-8Zk6XR-vtefQ7-vtehjQ-xhhN9r-vdXhWm-xFBgtQ-vdXdJU-vvTH6R-uyG5rH-vuZChC-xhhGii-vvU5Uv-vvTNpB-vvxqsV-xyN2Ai-vdXcFw-vdXuNC-wBMhes-xxYmxu-vdXxwS-vvU8Zt
|
||||
[11]:https://www.flickr.com/photos/internetarchivebookimages/14774719031/in/photolist-ovAie2-otPK99-xtDX7p-tmxqWf-ow3i43-odd68o-xUPaxW-yHCtWi-wZVsrD-DExW5g-BrzB7b-CmMpC9-oy4hyF-x3UDWA-ow1m4A-x1ij7w-tBdz9a-tQMoRm-wn3tdw-oegTJz-owgrs2-rtpeX1-vNN6g9-owemNT-x3o3pX-wiJyEs-CGCC4W-owg22q-oeT71w-w6PRMn-Ds8gyR-x2Aodm-owoJQm-owtGp9-qVxppC-xM3Gw7-owgV5J-ou9WEs-wihHtF-CRmosE-uk9vB3-wiKdW6-oeGKq3-oeFS4f-x5AZtd-w6PNuv-xgkofr-wZx1gJ-EaYPED-oxCbFP
|
||||
[12]:https://en.wikipedia.org/wiki/Cp_(Unix)
|
||||
[13]:https://en.wikipedia.org/wiki/Unix
|
||||
[14]:https://opensource.com/users/clhermansen
|
||||
[15]:https://opensource.com/users/clhermansen
|
||||
[16]:https://opensource.com/article/17/7/two-great-uses-cp-command#comments
|
Loading…
Reference in New Issue
Block a user