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

81 lines
3.5 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="dd">dd</h1>
<p>复制文件并对原文件的内容进行转换和格式化处理</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>dd命令</strong> 用于复制文件并对原文件的内容进行转换和格式化处理。dd命令功能很强大的对于一些比较底层的问题使用dd命令往往可以得到出人意料的效果。用的比较多的还是用dd来备份裸设备。但是不推荐如果需要备份oracle裸设备可以使用rman备份或使用第三方软件备份使用dd的话管理起来不太方便。</p>
<p>建议在有需要的时候使用dd 对物理磁盘操作如果是文件系统的话还是使用tar backup cpio等其他命令更加方便。另外使用dd对磁盘操作时最好使用块设备文件。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">dd(选项)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">bs=&lt;字节数&gt;将ibs输入与obs输出设成指定的字节数
cbs=&lt;字节数&gt;:转换时,每次只转换指定的字节数;
conv=&lt;关键字&gt;:指定文件转换的方式;
count=&lt;区块数&gt;:仅读取指定的区块数;
ibs=&lt;字节数&gt;:每次读取的字节数;
obs=&lt;字节数&gt;:每次输出的字节数;
of=&lt;文件&gt;:输出到文件;
seek=&lt;区块数&gt;:一开始输出时,跳过指定的区块数;
skip=&lt;区块数&gt;:一开始读取时,跳过指定的区块数;
--help帮助
--version显示版本信息。</code></pre>
<h3 id="实例">实例</h3>
<pre><code class="language-bash">[root@localhost text]# dd if=/dev/zero of=sun.txt bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.006107 seconds, 172 MB/s
[root@localhost text]# du -sh sun.txt
1.1M sun.txt</code></pre>
<p>该命令创建了一个1M大小的文件sun.txt其中参数解释</p>
<ul>
<li><strong>if</strong> 代表输入文件。如果不指定if默认就会从stdin中读取输入。</li>
<li><strong>of</strong> 代表输出文件。如果不指定of默认就会将stdout作为默认输出。</li>
<li><strong>bs</strong> 代表字节为单位的块大小。</li>
<li><strong>count</strong> 代表被复制的块数。</li>
<li><strong>/dev/zero</strong> 是一个字符设备会不断返回0值字节\0</li>
</ul>
<p>块大小可以使用的计量单位表</p>
<table>
<thead>
<tr class="header">
<th>单元大小</th>
<th>代码</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>字节1B</td>
<td>c</td>
</tr>
<tr class="even">
<td>字节2B</td>
<td>w</td>
</tr>
<tr class="odd">
<td>512B</td>
<td>b</td>
</tr>
<tr class="even">
<td>千字节1024B</td>
<td>k</td>
</tr>
<tr class="odd">
<td>兆字节1024KB</td>
<td>M</td>
</tr>
<tr class="even">
<td>吉字节1024MB</td>
<td>G</td>
</tr>
</tbody>
</table>
<p>以上命令可以看出dd命令来测试内存操作速度</p>
<pre><code class="language-bash">1048576 bytes (1.0 MB) copied, 0.006107 seconds, 172 MB/s</code></pre>
<p><strong>生成随机字符串</strong></p>
<p>我们甚至可以使用 /dev/urandom 设备配合 dd 命令 来获取随机字符串。</p>
<pre><code class="language-bash">[root@localhost ~]# dd if=/dev/urandom bs=1 count=15|base64 -w 0
15+0 records in
15+0 records out
15 bytes (15 B) copied, 0.000111993 s, 134 kB/s
wFRAnlkXeBXmWs1MyGEs</code></pre>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->