想要变瘦变美 發表於 2024-2-26 00:00:00

Linux移动文件与目录-mv命令的10个实用例子

<p>
        <span><strong>mv 命令</strong></span></p>
<p>
        mv 命令是一个与cp类似的命令,但是它并非创建文件或目录的复制品/副本。不管你在使用什么版本的linux系统,mv 都默认安装在你的linux系统上了。</p>
<p>
        <span><strong>来看一下 mv 命令在日常操作中的一些例子。</strong></span></p>
<p>
        <strong>1.移动文件</strong></p>
<p>
        移动文件时需要注意的是文件的源地址和目标地址必须不同。这里有个例子,想要将file_1.txt文件从当前目录移动到其它目录,以/home/pungki/为例,语法应该如下:</p>
<div>
        <div>
                <div id="highlighter_930085">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>file_1.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/9c70fa4d25976323aa75237a8ac030c2.jpg"></p>
<p>
        如我们所见,当我们移动 file_1.txt 文件时,先前目录的 file_1.txt 就被删除了。</p>
<p>
        <strong>2.移动多个文件</strong></p>
<p>
        如果想一次移动多个文件,我们可以将他们放在一行并用空格分开。</p>
<div>
        <div>
                <div id="highlighter_649759">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>file_2.txt file_3.txt file_4.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/760717bf638a26c53ce14e8ea99a2cd8.jpg"></p>
<p>
        如果你的文件有规律可循的话那么你就可以使用通配符。比如,为了移除所有以.txt为扩展名的文件,我们可以用下面的命令:</p>
<div>
        <div>
                <div id="highlighter_469231">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>*.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/7f862e95de2213623fb6231423d96195.jpg"></p>
<p>
        <strong>3.移动目录</strong></p>
<p>
        不同于复制命令,用 mv 命令移动目录相当直接。移动目录你可以使用不带选项的 mv 命令。看下面的截图就一目了然了。</p>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/162eea688900317b88ccc4af0243029b.jpg"></p>
<p>
        <strong>4.重命名文件或目录</strong></p>
<p>
        我们也用 mv 命令来重命名文件或目录。不过目标位置和源位置必须相同才可以。然后文件名必须不同。</p>
<p>
        假定我们当前所在目录为/home/pungki/documents,而我们想将file_1.txt重命名为file_2.txt。那么命令应该如下:</p>
<div>
        <div>
                <div id="highlighter_450322">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>file_1.txt file_2.txt</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        如果是绝对路径,它应该像下面这样:</p>
<div>
        <div>
                <div id="highlighter_727426">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>/home/pungki/documents/file_1</code><code>.txt </code><code>/home/pungki/documents/file_2</code><code>.txt</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/67676a8adb0120081710dad8a6ed5955.jpg"></p>
<p>
        <strong>5. 重命名目录</strong></p>
<p>
        上一段的规则同样适用于目录。请看这个例子:</p>
<div>
        <div>
                <div id="highlighter_901221">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>directory_1/ directory_2/</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/d3b8be236fe298b11c8c8644fdb85e7c.jpg"></p>
<p>
        <strong>6. 打印移动信息</strong></p>
<p>
        当你移动或重命名一大堆文件或目录时,你可能会想在不去目标位置去查看的情况下知道你自己的命令是否成功地执行了。这就要用到-v选项了。</p>
<div>
        <div>
                <div id="highlighter_143416">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>-</code><code>v</code> <code>*.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        </p>
<p>
        该方法同样适用于目录。</p>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/7ac0558172326f23ade1bf0d13bf672c.jpg"></p>
<p>
        <strong>7. 使用交互模式</strong></p>
<p>
        当你将文件移动到其它位置,而那个位置恰好有同样的文件,这时 mv 命令会覆盖掉原来的文件。对于mv的这一行为一般不会有什么提示。如果想产生一个关于覆盖文件的提示,我们可以使用-i选项。(译注:通常发行版会通过alias命令,将-i作为默认选项,所以会有提示。)</p>
<p>
        假设我们想将 file_1.txt 移动到 /home/pungki/office。同时,/home/pungki/office 目录下已经有file_1.txt文件了。</p>
<div>
        <div>
                <div id="highlighter_136919">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>-i file_1.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/232af71427f0977589884772d889a148.jpg"></p>
<p>
        这个提示会让我们知道目标位置处file_1.txt的存在。如果我们按y键,那么那个文件将会被删除,否则不会。</p>
