-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinux技巧——用dd生成指定大小的文件.html
276 lines (262 loc) · 17.9 KB
/
Linux技巧——用dd生成指定大小的文件.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="littlewhite" />
<meta name="copyright" content="littlewhite" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary">
<meta name="keywords" content="linux, skill, " />
<meta property="og:title" content="Linux技巧——用dd生成指定大小的文件 "/>
<meta property="og:url" content="https://chukeer.github.io/Linux技巧——用dd生成指定大小的文件.html" />
<meta property="og:description" content="我们在测试或调试的时候,有时候会需要生成某个size的文件,比如在测试存储系统时,需要将磁盘剩余空间减少5G,最简单的办法就是拷贝一个5G的文件过来,但是从哪儿去弄这样大小的文件呢,或许你想到随便找一个文件,不停的拷贝,最后合并,这也不失为一种办法,但是有了dd,你会更容易且更灵活的实现 我们来case by case的介绍dd的用法。先看第一个 生成一个大小为5G的文件,内容不做要求 命令如下 $ dd if=/dev/zero of=tmp.5G bs=1G count=5 解释一下这里用到的参数 if=FILE : 指定输入文件,若不指定则从标注输入读取。这里指定为/dev/zero是Linux的一个伪文件,它可以产生连续不断的null流(二进制的0) of=FILE : 指定输出文件,若不指定则输出到标准输出 bs=BYTES : 每次读写的字节数,可以使用单位K、M、G等等 ..." />
<meta property="og:site_name" content="楚客" />
<meta property="og:article:author" content="littlewhite" />
<meta property="og:article:published_time" content="2014-12-07T00:00:00+08:00" />
<meta property="" content="2014-12-07T00:00:00+08:00" />
<meta name="twitter:title" content="Linux技巧——用dd生成指定大小的文件 ">
<meta name="twitter:description" content="我们在测试或调试的时候,有时候会需要生成某个size的文件,比如在测试存储系统时,需要将磁盘剩余空间减少5G,最简单的办法就是拷贝一个5G的文件过来,但是从哪儿去弄这样大小的文件呢,或许你想到随便找一个文件,不停的拷贝,最后合并,这也不失为一种办法,但是有了dd,你会更容易且更灵活的实现 我们来case by case的介绍dd的用法。先看第一个 生成一个大小为5G的文件,内容不做要求 命令如下 $ dd if=/dev/zero of=tmp.5G bs=1G count=5 解释一下这里用到的参数 if=FILE : 指定输入文件,若不指定则从标注输入读取。这里指定为/dev/zero是Linux的一个伪文件,它可以产生连续不断的null流(二进制的0) of=FILE : 指定输出文件,若不指定则输出到标准输出 bs=BYTES : 每次读写的字节数,可以使用单位K、M、G等等 ...">
<title>Linux技巧——用dd生成指定大小的文件 · 楚客
</title>
<!--
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
--!>
<link href="https://chukeer.github.io/theme/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="https://chukeer.github.io/theme/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/pygments.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/tipuesearch/tipuesearch.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/elegant.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/custom.css" media="screen">
</head>
<body>
<div id="content-sans-footer">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="https://chukeer.github.io/"><span class=site-name>楚客</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li ><a href="https://chukeer.github.io">Home</a></li>
<li ><a href="https://chukeer.github.io/categories.html">Categories</a></li>
<li ><a href="https://chukeer.github.io/tags.html">Tags</a></li>
<li ><a href="https://chukeer.github.io/archives.html">Archives</a></li>
<li><form class="navbar-search" action="https://chukeer.github.io/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article>
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="https://chukeer.github.io/Linux技巧——用dd生成指定大小的文件.html"> Linux技巧——用dd生成指定大小的文件 </a></h1>
</header>
</div>
<div class="row-fluid">
<div class="span2 table-of-content">
<nav>
<h4>Contents</h4>
<div class="toc">
<ul></ul>
</div>
</nav>
</div>
<div class="span8 article-content">
<p>我们在测试或调试的时候,有时候会需要生成某个size的文件,比如在测试存储系统时,需要将磁盘剩余空间减少5G,最简单的办法就是拷贝一个5G的文件过来,但是从哪儿去弄这样大小的文件呢,或许你想到随便找一个文件,不停的拷贝,最后合并,这也不失为一种办法,但是有了dd,你会更容易且更灵活的实现</p>
<p>我们来case by case的介绍dd的用法。先看第一个</p>
<blockquote>
<p>生成一个大小为5G的文件,内容不做要求</p>
</blockquote>
<p>命令如下</p>
<div class="highlight"><pre><span></span>$ dd <span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span>tmp.5G <span class="nv">bs</span><span class="o">=</span>1G <span class="nv">count</span><span class="o">=</span>5
</pre></div>
<p>解释一下这里用到的参数</p>
<div class="highlight"><pre><span></span>if=FILE : 指定输入文件,若不指定则从标注输入读取。这里指定为/dev/zero是Linux的一个伪文件,它可以产生连续不断的null流(二进制的0)
of=FILE : 指定输出文件,若不指定则输出到标准输出
bs=BYTES : 每次读写的字节数,可以使用单位K、M、G等等。另外输入输出可以分别用ibs、obs指定,若使用bs,则表示是ibs和obs都是用该参数
count=BLOCKS : 读取的block数,block的大小由ibs指定(只针对输入参数)
</pre></div>
<p>这样上面生成5G文件的命令就很好理解了,即从/dev/zero每次读取1G数据,读5次,写入tmp.5G这个文件</p>
<p>再看下面一个问题</p>
<blockquote>
<p>将file.in的前1M追加到file.out的末尾</p>
</blockquote>
<p>命令如下</p>
<div class="highlight"><pre><span></span>$ <span class="nv">file_out_size</span><span class="o">=</span><span class="sb">`</span>du -b file.out <span class="p">|</span> awk <span class="s1">'{print $1}'</span><span class="sb">`</span>
$ dd <span class="k">if</span><span class="o">=</span>./file.in <span class="nv">ibs</span><span class="o">=</span>1M <span class="nv">count</span><span class="o">=</span><span class="m">1</span> <span class="nv">of</span><span class="o">=</span>./file.out <span class="nv">seek</span><span class="o">=</span><span class="m">1</span> <span class="nv">obs</span><span class="o">=</span><span class="nv">$file_out_size</span>
</pre></div>
<p>这里ibs和obs设置为了不同的值,和前面的命令相比,只多了一个seek参数</p>
<div class="highlight"><pre><span></span>seek=BLOCKS : 在拷贝数据之前,从输出文件开头跳过BLOCKS个block,block的大小由obs指定
</pre></div>
<p>命令的意思就是从file.in读取1个1M的数据块写入file.out,不过写入位置并不在file.out的开头,而是在1*$file_out_size字节偏移处(也就是文件末尾)</p>
<p>在此基础上再增加一个要求</p>
<blockquote>
<p>将file.in的第3M追加到file.out的末尾</p>
</blockquote>
<div class="highlight"><pre><span></span>$ <span class="nv">file_out_size</span><span class="o">=</span><span class="sb">`</span>du -b file.out <span class="p">|</span> awk <span class="s1">'{print $1}'</span><span class="sb">`</span>
$ dd <span class="k">if</span><span class="o">=</span>./file.in <span class="nv">skip</span><span class="o">=</span><span class="m">2</span> <span class="nv">ibs</span><span class="o">=</span>1M <span class="nv">count</span><span class="o">=</span><span class="m">1</span> <span class="nv">of</span><span class="o">=</span>./file.out <span class="nv">seek</span><span class="o">=</span><span class="m">1</span> <span class="nv">obs</span><span class="o">=</span><span class="nv">$file_out_size</span>
</pre></div>
<p>这里多了一个参数skip</p>
<div class="highlight"><pre><span></span>skip=BLOCKS : 拷贝数据前,从输入文件跳过BLOCKS个block,block的大小由ibs指定。这个参数和seek是对应的
</pre></div>
<p>上面命令的意思就是,从文件file.in开始跳过2<em>1M,拷贝1</em>1M数据,写入文件file.out的1*$file_out_size偏移处</p>
<p>这样基本的参数都介绍全了,无非就是设置输入输出文件以及各自的偏移,设置读写数据块大小和读取数据块个数,下面总结一下</p>
<div class="highlight"><pre><span></span>输入参数:
if
skip
ibs
count
输出参数:
of
seek
obs
</pre></div>
<p>最后来一道终极题。前面创建的都是null流,这次换一个</p>
<blockquote>
<p>指定某个字符,创建一个全是这个字符的指定大小的文件。比如创建一个文件,大小为123456字节,每个字节都是字符A</p>
</blockquote>
<p>这问题看似没什么意义,但有时候确实需要用到。比如我通过/dev/zero创建了一个1G的文件,但是出于测试需求我想修改中间100M数据,这时我需要创建一个100M的文件,将该文件写入到那个1G文件的指定位置,而这个100M的文件是不能从/dev/zero创建的,否则达不到修改的目的,这时候就需要这样的功能了</p>
<p>话不多说,直接上脚本,有了前面的基础,相信都能看得懂</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="ch">#!/bin/bash</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$#</span> -ne <span class="m">3</span> <span class="o">]</span><span class="p">;</span><span class="k">then</span>
<span class="nb">echo</span> <span class="s2">"usage : </span><span class="nv">$0</span><span class="s2"> character out_file file_size(Byte)"</span>
<span class="nb">exit</span> 1
<span class="k">fi</span>
<span class="nb">echo</span> <span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span> <span class="p">|</span> grep -q <span class="s2">"^[a-zA-Z]</span>$<span class="s2">"</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$?</span> -ne <span class="m">0</span> <span class="o">]</span><span class="p">;</span><span class="k">then</span>
<span class="nb">echo</span> <span class="s2">"arg1 must be character"</span>
<span class="nb">exit</span> 1
<span class="k">fi</span>
<span class="nv">character</span><span class="o">=</span><span class="nv">$1</span>
<span class="nv">out_file</span><span class="o">=</span><span class="nv">$2</span>
<span class="nv">target_size</span><span class="o">=</span><span class="nv">$3</span>
<span class="c1"># echo输出默认是带'\n'字符的,所以需要通过dd指定输入字节数</span>
<span class="nb">echo</span> <span class="s2">"</span><span class="nv">$character</span><span class="s2">"</span> <span class="p">|</span> dd <span class="nv">of</span><span class="o">=</span><span class="nv">$out_file</span> <span class="nv">ibs</span><span class="o">=</span><span class="m">1</span> <span class="nv">count</span><span class="o">=</span>1
<span class="k">while</span> <span class="nb">true</span>
<span class="k">do</span>
<span class="nv">cur_size</span><span class="o">=</span><span class="sb">`</span>du -b <span class="nv">$out_file</span> <span class="p">|</span> awk <span class="s1">'{print $1}'</span><span class="sb">`</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$cur_size</span> -ge <span class="nv">$target_size</span> <span class="o">]</span><span class="p">;</span><span class="k">then</span>
<span class="nb">break</span>
<span class="k">fi</span>
<span class="nv">remain_size</span><span class="o">=</span><span class="k">$((</span>target_size-<span class="nv">$cur_size</span><span class="k">))</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$remain_size</span> -ge <span class="nv">$cur_size</span> <span class="o">]</span><span class="p">;</span><span class="k">then</span>
<span class="nv">input_size</span><span class="o">=</span><span class="nv">$cur_size</span>
<span class="k">else</span>
<span class="nv">input_size</span><span class="o">=</span><span class="nv">$remain_size</span>
<span class="k">fi</span>
dd <span class="k">if</span><span class="o">=</span><span class="nv">$out_file</span> <span class="nv">ibs</span><span class="o">=</span><span class="nv">$input_size</span> <span class="nv">count</span><span class="o">=</span><span class="m">1</span> <span class="nv">of</span><span class="o">=</span><span class="nv">$out_file</span> <span class="nv">seek</span><span class="o">=</span><span class="m">1</span> <span class="nv">obs</span><span class="o">=</span><span class="nv">$cur_size</span> <span class="o">||</span> <span class="nb">exit</span> 1
<span class="k">done</span>
</pre></div>
</td></tr></table>
<p>有了这些技巧,在对文件内容无要求的前提下,你就可以任意创建指定大小的文件,任意修改文件指定字节数,这会让某些测试场合变得非常方便</p>
<hr/>
<aside>
<nav>
<ul class="articles-timeline">
<li class="previous-article">« <a href="https://chukeer.github.io/如何让Magic Mouse真正拥有Magic.html" title="Previous: 如何让Magic Mouse真正拥有Magic">如何让Magic Mouse真正拥有Magic</a></li>
<li class="next-article"><a href="https://chukeer.github.io/shell命令技巧——文本去重并保持原有顺序.html" title="Next: shell命令技巧——文本去重并保持原有顺序">shell命令技巧——文本去重并保持原有顺序</a> »</li>
</ul>
</nav>
</aside>
</div>
<section>
<div class="span2" style="float:right;font-size:0.9em;">
<table class="table">
<!-- <time pubdate="pubdate" datetime="2014-12-07T00:00:00+08:00">12 7, 2014</time> -->
<tr>
<td>Published</td>
<td><time pubdate="pubdate" datetime="2014-12-07T00:00:00+08:00">2014-12-7</time></td>
</tr>
<tr>
<td>Category</td>
<td><a class="category-link" href="https://chukeer.github.io/categories.html#skill-ref">skill</a></td>
</tr>
<tr>
<td>Tags</td>
<td>
<ul class="list-of-tags tags-in-article">
<li><a href="https://chukeer.github.io/tags.html#linux-ref">linux
<span>7</span>
</a></li>
</ul>
</td>
</tr>
</table>
</div>
</section>
</div>
</article>
</div>
<div class="span1"></div>
</div>
</div>
<div id="push"></div>
</div>
<footer>
<div id="footer">
<ul class="footer-content">
<li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Talha Mansoor</a></li>
</ul>
</div>
</footer> <!--
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
-->
<script src="https://chukeer.github.io/theme/js/jquery.min.js"></script>
<script src="https://chukeer.github.io/theme/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
<script>
$("div.article-content table").addClass("table table-hover");
</script>
</body>
<!-- Theme: Elegant built for Pelican
License : http://oncrashreboot.com/pelican-elegant -->
</html>