<?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/"
	xmlns:go='http://ns.gigaom.com/'
xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>GigaOM &#187; Apple</title>
	<atom:link href="http://gigaom.com/apple/tag/dig-into-unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://gigaom.com</link>
	<description></description>
	<lastBuildDate>Fri, 10 Feb 2012 13:01:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gigaom.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/0db8f6557d022075dbbf010c54d46d93?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>GigaOM &#187; Apple</title>
		<link>http://gigaom.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gigaom.com/osd.xml" title="GigaOM" />
	<atom:link rel='hub' href='http://gigaom.com/?pushpress=hub'/>
		<item>
		<title>Dig Into Unix: Sed and Awk</title>
		<link>http://gigaom.com/apple/dig-into-unix-sed-and-awk/</link>
		<comments>http://gigaom.com/apple/dig-into-unix-sed-and-awk/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 20:37:46 +0000</pubDate>
		<dc:creator>Jon Buys</dc:creator>
				<category><![CDATA[Walkthroughs]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[dig into unix]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://theappleblog.com/?p=29831</guid>
		<description><![CDATA[Time again to pop a shell and dig into the deep, geeky Unix internals of OS X with Dig Into Unix. Today we are going to look at two top-shelf power tools for text editing: sed and awk. Sed is a Stream EDitor, and if you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=173173&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img  title="Terminal" src="http://gigapple.files.wordpress.com/2009/05/terminal.png?w=133&#038;h=118" alt="Terminal" width="133" height="118" class=" alignleft" /></p>
<p class="excerpt">Time again to pop a shell and dig into the deep, geeky Unix internals of OS X with <a href="http://theappleblog.com/tag/dig-into-unix/">Dig Into Unix</a>. Today we are going to look at two top-shelf power tools for text editing: <a href="http://developer.apple.com/documentation/Darwin/Reference/Manpages/man1/sed.1.html">sed</a> and <a href="http://developer.apple.com/documentation/Darwin/Reference/Manpages/man1/awk.1.html">awk</a>.</p>
<p>Sed is a Stream EDitor, and if you recall our <a href="http://gigaom.com/apple/dig-into-unix-standard-streams/">previous</a> Dig Into Unix installment concerning standard streams, you&#8217;ll understand that the streams we are talking about are actually just text from one source or another. Sed&#8217;s bread and butter is text search and replace, very similary to the &#8220;Edit&#8221; and &#8220;Find&#8230;&#8221; functions in TextEdit and many other GUI text editors. Unlike those text editors though, sed, by default, will write its output to the screen, or stdout. <span id="more-173173"></span></p>
<p>As an example, try some basic operations on this string of text:<br />
<code>The quick brown fox jumped over the lazy dog's back.</code></p>
<p>Save the string of text as a file named test.txt, and type this into the Terminal:<br />
<code>sed s/quick/slow/g test.txt</code></p>
<p>The fox is now slow on the screen, but not changed in the file itself. To follow the stream, the text came from the file, through sed, and to the screeen. The best set of examples I&#8217;ve found for getting right into sed and starting to play with it is the collection of <a href="http://sed.sourceforge.net/sed1line.txt">sed one liners</a> hosted at Sourceforge.</p>
<p>Personally, I use sed when I&#8217;ve got a large number of configuration files that need to be edited. For example, it might be decided that we do not need our <a href="http://www.nagios.org/">Nagios</a> monitoring system alerting on the a certain statistic. I could go into 100 different files and perform the same action on all of them, or I could rely on a simple shell script and sed to do it for me.</p>
<p><code><br />
for each in `ls *.cfg`; do<br />
mv $each $each.bak #Safety First!<br />
sed '30,35s/^/#/g' $each.bak &gt; $each<br />
done<br />
</code></p>
<p>This will plow through all of the config files in a certain directory and add a # sign at the beginning of lines 30 through 35, commenting those lines out. Then I can restart Nagios, and if all goes well, delete all of the .bak files created as backups by the script.</p>
<p>While sed operates on lines and regular expressions (the subject of a future Dig Into Unix article!), awk works with <em>fields</em>. When given a stream of text, either from a text file or piped in from another application, awk can manipulate the text and rearrange the words. By default, awk separates the text fields by a space character, but you can use any other character you&#8217;d like.</p>
<p>Like sed, awk also has a great collection of <a href="http://www.pement.org/awk/awk1line.txt">one liners</a>, this collection here is a great resource collected by Eric Pement. In my day to day activities, I call on awk when I want to format text for a report or to be input into another application.</p>
<p>To use our quick brown fox example again, we can print only the fourth, third, and second words, in reverse order, with this command:<br />
<code>awk '{ print $4, $3, $2 }' test</code></p>
<p>That will print out &#8220;fox brown quick&#8221;. Not very practical or useful. Something more practical might be to manipulate a list of comma separated values. Using the &#8220;-F&#8221; flag in awk, you can tell awk to separate it&#8217;s fields based on the comma, as in:<br />
<code>awk -F, '{ print $1 }' test</code></p>
<p>Since there are no commas in the test file, this will print the entire string of text. So, we could run this command to take care of that:<br />
<code>sed s/ /,/g test &gt; test2</code></p>
<p>This will use sed to replace all spaces with commas. The backslash is there to escape a special character, so the space is interpreted literally and not as part of a command. Now, you could use awk to manipulate the string of text as needed.<br />
<code>awk -F, '{ print $7, $8, $9, $10, $5, $6, $1, $2, $3, $4 " did" }' test2</code></p>
<p>This article has just barely skimmed the surface of what sed and awk can do. There are some rather hefty books dedicated to the pair, <a title="sed &amp; awk | O'Reilly Media" href="http://oreilly.com/catalog/9781565922259/">this one</a> from O&#8217;Reilly has been on my desk for years now. Awk is an entire programming language, but the point of this series is not to teach the in-depth details, it&#8217;s just to get your feet wet, and maybe, just maybe leave you thirsting for more. The real rub is that everything that sed and awk can do can also be done, at times more efficiently, with the practical extraction and report language&#8230;better known as Perl, which is the subject of a future Dig Into Unix article.</p>
<p><strong>Related research and analysis from GigaOM Pro:</strong><br />Subscriber content. <a href="http://pro.gigaom.com/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=173173+dig-into-unix-sed-and-awk&utm_content=oszen">Sign up for a free trial</a>.</p><ul><li><a href="http://pro.gigaom.com/2011/03/why-ipad-2-will-lead-consumers-into-the-post-pc-era/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=173173+dig-into-unix-sed-and-awk&utm_content=oszen">Why iPad 2 Will Lead Consumers Into the Post-PC&nbsp;Era</a></li><li><a href="http://pro.gigaom.com/2011/03/the-near-term-evolution-of-social-commerce/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=173173+dig-into-unix-sed-and-awk&utm_content=oszen">The Near-Term Evolution of Social&nbsp;Commerce</a></li><li><a href="http://pro.gigaom.com/2011/02/content-farms-the-players-the-benefits-the-risks/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=173173+dig-into-unix-sed-and-awk&utm_content=oszen">Content Farms: The Players, The Benefits, The&nbsp;Risks</a></li></ul><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=173173&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gigaom.com/apple/dig-into-unix-sed-and-awk/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d5b8247e2eb580f5443ade7bbf2a067?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jBuys</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/05/terminal.png" medium="image">
			<media:title type="html">Terminal</media:title>
		</media:content>
	</item>
		<item>
		<title>Dig Into Unix: Standard Streams</title>
		<link>http://gigaom.com/apple/dig-into-unix-standard-streams/</link>
		<comments>http://gigaom.com/apple/dig-into-unix-standard-streams/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:00:54 +0000</pubDate>
		<dc:creator>Jon Buys</dc:creator>
				<category><![CDATA[SYN Feature Enterprise]]></category>
		<category><![CDATA[Walkthroughs]]></category>
		<category><![CDATA[dig into unix]]></category>
		<category><![CDATA[pipe]]></category>
		<category><![CDATA[stderr]]></category>
		<category><![CDATA[stdin]]></category>
		<category><![CDATA[stdout]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://theappleblog.com/?p=27216</guid>
		<description><![CDATA[This is the third installment of our Dig Into Unix series, an ongoing look into the deep, geeky insides of the core of OS X. In the first part, we got to fire up the Terminal and take a look around the filesystem as the OS [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=172999&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img  title="terminal_icon" src="http://gigapple.files.wordpress.com/2009/06/terminal_icon.png?w=150&#038;h=150" alt="terminal_icon" width="150" height="150" class=" alignleft" /></p>
<p class="excerpt">This is the third installment of our <a title="dig into unix" href="http://theappleblog.com/tag/dig-into-unix/">Dig Into Unix series</a>, an ongoing look into the deep, geeky insides of the core of OS X. In the <a href="http://gigaom.com/apple/dig-into-unix/">first part</a>, we got to fire up the Terminal and take a look around the filesystem as the OS sees it, which is slightly different from how the rest of us see it through the Finder. In the <a href="http://gigaom.com/apple/dig-into-unix-vi/">second installment</a>, we took a look at vi, the ancient text editor fit for kings.</p>
<p>Today, I’d like to cover a very basic, but very powerful, aspect of Unix, the three standard streams: standard in, standard out, and standard error. These three are normally abbreviated as stdin, stdout and stderr. <span id="more-172999"></span></p>
<p><img  title="dig_unix_single_stream" src="http://gigapple.files.wordpress.com/2009/06/dig_unix_single_stream.png?w=161&#038;h=160" alt="dig_unix_single_stream" width="161" height="160" class=" alignleft" /> When interacting with an application on the command line, it’s good to imagine that application with three pipes. One is a big funnel, right on top &#8212; this is stdin, accepting input from the keyboard. The second pipe is on the bottom, stdout, sending text to the Terminal. The third, stderr, sticks out the side, and normally also sends its output to the Terminal. I say &#8220;normally&#8221; because the standard streams can be redirected by certain key characters. For example, if you use “cat” to display a document, you are sending stdout to the Terminal. However, if you use “cat” like this, you actually send the output of the cat command to another file, redirecting stdout to a new text file.</p>
<p><code>cat someFile 1&gt; someCopy.txt</code></p>
<p>The number one in the command above represents stdout. The right arrow is telling the shell that you’d like to redirect stdout to a new file. If that file already exists, this command will replace that file with the contents of “someFile”. If you’d like to append the text to someCopy.txt, you could do this as well:</p>
<p><code>cat someOtherFile 1&gt;&gt; someCopy.txt</code></p>
<p>You’ll notice that I used two right arrows that time. Using the double right arrows tells the shell to append the text to the end of the file. This works great for keeping a running log of regularly scheduled tasks.</p>
<p>Sometimes you’ll want to either keep error messages out of the way, or you’ll like to keep a log of error messages. In this case, you can redirect stderr to a file like this:</p>
<p><code>grep something somewhere 2&gt; grep_error_log</code></p>
<p>The grep command is another Unix power tool that we’ll touch on later in the series. For now, just know that grep searches for strings of text in files. In the command above, I told grep to search for the string “something” in the file “somewhere” and then redirect stderr “2” to a file named “grep_error_log.” Run this command without the redirect on it, then add the redirect and run it again. You can compare the contents of the error log to what you saw on the terminal and see that they are the same.</p>
<p>Being able to redirect output is handy, but not really a “power tool.” The real power is in the pipe, the key on the far right above the return key, that looks like this: &#8220;|&#8221;, and it’s probably a key that you may have looked at and wondered…“why is that there?” The pipe takes the stdout from one command and feeds it to the stdin of another command. Think about the picture above; now imagine plugging lots of different commands into each other in a long string. The nature of Unix is that it is continually used in ways that its creators never imagined. It is truly a system limited only by your creativity and curiosity.</p>
<p style="text-align: center;"><img  title="dig_unix_multiple_streams" src="http://gigapple.files.wordpress.com/2009/06/dig_unix_multiple_streams.png?w=604" alt="dig_unix_multiple_streams" class=" alignleft" /></p>
<p>During one of my early classes on Unix, after we had entered in a command very similar to the one below, the instructor stood up and said, &#8220;Congratulations, you are all programmers!&#8221; We weren&#8217;t, not by a long shot, but the idea was that by entering commands and stringing them together in the Terminal, we were programming the computer to perform a specific task. We were not using a traditional &#8220;programming&#8221; language like C or even Java, but we were <em>programming</em> directly to the shell.</p>
<p>To put this idea into action, consider this command:</p>
<p><code>cat someFile | grep someText 1&gt; foundText</code></p>
<p>First of all, yes, I’m aware that this is a <a href="http://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat">useless use of cat</a>. That’s not the point. The point is that the above command connects cat&#8217;s stdout, which normally points to the Terminal, to stdin of grep, which then searches what it has been handed for the string “someText,” and then, just for good measure, redirect the output of grep to a file named “foundText.”</p>
<p>The simple illustrations I’ve given here work in all kinds of situations. Personally, I use the pipe and stream redirection to parse the results of commands that search logs, retrieve information from the Internet, and reformat data into comma separated values for importing into Numbers, Excel or OpenOffice. There are very few limitations to what you can do with plain text and the tools lurking under the hood of your Mac.</p>
<p><strong>Related research and analysis from GigaOM Pro:</strong><br />Subscriber content. <a href="http://pro.gigaom.com/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172999+dig-into-unix-standard-streams&utm_content=oszen">Sign up for a free trial</a>.</p><ul><li><a href="http://pro.gigaom.com/2011/03/why-ipad-2-will-lead-consumers-into-the-post-pc-era/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172999+dig-into-unix-standard-streams&utm_content=oszen">Why iPad 2 Will Lead Consumers Into the Post-PC&nbsp;Era</a></li><li><a href="http://pro.gigaom.com/2011/03/the-near-term-evolution-of-social-commerce/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172999+dig-into-unix-standard-streams&utm_content=oszen">The Near-Term Evolution of Social&nbsp;Commerce</a></li><li><a href="http://pro.gigaom.com/2011/02/content-farms-the-players-the-benefits-the-risks/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172999+dig-into-unix-standard-streams&utm_content=oszen">Content Farms: The Players, The Benefits, The&nbsp;Risks</a></li></ul><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=172999&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gigaom.com/apple/dig-into-unix-standard-streams/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d5b8247e2eb580f5443ade7bbf2a067?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jBuys</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/terminal_icon.png" medium="image">
			<media:title type="html">terminal_icon</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/dig_unix_single_stream.png" medium="image">
			<media:title type="html">dig_unix_single_stream</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/dig_unix_multiple_streams.png" medium="image">
			<media:title type="html">dig_unix_multiple_streams</media:title>
		</media:content>
	</item>
		<item>
		<title>Dig Into Unix: vi</title>
		<link>http://gigaom.com/apple/dig-into-unix-vi/</link>
		<comments>http://gigaom.com/apple/dig-into-unix-vi/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 19:00:01 +0000</pubDate>
		<dc:creator>Jon Buys</dc:creator>
				<category><![CDATA[SYN Feature Enterprise]]></category>
		<category><![CDATA[dig into unix]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[terminal.app]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://theappleblog.com/?p=25092</guid>
		<description><![CDATA[Continuing our Dig Into Unix series, we&#8217;ve now covered the absolute basics of launching Terminal.app, moving around the file system, looking at files with cat, and learning about commands with man. Now, I&#8217;d like to introduce you to the power of vi. vi (pronounced vee-eye) is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=172837&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img  title="terminal_icon" src="http://gigapple.files.wordpress.com/2009/06/terminal_icon.png?w=150&#038;h=150" alt="terminal_icon" width="150" height="150" class=" alignleft" /></p>
<p class="excerpt">Continuing our <a href="http://theappleblog.com/tag/dig-into-unix">Dig Into Unix</a> series, we&#8217;ve now covered the absolute basics of launching Terminal.app, moving around the file system, looking at files with <strong>cat</strong>, and learning about commands with <strong>man</strong>. Now, I&#8217;d like to introduce you to the power of <strong>vi</strong>.</p>
<p>vi (pronounced vee-eye) is a text editor for the command line, and was originally written by Bill Joy in 1976. vi is now part of every single Unix system, including OS X. The version included with Leopard is <a href="http://www.vim.org/">vim</a>, which stands for Vi iMproved. One of the major problems that vi has faced over the years is that it has a major learning curve. Every action that you take in vi you do with keyboard combos. Since there are so many actions that you can do, it takes awhile to get used to the different keystrokes, but once you do, they become ingrained in your fingers. From time to time, when typing in some other app, I find myself hitting the escape key followed by <code>jjj</code> or <code>hhh</code> trying to move the curser. <span id="more-172837"></span></p>
<h3>Getting Started</h3>
<p>To get started, open up Terminal.app and type <code>vi</code>. This starts the editor in command mode. Type <code>i</code> to enter insert mode, and then you can begin typing text into the file. Copy the text from this article and paste it into vi, regular CMD-C / CMD-V will work just fine. This will give you something to work with.</p>
<p>Now that you&#8217;ve got some text, it&#8217;d probably be good to save it. Hit the escape key to put the editor back into command mode, and type <code>:w newfile.txt</code>. This will create a text file named <code>newfile.txt</code> in your home directory. Press <code>i</code> again, and you can begin typing, starting where you left off. Press escape again.</p>
<h3>Navigating Lines</h3>
<p>To move to any line of text in vi, you can press the number of the line you want to go to and <code>G</code>. For example, to go to the first line, type <code>1G</code>. If you type that and the characters &#8220;1G&#8221; show up on your screen, you are not in command mode, don&#8217;t forget to press escape first. To get back to the end of the file, you can just press <code>G</code> by itself. That will bring you to the start of the last line in the file.</p>
<ul>
<li>To get to the end of a line, you can press <code>$</code>, and your curser will jump to the end of the line.</li>
<li>To go to the beginning of the line, you press <code>^</code>. Now you can jump around the file.</li>
<li>To move the curser one character at a time, use the four keys <code>h j k</code> and <code>l</code>. <code>h</code> moves one character to the left, <code>j</code> moves one character to the right, <code>k</code> to moves one character up, and <code>l</code> moves one character down.</li>
<li>To open a line below, you can press <code>o</code>, and to open a line above, <code>O</code>.</li>
</ul>
<h3>Configuration File</h3>
<p>These basic text editing commands form the basis of the real strengths of vi, and the reason I continue to use it every day. But to really take advantage of the power of vi, you need to create a configuration file for shortcuts.</p>
<p>Exit out of vi and write the file to disk at the same time by typing <code>:wq!</code> while in command mode. Now, create a new file, and open the file in vi for editing by typing <code>vi .vimrc</code>. The .vimrc file is the configuration file vi reads when starting. The important thing to remember about this configuration file is that you can add any of the commands above into the file, including movement, inserting text, and opening lines, and then map those commands to a keyboard combo. If you are familiar with <a href="http://www.smileonmymac.com/TextExpander/">TextExpander</a>, you&#8217;ll follow right along with this concept, but it&#8217;s even stronger than that.</p>
<p>We are going to create a single line of text in the file that looks like this:<br />
<code>map ,a 1GiJack Doe^MGuy in Charge^MACME Inc.^M111 Main St.^MAnytown, USA </code></p>
<p><code> </code></p>
<p><code>^M^M^M^MDear Sir,^M^M^M^MSincerely,^M^M^MJohn Doe^[10G i</code></p>
<p>The line above has been separated into two lines to fit on the page.  When you see <code>^M</code>, that&#8217;s not really two characters, it&#8217;s only one. It&#8217;s the return key, captured in raw form in the config file for use in generating our commands. To capture it, you press Control-V, followed by return. The esc key is also captured above by pressing Control-V and then hitting the escape key. That creates the <code>^[</code> character above, and allows us to enter some of the vi commands above into the config. Enter in that line of text above, using the Control-V trick to capture the return and esc characters.</p>
<p>Now, save the new file by typing <code>:wq!</code> in command mode, open up a new file with <code>vi file</code>, and test out your new <code>,a</code> shortcut. If everything went well, it should look just like the video below.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/ES8u-T0ozKM&amp;hl=en&amp;fs=1&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/ES8u-T0ozKM&amp;hl=en&amp;fs=1&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<h3>Worth Learning</h3>
<p>The learning curve with vi is steep, but as with most things worth learning, the payoff is significant. This short article has barely scratched the surface of what vi can do. vi can be a full-fledged IDE, or you could use it to write your next book. The mapping features above are especially helpful if you do any programming, or if you manage any Unix or Linux servers. If you&#8217;d like to learn more, I&#8217;d suggest starting with <code>man vi</code>, and maybe picking up a copy of the O&#8217;Reilly book &#8220;<a href="http://oreilly.com/catalog/9780596529833/">Learning the vi and Vim Editors</a>.&#8221; Of course, I&#8217;d be happy to help out as well.</p>
<p>Back when I was first learning Unix, I asked an old graybeard why I should bother learning such a complicated, ancient text editor. He told me that since everything in Unix is a file, and almost all files in Unix are text, I needed some way to edit those files to control the system. Someday, he said, I would find myself without any other text editor, and without any other way to change a file. He was right, since I&#8217;m now working as a Unix systems administrator, I&#8217;ve been in that position for several years. Over time, I&#8217;ve come to appreciate all that vi can do for me.</p>
<p><strong>Related research and analysis from GigaOM Pro:</strong><br />Subscriber content. <a href="http://pro.gigaom.com/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172837+dig-into-unix-vi&utm_content=oszen">Sign up for a free trial</a>.</p><ul><li><a href="http://pro.gigaom.com/2011/03/why-ipad-2-will-lead-consumers-into-the-post-pc-era/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172837+dig-into-unix-vi&utm_content=oszen">Why iPad 2 Will Lead Consumers Into the Post-PC&nbsp;Era</a></li><li><a href="http://pro.gigaom.com/2011/03/the-near-term-evolution-of-social-commerce/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172837+dig-into-unix-vi&utm_content=oszen">The Near-Term Evolution of Social&nbsp;Commerce</a></li><li><a href="http://pro.gigaom.com/2011/02/content-farms-the-players-the-benefits-the-risks/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172837+dig-into-unix-vi&utm_content=oszen">Content Farms: The Players, The Benefits, The&nbsp;Risks</a></li></ul><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=172837&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gigaom.com/apple/dig-into-unix-vi/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d5b8247e2eb580f5443ade7bbf2a067?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jBuys</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/terminal_icon.png" medium="image">
			<media:title type="html">terminal_icon</media:title>
		</media:content>
	</item>
		<item>
		<title>Dig Into Unix</title>
		<link>http://gigaom.com/apple/dig-into-unix/</link>
		<comments>http://gigaom.com/apple/dig-into-unix/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 20:00:51 +0000</pubDate>
		<dc:creator>Jon Buys</dc:creator>
				<category><![CDATA[CNN Big Tech]]></category>
		<category><![CDATA[NYT Enterprise]]></category>
		<category><![CDATA[SYN Feature Enterprise]]></category>
		<category><![CDATA[darwin]]></category>
		<category><![CDATA[dig into unix]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://theappleblog.com/?p=25093</guid>
		<description><![CDATA[When Apple revamped its operating system and adopted Nextstep as the base of OS X, they brought along with it an extremely powerful version of Unix based on the open-source project FreeBSD, now known as Darwin. Unix has a long history, one that started in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=172838&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img  title="terminal_icon" src="http://gigapple.files.wordpress.com/2009/06/terminal_icon.png?w=150&#038;h=150" alt="terminal_icon" width="150" height="150" class=" alignleft" /></p>
<p class="excerpt">When Apple revamped its operating system and adopted <a href="http://en.wikipedia.org/wiki/NeXTSTEP">Nextstep</a> as the base of OS X, they brought along with it an extremely powerful version of Unix based on the open-source project <a href="http://www.freebsd.org/">FreeBSD</a>, now known as <a href="http://developer.apple.com/referencelibrary/Darwin/">Darwin</a>.</p>
<p>Unix has a long history, one that started in the basements of Bell Labs by a group of AT&amp;T engineers some 40 years ago. A professor in a C programming course I took once said that they were supposed to be writing drivers for the AT&amp;T hardware, but instead, they wanted some way to use the system to play games, so they invented Unix.</p>
<p>Unix is now a mature and robust operating system, and since OS X is based on Unix, it has inherited all of its power, and some of its complexity. The beautiful <a href="http://developer.apple.com/macosx/architecture/index.html">aqua</a> interface that we are used to seeing is really all that is needed, but if you would like to take a look at what makes your computer tick, Apple included Terminal.app to act as a window into the GUI and into the Unix soul of OS X.</p>
<p>Nick started a <a href="http://gigaom.com/apple/unix-tip-remember-the-tab/">great series</a> back in January 2007 on this subject, and now I&#8217;d like to cover some of the basics again, and maybe bring a different point of view to the table as well. <span id="more-172838"></span></p>
<p style="text-align: center;"><img  title="Finding Terminal" src="http://gigapple.files.wordpress.com/2009/06/findingterminal.png?w=590&#038;h=343" alt="Finding Terminal" width="590" height="343" class=" alignleft" /></p>
<p>Open Terminal.app (found in Applications → Utilities), and you&#8217;ll see a window with a prompt waiting for you to start typing.</p>
<p style="text-align: center;"><img  title="Terminal" src="http://gigapple.files.wordpress.com/2009/06/terminal1.png?w=505&#038;h=367" alt="Terminal" width="505" height="367" class=" alignleft" /></p>
<p>At this point, it&#8217;s important to understand a few things about the Terminal. For one, the commands that you can type are interpreted and carried out immediately, no waiting around. So if you tell it to remove a file, it will do it right then, with no easy way of recovering it. There isn&#8217;t a recycle bin on the command line (not without a little coaxing, anyway). Secondly, since Unix was developed decades ago, many of the commands seem a bit archaic. Back when most of these utilities were written, they were all abbreviated to save space and cut down on the number of keystrokes you&#8217;d need to type. Below is a list of a few essentials, and another list from Nick&#8217;s post is <a href="http://gigaom.com/apple/unix-tip-commands/">here</a>.</p>
<ul>
<li><code>ls</code> (list): Probably one of the most important commands, it lets you see what&#8217;s in your current directory.</li>
<li><code>cd</code> (change directory): This is how you move about the filesystem in Unix, for example, to move from Library to Documents.</li>
<li><code>file</code>: This one isn&#8217;t short for anything, but it will give you a brief description of what a particular file is.</li>
<li><code>cat</code> (concatenate): Or &#8220;Grab everything in this file and let me read it.&#8221;</li>
<li><code><a href="http://gigaom.com/apple/unix-tip-its-a-man-man/">man</a></code> (manual): The online manual will describe most commands that you&#8217;re interested in; for example, typing <code>man ls</code> will give you the manual page about the <code>ls</code> command.</li>
</ul>
<p>It might be helpful to open a Finder window and put it right next to the Terminal. Open the Finder so it shows your home directory. Type <code>ls</code> in the Terminal to see the same files that you see in the Finder. Try another command: <code>touch</code>. <code>touch</code> is designed to change the last accessed timestamp of a file, but it will also create a blank file. You can see the file created in the Finder as well. You can cat the file, and see that there is nothing in it.</p>
<p>Now that you&#8217;ve created a file, and looked at its (blank) contents, you can remove the file using the <code>rm</code> command. This is one of those dangerous commands that, if used carelessly, can really screw things up. For this example, carefully type <code>rm file</code> into the terminal, and watch the file disappear in the Finder. You&#8217;ll notice that your Trash stays empty &#8212; that file is goners.</p>
<p>The Unix filesystem is a <a href="http://www.pathname.com/fhs/pub/fhs-2.3.html">nested hierarchy</a>, with each directory separated by a forward slash (<code>/</code>). The current working directory is symbolized as a dot (<code>.</code>), and the parent directory is symbolized by two dots (<code>..</code>). The top of the hierarchy, known as the root, is symbolized by a single forward slash.</p>
<p>So, to move to the parent directory, you could type <code>cd ..</code>. If you were in the Library directory and you typed this command, you would then be moved into your home directory. To see the very top of the hierarchy, you would type <code>cd /</code>. Type this command now.</p>
<p>Also, move in the Finder to the hard drive where you have OS X installed. I have mine named, originally, &#8220;OS X&#8221;.  Type <code>ls</code> in the Terminal, and you&#8217;ll notice a few more files than you can see in the Finder. These files are important Unix system files, and should not be touched unless you really know what you&#8217;re doing&#8230;and really, not even then. To illustrate the importance of these files, you could type <code>file mach_kernel</code>.</p>
<p><img  title="Terminal" src="http://gigapple.files.wordpress.com/2009/06/terminal_02.png?w=545&#038;h=407" alt="Terminal" width="545" height="407" class=" alignleft" /></p>
<p>This is the kernel, the core of the operating system. Do <strong>not</strong> mess with this file. Several of the other files are directories. You can change directories into <code>/bin</code>, for example, and type <code>ls</code> to list the contents of that directory. You&#8217;ll find a file in that directory named &#8220;ls,&#8221; which is the executable for the <code>ls</code> command. When you type <code>ls</code> into the terminal, it executes this tiny app. A great place to learn about Unix is to get a list of this directory, and then read the <code>man</code> page for every file listed.</p>
<p>To exit the Terminal, just CMD-Q like any other app, and you are back in the comforts of OS X.</p>
<p>This has been a very brief overview of how to go from absolutely no Unix knowledge whatsoever to the smallest amount of Unix knowledge necessary to poke around a bit. There&#8217;s a lot of power under there, but before you can really start to harness it, you need to get a decent understanding of the hows and whys.</p>
<p><strong>Related research and analysis from GigaOM Pro:</strong><br />Subscriber content. <a href="http://pro.gigaom.com/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172838+dig-into-unix&utm_content=oszen">Sign up for a free trial</a>.</p><ul><li><a href="http://pro.gigaom.com/2011/03/why-ipad-2-will-lead-consumers-into-the-post-pc-era/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172838+dig-into-unix&utm_content=oszen">Why iPad 2 Will Lead Consumers Into the Post-PC&nbsp;Era</a></li><li><a href="http://pro.gigaom.com/2011/03/the-near-term-evolution-of-social-commerce/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172838+dig-into-unix&utm_content=oszen">The Near-Term Evolution of Social&nbsp;Commerce</a></li><li><a href="http://pro.gigaom.com/2011/02/content-farms-the-players-the-benefits-the-risks/?utm_source=apple&utm_medium=editorial&utm_campaign=auto3&utm_term=172838+dig-into-unix&utm_content=oszen">Content Farms: The Players, The Benefits, The&nbsp;Risks</a></li></ul><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gigaom.com&amp;blog=14960843&amp;post=172838&amp;subd=gigaom2&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gigaom.com/apple/dig-into-unix/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7d5b8247e2eb580f5443ade7bbf2a067?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jBuys</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/terminal_icon.png" medium="image">
			<media:title type="html">terminal_icon</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/findingterminal.png" medium="image">
			<media:title type="html">Finding Terminal</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/terminal1.png" medium="image">
			<media:title type="html">Terminal</media:title>
		</media:content>

		<media:content url="http://gigapple.files.wordpress.com/2009/06/terminal_02.png" medium="image">
			<media:title type="html">Terminal</media:title>
		</media:content>
	</item>
	</channel>
</rss>
