<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rajaseelan.com &#187; generate</title>
	<atom:link href="http://rajaseelan.com/tag/generate/feed/" rel="self" type="application/rss+xml" />
	<link>http://rajaseelan.com</link>
	<description>junk food for the brain ...</description>
	<lastBuildDate>Tue, 28 Jul 2009 16:37:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generate Files with Random Content and Size in Bash</title>
		<link>http://rajaseelan.com/2009/07/29/generate-files-with-random-content-and-size-in-bash/</link>
		<comments>http://rajaseelan.com/2009/07/29/generate-files-with-random-content-and-size-in-bash/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 16:37:17 +0000</pubDate>
		<dc:creator>raja</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[dd]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://rajaseelan.com/?p=244</guid>
		<description><![CDATA[Occasionally you need to generate a bunch of random files with random content, usually for testing compression, user quotas or miscellaneous stuff.
Here&#8217;s one way, using the bash shell and a few handy linux utilities.

The bash $RANDOM function. It generates a random number between 0 &#8211; 32767.
Linux DD utility, to output files.
/dev/(h&#124;s)da, your hard drive in [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally you need to generate a bunch of random files with random content, usually for testing compression, user quotas or miscellaneous stuff.</p>
<p>Here&#8217;s one way, using the bash shell and a few handy linux utilities.</p>
<ol>
<li>The bash <code>$RANDOM</code> function. It generates a random number between 0 &#8211; 32767.</li>
<li>Linux <code>DD</code> utility, to output files.</li>
<li><code>/dev/(h|s)da</code>, your hard drive in linux.</li>
<li>All wrapped in a bash <code>while</code> loop.</li>
</ol>
<p>So lets start. First define a bash variable with the number of files we wish to create, lets say 10.</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #007800;">no_of_files</span>=<span style="color: #000000;">10</span></pre>
</div>
</div>
<p>Then we&#8217;ll assign the a bash variable for the counter.</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #007800;">counter</span>=<span style="color: #000000;">1</span></pre>
</div>
</div>
<p>As for the <code>dd</code> command, this creates a file with random content from your hard disk. (Mine&#8217;s <code>/dev/sda</code>)  which is 1KB in size. The <code>count</code> switch tells dd to repeat  1024 bytes 1 time, thus the 1Kb file size. <code>skip</code> makes <code>dd</code> skip an <em>x</em> amount of bytes before reading further. Since this requires raw access to your hard drive, you&#8217;ll have to run this as root unfortunately. <img src='http://rajaseelan.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<div class="wp_syntax">
<div class="code">
<pre class="text" style="font-family:monospace;">dd bs=1024 count=1 skip=0 if=/dev/sda of=random-file</pre>
</div>
</div>
<p>So now imagine, if we let bash assign the count and skip random numbers, we get random file contents. (Light bulbs flashing eh?)</p>
<p>Of course, all of it will be written to a single file called random-file in my case. To add just slight amount of variety, we can add the counter variable that we will use in our <code>while</code> loop as an extension. The <code>dd</code> command will now be:-</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">dd</span> <span style="color: #007800;">bs</span>=<span style="color: #000000;">1024</span> <span style="color: #007800;">count</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$RANDOM</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">skip</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$RANDOM</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;"><span style="color: #000000; font-weight: bold;">if</span></span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda <span style="color: #007800;">of</span>=random-file.<span style="color: #007800;">$counter</span></pre>
</div>
</div>
<p>Finally, we will wrap it up in a bash while loop like this:-</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family:monospace;"><span style="color: #007800;">no_of_files</span>=<span style="color: #000000;">10</span>;
<span style="color: #007800;">counter</span>=<span style="color: #000000;">1</span>;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$counter</span> <span style="color: #660033;">-le</span> <span style="color: #007800;">$no_of_files</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>;
 <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> Creating <span style="color: #c20cb9; font-weight: bold;">file</span> no <span style="color: #007800;">$counter</span>;
  <span style="color: #c20cb9; font-weight: bold;">dd</span> <span style="color: #007800;">bs</span>=<span style="color: #000000;">1024</span> <span style="color: #007800;">count</span>=<span style="color: #007800;">$RANDOM</span> <span style="color: #007800;">skip</span>=<span style="color: #007800;">$RANDOM</span> <span style="color: #007800;"><span style="color: #000000; font-weight: bold;">if</span></span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda <span style="color: #007800;">of</span>=random-file.<span style="color: #007800;">$counter</span>;
  <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #ff0000;">&quot;counter += 1&quot;</span>;
 <span style="color: #000000; font-weight: bold;">done</span></pre>
</div>
</div>
<p>When you run it, you will get output like this:-</p>
<div class="wp_syntax">
<div class="code">
<pre class="text" style="font-family:monospace;">Creating file no 1
16614+0 records in
16614+0 records out
17012736 bytes (17 MB) copied, 0.29308 s, 58.0 MB/s
Creating file no 2
14456+0 records in
14456+0 records out
14802944 bytes (15 MB) copied, 0.100101 s, 148 MB/s
.................
Creating file no 10
25224+0 records in
25224+0 records out
25829376 bytes (26 MB) copied, 0.492113 s, 52.5 MB/s</pre>
</div>
</div>
<p>when you do a directory listing, you&#8217;ll see this:-</p>
<div class="wp_syntax">
<div class="code">
<pre class="text" style="font-family:monospace;">[root@atreides rd-test]# ls -lh
total 226M
-rw-r--r-- 1 root root 17M 2009-07-29 00:25 random-file.1
-rw-r--r-- 1 root root 25M 2009-07-29 00:25 random-file.10
-rw-r--r-- 1 root root 15M 2009-07-29 00:25 random-file.2
-rw-r--r-- 1 root root 20M 2009-07-29 00:25 random-file.3
-rw-r--r-- 1 root root 21M 2009-07-29 00:25 random-file.4
-rw-r--r-- 1 root root 30M 2009-07-29 00:25 random-file.5
-rw-r--r-- 1 root root 22M 2009-07-29 00:25 random-file.6
-rw-r--r-- 1 root root 27M 2009-07-29 00:25 random-file.7
-rw-r--r-- 1 root root 25M 2009-07-29 00:25 random-file.8
-rw-r--r-- 1 root root 29M 2009-07-29 00:25 random-file.9</pre>
</div>
</div>
<p>For more info, refer to the $RANDOM function from the <a target="blank" href="http://tldp.org/LDP/abs/html/randomvar.html">Advanced Bash Scripting Guide.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rajaseelan.com/2009/07/29/generate-files-with-random-content-and-size-in-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