<p>
        <strong>8. 使用更新选项</strong></p>
<p>
        -i 选项会提示我们关于覆盖文件的提示,而 -u 则只在源文件比目标文件新时才执行更新。让我们看一看下面的例子:</p>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/ce391098dfdea5d98334a30527e408b1.jpg"></p>
<p>
        假如 file_1.txt 和 file_2.txt有如下特点:</p>
<div>
        <div>
                <div id="highlighter_298530">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                        <div>
                                                                2</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>file_1.txt has 84 bytes </code><code>file</code> <code>size and it last modified </code><code>time</code> <code>is 12:00</code>
</div>
                                                                <div>
                                                                        <code>file_2.txt has 0 bytes </code><code>file</code> <code>size and it last modified </code><code>time</code> <code>is 11:59</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        我们想将它们移动到 /home/pungki/office 目录下。但是目标地址已经有file_1.txt和file_2.txt了。</p>
<p>
        我们用下面的命令将file_1.txt 和file_2.txt从当前目录移动到/home/pungki/office</p>
<div>
        <div>
                <div id="highlighter_240695">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>-uv *.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        可以看到这些文件被移动了。能移动这些文件是因为它们最近的修改时间戳比 /home/pungki/office 目录中的文件新。</p>
<p>
        <strong>9.不要覆盖任何已存在的文件</strong></p>
<p>
        如果-i选项询问我们是否要覆盖文件,那么 -n 选项将不会允许我们覆盖任何已存在的文件。</p>
<p>
        继续使用第8点中的例子,如果我们将-u 换成 -n同时加上-v选项,那么我们会看到没有任何文件移动到了 /home/pungki/office 目录下。</p>
<div>
        <div>
                <div id="highlighter_77396">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>-vn *.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/6443c527abb01dac08f0237e64ede88f.jpg"></p>
<p>
        <strong>10. 复制时创建备份</strong></p>
<p>
        默认情况下,移动文件将会覆盖已存在的目标文件。但是如果我们移动错了文件而目标文件已经被新的文件覆盖了,这时应该怎么办才好呢?有没有一种方法可以恢复之前的文件呢?答案是肯定的。我们可以用-b选项。该选项会在新文件覆盖旧文件时将旧文件做备份。这里我们还以第8点为例。</p>
<div>
        <div>
                <div id="highlighter_151961">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>-bv *.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <img title="Linux移动文件与目录-mv命令的10个实用例子" alt="Linux移动文件与目录-mv命令的10个实用例子" src="https://zhuji.jb51.net/uploads/img/20230519/5a2417ef4e25bee3a5617adf1557450f.jpg"></p>
<p>
        如截图中所见,在 /home/pungki/office 目录下出现了名为file_1.txt~ 和 file_2.txt~ 的文件。那个波浪符号(~)意味着这些文件是备份文件。从它们的属性中我们可以看到,这些文件比file_1.txt和file_2.txt要旧。</p>
<p>
        <strong>11. 无条件覆盖已经存在的文件</strong></p>
<p>
        (译注:这一节是译者补充的,原文遗漏了这个重要选项)</p>
<p>
        当你希望无论如何都覆盖已经存在的文件或目录时,你可以使用 -f 选项。如果同时指定了 -f 选项和 -i 或 -n 选项,则 -f 选项会覆盖它们——即不进行任何提示而覆盖,所以,在使用此参数时,知道你在做什么。</p>
<div>
        <div>
                <div id="highlighter_6933">
                        <div>
                                <span>?</span>
</div>
                        <table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
                                                        <div>
                                                                1</div>
                                                </td>
                                                <td>
                                                        <div>
                                                                <div>
                                                                        <code>$ </code><code>mv</code> <code>-f *.txt </code><code>/home/pungki/office</code>
</div>
                                                        </div>
                                                </td>
                                        </tr></tbody></table>
</div>
        </div>
</div>
<p>
        <span><strong>总结</strong></span></p>
<p>
        移动文件和目录命令是linux系统的基本命令。通常你可以通过man mv 或者 mv --help显示mv的手册页以了解更多详细信息。以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。</p>
<p>
        via: http://linoxide.com/linux-command/mv-command-linux/</p>
<p>
        译者:linchenguang 校对:wxy</p>
<p>
        本文由 lctt 原创翻译,linux中国 荣誉推出</p>
頁: [1]
查看完整版本: Linux移动文件与目录-mv命令的10个实用例子