<?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>The Linux Daily &#187; Tips and Tricks</title>
	<atom:link href="http://www.thelinuxdaily.com/category/tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thelinuxdaily.com</link>
	<description>Tutorials, Guides, Tips, and Tricks from Everyday Experiences</description>
	<lastBuildDate>Tue, 03 Jan 2012 19:54:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Find/Highlight Lines Longer Than 80 or &#8216;n&#8217; Characters in VIM</title>
		<link>http://www.thelinuxdaily.com/2011/11/findhighlight-lines-longer-than-80-or-n-characters-in-vim/</link>
		<comments>http://www.thelinuxdaily.com/2011/11/findhighlight-lines-longer-than-80-or-n-characters-in-vim/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 21:17:02 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2467</guid>
		<description><![CDATA[Here&#8217;s a quickie that will help you find lines longer than 80 characters (or any number of characters) within VIM. Open vim, and copy/paste the following: :/.{80,} You can use the regex anywhere, actually, but this is a good one to store in the useful command list. Or, create an alias for it in your [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quickie that will help you find lines longer than 80 characters (or any number of characters) within VIM.  Open vim, and copy/paste the following:</p>
<p></p>
<p><code>:/.{80,}</code></p>
<p>You can use the regex anywhere, actually, but this is a good one to store in the useful command list.  Or, create an alias for it in your .vimrc file for quick access.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/11/findhighlight-lines-longer-than-80-or-n-characters-in-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Arrays and For Loops</title>
		<link>http://www.thelinuxdaily.com/2011/09/using-arrays-and-for-loops-in-the-shell/</link>
		<comments>http://www.thelinuxdaily.com/2011/09/using-arrays-and-for-loops-in-the-shell/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 13:00:38 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[space]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2455</guid>
		<description><![CDATA[Here&#8217;s how to setup an array and use it in a for loop. Open up the terminal and let&#8217;s get started&#8230; Step 1: Create an array This is easily done by surround space separated items in parenthesis. For example: files=( dir1/file1.php dir2/file2.php dir1/subdir/file3.php ) Note: Be aware of any spaces within the array items. If [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to setup an array and use it in a for loop.  Open up the terminal and let&#8217;s get started&#8230;</p>
<h3><span style="color: #800000;">Step 1: Create an array</span></h3>
<p>This is easily done by surround <strong>space</strong> separated items in parenthesis.  For example:</p>
<pre>files=( dir1/file1.php dir2/file2.php dir1/subdir/file3.php )</pre>
<p>Note: Be aware of any spaces within the array items.  If you have this, you&#8217;ll need to surround it in quotes.  See appendix at the end of this post.</p>
<h3><span style="color: #800000;">Step 2: Figure out what you want to do to each of the items in the array</span></h3>
<p>The next part is figuring out what you want to do to each of the items in the array.  Keep in mind, these items can be variables, filenames, text, numbers, whatever.  For example, I want to see how large each of these files are along with other information.  For this, I would run <code>ls -lh filename.php</code> </p>
<pre>Unix~ ls -lh dir1/file1.php
-rw-r--r-- 1 user group 2.7K May  4 16:47 dir1/file1.php
</pre>
<h3><span style="color: #800000;">Step 3: Iterate through the list of array items</span></h3>
<p>Finally, let&#8217;s perform the action from step 2 on all of the items in our array.  For this, the key is to use ${array[@]}, which says run through all (@ll) of the items in the array called &#8220;array&#8221;.  Going along with the contrived, simple example set in the previous steps, the final command is:</p>
<pre>for file in ${files[@]}; do ls -lh $file; done</pre>
<p>The output from the above command is:</p>
<pre>
Unix~ for file in ${files[@]}; do ls -lh $file; done
-rw-r--r-- 1 user group 2.7K May  4 16:47 dir1/file1.php
-rw-rw-r-- 1 user group 2.8K Aug 25 15:23 dir2/file2.php
-rw-r--r-- 1 user group 2.4K Mar  7 19:57 dir1/subdir/file3.php
</pre>
<h3><span style="color: #800000;">APPENDIX: Working with spaces in array items</span></h3>
<p>If you have spaces within your array items, be sure to surround them with quotes like such:</p>
<pre>test=( one &quot;two 2&quot; &quot;three 3 tree&quot; )</pre>
<p>On top of that, you&#8217;ll need to surround ${test[@]} in quotes like such:</p>
<pre>for i in &quot;${test[@]}&quot;; do echo $i; done</pre>
<p>Here&#8217;s what you&#8217;ll get:</p>
<pre>Unix~ for i in &quot;${test[@]}&quot;; do echo &quot;==&gt; $i&quot;; done
==&gt; one
==&gt; two 2
==&gt; three 3 tree
</pre>
<p>If you don&#8217;t use quotes, you&#8217;ll get this instead (BAD!):</p>
<pre>for i in ${test[@]}; do echo &quot;==&gt; $i&quot;; done
==&gt; one
==&gt; two
==&gt; 2
==&gt; three
==&gt; 3
==&gt; tree
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/09/using-arrays-and-for-loops-in-the-shell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove a File / Directory from a Tarball Without Extracting First</title>
		<link>http://www.thelinuxdaily.com/2011/08/remove-a-file-directory-from-a-tarball-without-extracting-first/</link>
		<comments>http://www.thelinuxdaily.com/2011/08/remove-a-file-directory-from-a-tarball-without-extracting-first/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 13:00:48 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tarball]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2435</guid>
		<description><![CDATA[If you have an extra folder or file within a tarball (.tar) file, you can remove it without extracting the entire tarball first. This can be really handy when you have a massive file that you don&#8217;t want to spend a lot of time extracting and re-archiving. Open up a terminal shell and follow along [...]]]></description>
			<content:encoded><![CDATA[<p>If you have an extra folder or file within a tarball (.tar) file, you can remove it without extracting the entire tarball first.  This can be really handy when you have a massive file that you don&#8217;t want to spend a lot of time extracting and re-archiving.  Open up a terminal shell and follow along with the example below.</p>
<p>We&#8217;ll first see what&#8217;s in the compressed tarball named sandbox.tar.gz by using the <code>--list</code> option of <code>tar</code>:</p>
<pre>
thelinuxdaily$ tar --list --file=sandbox.tar.gz
sandbox/
sandbox/delete_me/
sandbox/delete_me/hello.txt
sandbox/hello.txt
sandbox/hello2.txt
sandbox/hello3.txt
sandbox/save_me/
sandbox/save_me/hello.txt
</pre>
<p>Let&#8217;s try to delete the folder called <em>sandbox/delete_me</em> from the compressed tarball:</p>
<pre>
thelinuxdaily$ tar --delete --file=sandbox.tar.gz sandbox/delete_me
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
</pre>
<p>See what happened?  That means we need to uncompress it.  In this case, it&#8217;s a .gz compression type, so we&#8217;ll use <code>gunzip</code> (if you were using bz2, you&#8217;d use <code>bunzip2</code>:</p>
<pre>
thelinuxdaily$ gunzip sandbox.tar.gz
</pre>
<p>Let&#8217;s try to delete the folder <em>sandbox/delete_me</em> again:</p>
<pre>
thelinuxdaily$ tar --delete --file=sandbox.tar sandbox/delete_me
</pre>
<p>We didn&#8217;t get an error message, so that&#8217;s good.  Let&#8217;s see if it&#8217;s gone by using <code>--list</code> again:</p>
<pre>
thelinuxdaily$ tar --list --file=sandbox.tar
sandbox/
sandbox/hello.txt
sandbox/hello2.txt
sandbox/hello3.txt
sandbox/save_me/
sandbox/save_me/hello.txt
thelinuxdaily$ gzip sandbox.tar
</pre>
<p>Excellent!  That&#8217;s what we needed to see.  If you have any other tips, feel free to use the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/08/remove-a-file-directory-from-a-tarball-without-extracting-first/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Give Normal User Write Access to /var/www</title>
		<link>http://www.thelinuxdaily.com/2011/05/give-normal-user-write-access-to-varwww/</link>
		<comments>http://www.thelinuxdaily.com/2011/05/give-normal-user-write-access-to-varwww/#comments</comments>
		<pubDate>Tue, 17 May 2011 14:44:17 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2308</guid>
		<description><![CDATA[Here&#8217;s a quick way to allow a normal user write access to the /var/www directory so that they can work with web server files without the need to login as root first. Open the terminal and issue the commands: sudo chown -R `id --user` /var/www/ sudo chgrp -R `id --user` /var/www/ If you don&#8217;t have [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick way to allow a normal user write access to the /var/www directory so that they can work with web server files without the need to login as root first.  Open the terminal and issue the commands:</p>
<pre>
sudo chown -R `id --user` /var/www/
sudo chgrp -R `id --user` /var/www/
</pre>
<p>If you don&#8217;t have sudo enabled on your distro, remove &#8216;sudo&#8217; from the commands and login as root first before executing them with &#8216;su&#8217;.</p>
<p>You should now be able to write to all the files within /var/www/.  My only other suggestion is that you can be a bit more selective about what directories and files are allowed write access by removing the recursive option &#8220;-R&#8221; from the command.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/05/give-normal-user-write-access-to-varwww/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Article: Playing Audio with Linux 2.4 Kernel</title>
		<link>http://www.thelinuxdaily.com/2011/04/article-playing-audio-with-linux-2-4-kernel/</link>
		<comments>http://www.thelinuxdaily.com/2011/04/article-playing-audio-with-linux-2-4-kernel/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 13:00:37 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[2.4]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[oss]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=1974</guid>
		<description><![CDATA[The 2.4 kernel seems dead to most, but it has it&#8217;s place in embedded systems. I dealt with a customer a while ago using an embedded system running the 2.4 kernel and he needed to play audio. Here&#8217;s an article that got me through the process: http://tldp.org/HOWTO/Sound-HOWTO/x504.html]]></description>
			<content:encoded><![CDATA[<p>The 2.4 kernel seems dead to most, but it has it&#8217;s place in embedded systems.  I dealt with a customer a while ago using an embedded system running the 2.4 kernel and he needed to play audio.  Here&#8217;s an article that got me through the process: <a href="http://tldp.org/HOWTO/Sound-HOWTO/x504.html">http://tldp.org/HOWTO/Sound-HOWTO/x504.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/04/article-playing-audio-with-linux-2-4-kernel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Quick Usage Example of the Split Command</title>
		<link>http://www.thelinuxdaily.com/2011/03/a-quick-usage-example-of-the-split-command/</link>
		<comments>http://www.thelinuxdaily.com/2011/03/a-quick-usage-example-of-the-split-command/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 13:00:10 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2304</guid>
		<description><![CDATA[Have a file that needs to be split into pieces? Here&#8217;s a quick example of splitting the text file &#8220;test.txt&#8221; containing an arbitrary number of lines into several files each with 10 lines a piece and named &#8220;split_aa, split_ab, split_ac, &#8230;&#8221;. cat test.txt &#124; split -l 10 - split_ If you&#8217;d like to use numeric [...]]]></description>
			<content:encoded><![CDATA[<p>Have a file that needs to be split into pieces?  Here&#8217;s a quick example of splitting the text file &#8220;test.txt&#8221; containing an arbitrary number of lines into several files each with 10 lines a piece and named &#8220;split_aa, split_ab, split_ac, &#8230;&#8221;.</p>
<pre>
cat test.txt | split -l 10 - split_
</pre>
<p>If you&#8217;d like to use numeric suffixes instead of alphabetic, use the &#8216;-d&#8217; option in split.  There&#8217;s more tips and tricks where that came from in the <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?split">split man page</a>, so be sure to check it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/03/a-quick-usage-example-of-the-split-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validate Email Using POSIX Regular Expressions</title>
		<link>http://www.thelinuxdaily.com/2011/02/validate-email-using-posix-regular-expressions/</link>
		<comments>http://www.thelinuxdaily.com/2011/02/validate-email-using-posix-regular-expressions/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 14:00:26 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[posix]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[validate]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2266</guid>
		<description><![CDATA[You can validate an email address using POSIX Regular Expressions like the following examples: ^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$ ^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com&#124;edu&#124;gov]+$ This is two different ways of doing the same thing. If you want tighter control on your validity, I would recommend specifying com, edu, gov or other domain names you consider valid like shown in the second example above. [...]]]></description>
			<content:encoded><![CDATA[<p>You can validate an email address using POSIX Regular Expressions like the following examples:</p>
<pre>
^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com|edu|gov]+$
</pre>
<p>This is two different ways of doing the same thing.  If you want tighter control on your validity, I would recommend specifying com, edu, gov or other domain names you consider valid like shown in the second example above.</p>
<p>A quick PHP script to demonstrate:</p>
<pre>
&lt;?php

// hit this in a browser and specify the email in the URL:
// http://www.mywebsite.com/email_validate.php?email=foo@bar.com

$email=$_GET['email'];

//if (!eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $email)) {
if (!eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com|edu|gov]+$', $email)) {
        echo &quot;&lt;p&gt;That is not a valid email address.&lt;/p&gt;&quot;.
             &quot;&lt;p&gt;Please return to the previous page and try again.&lt;/p&gt;&quot;;
        exit;
}
else
        echo &quot;&lt;p&gt;That is one sexy email address!&lt;/p&gt;&quot;;
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/02/validate-email-using-posix-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cute &#8216;sed&#8217; Tricks to Modify Specific Lines Within File</title>
		<link>http://www.thelinuxdaily.com/2011/02/cute-sed-tricks-to-modify-specific-lines-within-file/</link>
		<comments>http://www.thelinuxdaily.com/2011/02/cute-sed-tricks-to-modify-specific-lines-within-file/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 14:00:20 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[change]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2280</guid>
		<description><![CDATA[Here&#8217;s a cute trick to change or insert a line in a file given a line number using sed. You can pair it with grep to make it even more powerful with pattern matching. #!/bin/sh if [ $# -lt 1 ]; then echo &#34;Usage: $0 &#60;filename&#62;&#34; exit fi # Variables #file=testfile.txt file=$1 # First, change [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a cute trick to change or insert a line in a file given a line number using <code>sed</code>.  You can pair it with <code>grep</code> to make it even more powerful with pattern matching.</p>
<pre>
#!/bin/sh

if [ $# -lt 1 ]; then
   echo &quot;Usage: $0 &lt;filename&gt;&quot;
   exit
fi

# Variables
#file=testfile.txt
file=$1

# First, change line number $linenum, then insert
# two spaces at the beginning of that same line
linenum=1
sed -i &quot;${linenum}c This line was changed&quot; $file
sed -i &quot;${linenum}s/^/   /&quot; $file

# Insert a line at $linenum, then insert text at
# the end of that same line.
linenum=2
sed -i &quot;${linenum}i This line was inserted&quot; $file
sed -i &quot;${linenum}s/$/ end of line/&quot; $file

# Match a particular pattern in the file and insert a line
# directly above the first match (will need massaging for
# multiple lines, perhaps using an array)
linenum=`grep -n pattern $file | head -n1 | cut -d: -f1`
linenum_above=`echo &quot;$linenum - 1&quot; | bc`
sed -i &quot;${linenum_above}i This line was inserted&quot; $file

echo &quot;Resulting File:&quot;
cat $file
</pre>
<p>As usual, take a look at the man pages for <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?sed">sed</a> for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/02/cute-sed-tricks-to-modify-specific-lines-within-file/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Delete Lines Within File Containing Matching Pattern/Text</title>
		<link>http://www.thelinuxdaily.com/2011/01/delete-lines-within-file-containing-matching-patterntext/</link>
		<comments>http://www.thelinuxdaily.com/2011/01/delete-lines-within-file-containing-matching-patterntext/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 14:00:05 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2239</guid>
		<description><![CDATA[Here&#8217;s a quick one-liner command that will remove any lines matching a text patter from a file: sed -i '/pattern/d' filename For example, say I want to remove all lines in the file garbage.txt that contain the text &#8220;foobar&#8221;. Here is my console output. user@localhost $ cat garbage.txt This is a plain text file that [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick one-liner command that will remove any lines matching a text patter from a file:</p>
<pre>
sed -i '/pattern/d' filename
</pre>
<p>For example, say I want to remove all lines in the file garbage.txt that contain the text &#8220;foobar&#8221;.  Here is my console output.</p>
<pre>
user@localhost $ cat garbage.txt
This is a plain text file that
contains lots of text
but most of it is foobar
and it's unnecessary
to include foobar in text.

user@localhost$ sed -i '/foobar/d' garbage.txt
user@localhost$ cat garbage.txt
This is a plain text file that
contains lots of text
and it's unnecessary

user@localhost$
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2011/01/delete-lines-within-file-containing-matching-patterntext/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a &#8220;Fluff&#8221; File Filled with Specified or Random Text</title>
		<link>http://www.thelinuxdaily.com/2010/12/creating-a-fluff-file-filled-with-specified-or-random-text/</link>
		<comments>http://www.thelinuxdaily.com/2010/12/creating-a-fluff-file-filled-with-specified-or-random-text/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 14:00:06 +0000</pubDate>
		<dc:creator>Derek@TheDailyLinux</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[fluff]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://www.thelinuxdaily.com/?p=2249</guid>
		<description><![CDATA[Described below are two different methods for quickly generating a certain size of fluff, or filler, to be inserted into a text file for testing purposes. The first method is to use a single line command which will very quickly fill a file with random text (must have base64 installed). The second method is to [...]]]></description>
			<content:encoded><![CDATA[<p>Described below are two different methods for quickly generating a certain size of fluff, or filler, to be inserted into a text file for testing purposes.  The first method is to use a single line command which will very quickly fill a file with random text (must have <code>base64</code> installed).  The second method is to use a script that will quickly fill a file with text that the user specifies  (there are some limitations).<br />
<span id="more-2249"></span><br />
For the first method, let&#8217;s use this scenario:  we need to quickly come up with a 2MB file to test a text scanning function within our program and it does not matter what the text is.  Running the following command will create this file for us and call it <em>random.txt</em>.</p>
<pre>
base64 /dev/random | head -c 2000000 &gt; random.txt
</pre>
<p>For the second method, let&#8217;s use this scenario:  we need to quickly fill a file with the string &#8220;abcdefghijklmnopqrstuvwxyz 123456890n&#8221; repeated until the result is 2MB in size in order to test a parsing algorithm in our program.  The following script will generate this file for us and call it <em>result.txt</em>.  I have tested this script up to 20MB and it took only a few seconds, however, the larger the size, the longer this script will take to run (very inefficient for really large files).  At first glance, it seems like a complicated script, but upon second glance you&#8217;ll notice that it&#8217;s mostly user defined variables and safe guard checks for user input.</p>
<pre>
#!/bin/sh

# Variables; 2000000 = 2MB
max_size=20000000
filename=result.txt
tmpfilename=tmp.txt
string=&quot;abcdefghijklmnopqrstuvwxyz 123456890n&quot;

# File checks so we don't overwrite somebody's important files
if [ -e $tmpfilename ]; then
        echo &quot;File: $tmpfilename exists!&quot;
        echo &quot;This script is going to use this as a temporary file, so either&quot;
        echo &quot;modify the script variable $tmpfilename or remove the file.&quot;
        exit 1
fi

if [ -e $filename ]; then
        echo &quot;File: $filename exists!&quot;
        echo &quot;Remove it and restart this script&quot;
        exit 1
else
        echo -e $string &gt; $filename
fi

# Initialize the variable
size=$(stat -c %s $filename)

# Start the loop, increasing the size of the file 2x until reaching max_size
while [ $size -lt $max_size ]; do
        cat $filename &gt; $tmpfilename
        cat $tmpfilename &gt;&gt; $filename
        size=$(stat -c %s $filename)
done

# Chop off any excess
head -c $max_size $filename &gt; $tmpfilename
mv $tmpfilename $filename
</pre>
<p>I believe the first method is fantastic.  It creates a file very quickly and it&#8217;s easy to understand.  I can see room for improvement on the second method.  If you have any suggestions, please feel free to share them with me!  I would really love to hear it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelinuxdaily.com/2010/12/creating-a-fluff-file-filled-with-specified-or-random-text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

