2019-04-21 11:50:48 +08:00

54 lines
4.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="mv">mv</h1>
<p>用来对文件或目录重新命名</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>mv命令</strong> 用来对文件或目录重新命名或者将文件从一个目录移到另一个目录中。source表示源文件或目录target表示目标文件或目录。如果将一个文件移到一个已经存在的目标文件中则目标文件的内容将被覆盖。</p>
<p>mv命令可以用来将源文件移至一个目标文件中或将一组文件移至一个目标目录中。源文件被移至目标文件有两种不同的结果</p>
<ol type="1">
<li>如果目标文件是到某一目录文件的路径,源文件会被移到此目录下,且文件名不变。</li>
<li>如果目标文件不是目录文件则源文件名只能有一个会变为此目标文件名并覆盖己存在的同名文件。如果源文件和目标文件在同一个目录下mv的作用就是改文件名。当目标文件是目录文件时源文件或目录参数可以有多个则所有的源文件都会被移至目标文件中。所有移到该目录下的文件都将保留以前的文件名。</li>
</ol>
<p>注意事项mv与cp的结果不同mv好像文件“搬家”文件个数并未增加。而cp对文件进行复制文件个数增加了。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">mv(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">--backup=&lt;备份模式&gt;:若需覆盖文件,则覆盖前先行备份;
-b当文件存在时覆盖前为其创建一个备份
-f若目标文件或目录与现有的文件或目录重复则直接覆盖现有的文件或目录
-i交互式操作覆盖前先行询问用户如果源文件与目标文件或目标目录中的文件同名则询问用户是否覆盖目标文件。用户输入”y”表示将覆盖目标文件输入”n”表示取消对源文件的移动。这样可以避免误将文件覆盖。
--strip-trailing-slashes删除源文件中的斜杠“/”;
-S&lt;后缀&gt;:为备份文件指定后缀,而不使用默认的后缀;
--target-directory=&lt;目录&gt;:指定源文件要移动到目标目录;
-u当源文件比目标文件新或者目标文件不存在时才执行移动操作。</code></pre>
<h3 id="参数">参数</h3>
<ul>
<li>源文件:源文件列表。</li>
<li>目标文件:如果“目标文件”是文件名则在移动文件的同时,将其改名为“目标文件”;如果“目标文件”是目录名则将源文件移动到“目标文件”下。</li>
</ul>
<h3 id="实例">实例</h3>
<p>将目录<code>/usr/men</code>中的所有文件移到当前目录(用<code>.</code>表示)中:</p>
<pre><code class="language-bash">mv /usr/men/* .</code></pre>
<p>移动文件</p>
<pre><code class="language-bash">mv file_1.txt /home/office/</code></pre>
<p>移动多个文件</p>
<pre><code class="language-bash">mv file_2.txt file_3.txt file_4.txt /home/office/
mv *.txt /home/office/</code></pre>
<p>移动目录</p>
<pre><code class="language-bash">mv directory_1/ /home/office/</code></pre>
<p>重命名文件或目录</p>
<div class="sourceCode" id="cb7"><pre><code class="language-bash"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="fu">mv</span> file_1.txt file_2.txt <span class="co"># 将文件file_1.txt改名为file_2.txt</span></a></code></pre></div>
<p>重命名目录</p>
<pre><code class="language-bash">mv directory_1/ directory_2/</code></pre>
<p>打印移动信息</p>
<div class="sourceCode" id="cb9"><pre><code class="language-bash"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="fu">mv</span> -v *.txt /home/office</a></code></pre></div>
<p>提示是否覆盖文件</p>
<pre><code class="language-bash">mv -i file_1.txt /home/office</code></pre>
<p>源文件比目标文件新时才执行更新</p>
<pre><code class="language-bash">mv -uv *.txt /home/office</code></pre>
<p>不要覆盖任何已存在的文件</p>
<pre><code class="language-bash">mv -vn *.txt /home/office</code></pre>
<p>复制时创建备份</p>
<pre><code class="language-bash">mv -bv *.txt /home/office</code></pre>
<p>无条件覆盖已经存在的文件</p>
<pre><code class="language-bash">mv -f *.txt /home/office</code></pre>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->