<?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>TLF Blog &#187; Shell Programming</title>
	<atom:link href="http://thelinuxfix.com/blog/category/shell-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelinuxfix.com/blog</link>
	<description>Hosting, Unix, and everything in between.</description>
	<lastBuildDate>Wed, 06 Feb 2013 18:05:02 +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>Learning Linux Commands:  tee</title>
		<link>http://thelinuxfix.com/blog/2012/02/16/learning-linux-commands-tee/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-linux-commands-tee</link>
		<comments>http://thelinuxfix.com/blog/2012/02/16/learning-linux-commands-tee/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 23:16:49 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Hosting]]></category>
		<category><![CDATA[Shell Programming]]></category>
		<category><![CDATA[linux commands]]></category>
		<category><![CDATA[tee]]></category>

		<guid isPermaLink="false">http://thelinuxfix.com/blog/?p=174</guid>
		<description><![CDATA[So little input, so much output As any regular Unix user can tell you, there are three primary &#8220;streams&#8221; in which programs are fed and output data.    STDIN, which by default is the keyboard.   STDOUT, which by default is your terminal/screen, and STDERR, which by default is also your terminal/screen.   In addition [...]]]></description>
			<content:encoded><![CDATA[<h2>So little input, so much output</h2>
<p>As any regular Unix user can tell you, there are three primary &#8220;streams&#8221; in which programs are fed and output data.    STDIN, which by default is the keyboard.   STDOUT, which by default is your terminal/screen, and STDERR, which by default is also your terminal/screen.   In addition to their textual reference, these three also have file descriptor numbers assigned (you&#8217;ll find out why in a moment).  Good Unix shell users regularly take full advantage of all of these.   In fact, if you&#8217;ve done any Unix or Linux work you&#8217;ve likely used them without even knowing.   For example.</p>
<pre>cat ~/textfile.txt | more</pre>
<p>You&#8217;ve certainly used the pipe (&#8216;|&#8217;) character before, but do you know what it is actually doing behind-the-scenes?   Effectively, the pipe tells the shell to &#8220;stitch&#8221; the STDOUT from the &#8216;cat&#8217; command to STDIN of the &#8216;more&#8217; command.    In Unix-land this is referred to as creating a pipeline.</p>
<p>&nbsp;</p>
<h2>Master of redirection</h2>
<p>In magician terms, redirection means to draw away the focus of the audience so something sneaky and magical can be occurring elsewhere.   While there&#8217;s very little sneaky going on in the Unix shell, when used correctly STDIN and STDOUT redirection can be pretty magical.   For instance:</p>
<pre>/usr/bin/somepossiblybrokenprogram.sh  1&gt; /var/log/myapp.log  2&gt; /var/log/myapp-error.log</pre>
<p>Stiches the &#8220;regular&#8221; output of your possiblybrokenprogram to STDIN of a log file, while the errors go to STDIN of a log file aptly named myapp-error.log.    Remember I mentioned the file descriptor numbers?   Now you know what they&#8217;re for, and here&#8217;s how they&#8217;re assigned:</p>
<pre>STDIN  - 0
STDOUT - 1
STDERR - 2</pre>
<p>Now you should be able to see how we did that bit of &#8220;magic&#8221; above.   Redirection of output can be darn handy when debugging or extremely useful when running things as scheduled (cron) jobs.   Also note: by default the &#8217;1&#8242; when redirecting STDOUT is unnecessary, since using a bare &#8220;&gt;&#8221; assumes you mean STDOUT.  We specified it above for clarity&#8217;s sake, and you&#8217;re welcome to do it as well.  Either way works.</p>
<p>There is a downside to this however&#8211;you can only redirect any given file descriptor <em>once per command</em>. &#8230;but what if you want to log things to a file <em>and</em> see it on the screen at the same time?  Impossible right?   NAY!</p>
<p>&nbsp;</p>
<h2>Line up to the &#8216;tee&#8217;</h2>
<p>That&#8217;s exactly where the &#8216;tee&#8217; utility comes into play.    Don&#8217;t think of &#8216;tee&#8217; as in a golf tee, but rather like a &#8216;tee&#8217; in plumbing terms.  In other words, it takes the stream and splits it in two, three, or even more:    STDOUT <em>and</em> STDIN simultaneously.  Even STDOUT and STDIN <em>multiple times.</em>   I know you&#8217;re already scratching your head, so how about a few examples.  First the simplest:</p>
<pre>/usr/bin/somepossiblybrokenprogram.sh | tee /var/log/myapp.log</pre>
<p>Hopefully you see what&#8217;s going on here.   Instead of wholly <em>redirecting</em> STDOUT to a single file, we&#8217;ve instead utilized the &#8216;tee&#8217; command to split it to both the screen and a file named /var/log/myapp.log. Pretty handy huh?   But wait, there&#8217;s more!</p>
<pre>/usr/bin/somepossiblybrokenprogram.sh | tee /var/log/myapp.log /var/log/other.log /var/log/joe.sh.log</pre>
<p>You see what it&#8217;s doing?  Not only is it displaying to the screen, but it simultaneously wrote the output to three separate log files!</p>
<p>Finally the &#8216;tee&#8217; command has an option to <em>append</em> to files instead of overwriting them each time it is invoked&#8211;which would be a bummer if you were debugging something.   You do that like so:</p>
<pre>/usr/bin/somepossiblybrokenprogram.sh | tee <span style="color: #ff0000;"><strong>-a</strong></span> /var/log/myapp.log /var/log/other.log /var/log/joe.sh.log</pre>
<p>There you have it!  The very tiny, but immensely useful &#8216;tee&#8217; command.</p>
<p><em>Learning Linux Commands is part of a blog series that highlights useful Linux/Unix commands for our<a title="TLFHosting" href="http://tlfhosting.com/hosting" target="_blank"> web hosting</a> clients.  Keep our blog RSS feed refreshed for new entries&#8211;we&#8217;ll be adding many more soon!  </em></p>
]]></content:encoded>
			<wfw:commentRss>http://thelinuxfix.com/blog/2012/02/16/learning-linux-commands-tee/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding a line to the beginning of a file in Unix/Linux</title>
		<link>http://thelinuxfix.com/blog/2009/05/11/adding-a-line-to-the-beginning-of-a-file-in-unixlinux/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-a-line-to-the-beginning-of-a-file-in-unixlinux</link>
		<comments>http://thelinuxfix.com/blog/2009/05/11/adding-a-line-to-the-beginning-of-a-file-in-unixlinux/#comments</comments>
		<pubDate>Mon, 11 May 2009 20:38:46 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[Shell Programming]]></category>

		<guid isPermaLink="false">http://thelinuxfix.com/blog/?p=118</guid>
		<description><![CDATA[This morning someone asked me how to do a relatively simple operation from a Unix shell: Adding a line of text to the beginning of a file. Much to my surprise he asked for help with amazingly complex methods he had tried. Okay everyone, no need to hit this thumbtack with a sledgehammer!  Let’s say you have a [...]]]></description>
			<content:encoded><![CDATA[<p>This morning someone asked me how to do a relatively simple operation from a Unix shell: <strong>Adding a line of text to the beginning of a file.</strong></p>
<p>Much to my surprise he asked for help with <a href="http://archives.devshed.com/forums/unix-linux-135/insert-line-in-the-first-line-372253.html">amazingly complex methods</a> he had tried.</p>
<p>Okay everyone, no need to hit this thumbtack with a sledgehammer!  Let’s say you have a file “textfile.txt” and want to put “&lt;Header&gt;” on the top:</p>
<p>echo -e “&lt;Header&gt;\n$(cat textfile.txt)” &gt; textfile.txt</p>
<p>There you go, nice and simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelinuxfix.com/blog/2009/05/11/adding-a-line-to-the-beginning-of-a-file-in-unixlinux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
