命令行星期二——第四部分
命令行 命令行星期二
大家好。新的一周，新的冒险！


今天，我们将会学习使用四个相当简单的命令去操作文件。好，废话少说，我们开始吧。


在开始学习这些命令之前，我们先稍微说些题外话，讲讲“通配符”的用处。


### 通配符


使用图形工具区复制、粘贴、新建目录等操作也许很容易，但是若想完成一些更复杂的任务，例如仅仅将一个目录下的所有.html文件复制到另一个目录中、或者只复制在某个目录中不存在的文件，这时命令行也许会比较方便。我们回到通配符的学习中，通配符是shell的基本功能，它是一个由一些特殊字符组成的集合，它让你可以用一些简单的规则来选择出某些文件。（通配符可以出现在文件名中，用于指定文件名的字符个数和字母的大/小写等规则）。


![](/data/attachment/album/201407/22/161822sbgb4ohlpkw4bbth.jpg)


如下表 ：


![](/data/attachment/album/201407/22/161459unhlavnyxtgbmvgv.png)


下面是肖茨先生给出的一些实例，如下表：


![](/data/attachment/album/201407/22/161501neym9z7z8zkhyn15.png)


如果你使用一个包含文件名参数的命令，你就可以使用通配符。


### cp


cp是一个用于复制文件或者目录的命令，它的用法相当的简单。进入到你想复制的文件所在的目录，然后使用如下命令


cp file1 file2 -复制一个文件


或者


cp file1 file2 ... directory -从当前工作目录复制多个文件到指定的目录。


下表是肖茨先生给出的cp命令的一些选项：


![](/data/attachment/album/201407/22/161507xt9uj9ufj66jten6.png)


### mv


mv是今天的第二个命令，我们可以使用mv来重命名一个文件或目录，或者移动一个文件或目录。我们可以这样使用mv命令。


mv filename1 filename2 -若想将文件filename1重命名为filename2。


或者


mv file directory -若想将一个文件移动到某个目录。


下表是一些mv命令的实例


![](/data/attachment/album/201407/22/161510kvsjoy80ym83m0yn.png)


### rm


rm命令是用于删除文件或目录，它的用法比较直接，如下：


rm file


或者


rm -r driectory


这里也有一个包含rm其他选项的表


![](/data/attachment/album/201407/22/161512lgahr44c5xjc15tx.png)


但是，使用rm命令时要小心点。因为并没有撤销删除的选项，因此使用rm命令式要格外的小心，避免对你的系统造成不必要的破坏。


### mkdir


mkdir是用于创建目录.它是今天最简单的一个命令：


mkdir directory


看，目录成功创建了！


这是本周的内容，下周二再见，致以最真诚的问候！




---


via: <https://news.opensuse.org/2014/07/08/command-line-tuesdays-part-four/>


译者：[cvsher](https://github.com/cvsher) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译，[Linux中国](http://linux.cn/) 荣誉推出


OK
# Command Line Tuesdays - Part Four

8. Jul 2014 | Nenad Latinović | No License

Heya there geekos! New week, new adventure!

Today, we’ll learn how to manipulate files using four fairly simple commands. So let’s begin!

Before we start with the commands themselves, let’s take a quick stop at a section called…

##### …wildcards:

Copying, pasting files, creating directories etc. is probably easier using graphical tools, but, if you’d like to perform more complicated tasks, like copying only .html files from one folder to another, and only copying files that don’t exist in the destination directory, CLI just might come in handy. So, to get back to wildcards, it’s basically a shell feature, a set of special characters, that helps you pick out a set of files based on some simple rules (which characters appear in a file name, how many characters, upper/lower case characters etc.). Here’s the table (click to enlarge):

And here are a few examples mr Shotts posted in a table of usage also click to enlarge:

[If you use a command with an argument containing a filename, you can use wildcards with no problem.](/wp-content/uploads/2014/07/Screenshot-08.-07.-2014-125959.png)![Screenshot - 08. 07. 2014 - 12:59:59](/wp-content/uploads/2014/07/Screenshot-08.-07.-2014-125959.png)


![Screenshot - 08. 07. 2014 - 12:59:59](/wp-content/uploads/2014/07/Screenshot-08.-07.-2014-125959.png)

##### cp

cp is used to copy files or directories. You can use it pretty easily: navigate to the folder you’d like to copy the files from and to, and simply do

*cp file1 file2* - to copy single files,

or

*cp file1 file2 … directory* - to copy files from your current working directory to the directory specified. Here’s mr Shotts’ table with numerous options:

##### mv

mv is the second command of the day. We can use mv to rename a file or directory, or to move a file or directory. We can use it this way:

*mv filename1 filename2* - if we want to rename filename1 to filename2

or

*mv file directory* - if we want to move file to directory.

Here’s a table of few examples of mv with options used with it:

![Screenshot - 08. 07. 2014 - 13:35:15](/wp-content/uploads/2014/07/Screenshot-08.-07.-2014-133515.png)

rm

![Screenshot - 08. 07. 2014 - 13:35:15](/wp-content/uploads/2014/07/Screenshot-08.-07.-2014-133515.png)

The rm command removes/deletes files and directories. Usage is pretty straightforward:

*rm file*

or

*rm -r directory*

And here’s also a table with some additional options:

But, do be careful when using rm as there is no undelete option, so be extra careful not to inflict unwanted damage to your system!

##### mkdir

mkdir is used for creating directories. It’s the most simple command of the day. Simply:

*mkdir directory*

Voila, directory created!

So this is it for this week, geekos. Hope to see you next tuesday! All the best and kind regards,

**Categories:**
[
Distribution
](/category/distribution)

**Tags:**