<?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>Chris JeanChris Jean &#187; Mastering The Command Line</title>
	<atom:link href="http://chrisjean.com/tag/mastering-the-command-line/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisjean.com</link>
	<description>Linux, WordPress, programming, anime, and other stuff</description>
	<lastBuildDate>Tue, 08 May 2012 20:36:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>man Pages for C Development in Ubuntu</title>
		<link>http://chrisjean.com/2009/09/26/man-pages-for-c-development-in-ubuntu/</link>
		<comments>http://chrisjean.com/2009/09/26/man-pages-for-c-development-in-ubuntu/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 05:00:23 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1493</guid>
		<description><![CDATA[I recently tutored a friend in C coding. Since I hadn&#8217;t worked with C in at least 8 years, I really needed to have some references to rely on for syntax and other specifics. Fortunately, there are some easy man pages that can be installed in Ubuntu that offer helpful information that I was able [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>I recently tutored a friend in C coding. Since I hadn&#8217;t worked with C in at least 8 years, I really needed to have some references to rely on for syntax and other specifics. Fortunately, there are some easy man pages that can be installed in Ubuntu that offer helpful information that I was able to use to help refresh my memory.</p>
<p>These man pages are easily installed by installing the <code>manpages-dev</code> package. You can install this package via Synaptic or directly on the command line. I like the command line method personally, so I ran <code>sudo apt-get install manpages-dev</code> from the command line to quickly install the package.</p>
<p>After installing the package, I&#8217;m able to access man pages for functions such as <code>printf</code>, <code>opendir</code>, and <code>putc</code>. For each function, it shows the valid syntax as well as what library is required to make use of the function.</p>
<p>The information isn&#8217;t limited to functions as you can also access information on the libraries, such as <code>stdio</code> or <code>string</code>.</p>
<p>To access any of this information, simply run <code>man [function or library name]</code> such as <code>man stdio</code>.</p>
<p>This package isn&#8217;t limited to C functions/libraries. It is a general use Linux development suite of man pages. For a full list of what is installed, check out the <a href="http://packages.ubuntu.com/hardy/all/manpages-dev/filelist" target="_blank">file list</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/09/26/man-pages-for-c-development-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updating Multiple Git Repositories Easily Using Bash for Loop</title>
		<link>http://chrisjean.com/2009/09/15/updating-multiple-git-repositories-easily-using-bash-for-loop/</link>
		<comments>http://chrisjean.com/2009/09/15/updating-multiple-git-repositories-easily-using-bash-for-loop/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 16:42:14 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1508</guid>
		<description><![CDATA[All of the WordPress themes that I work on for iThemes are managed as Git repositories. Recently, we moved past the 100 repositories mark. That&#8217;s a lot of repositories to manage, and unfortunately, too many of those repositories contain duplicated information. Later on, I might delve into how we use Git to manage our theme [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>All of the <a href="http://wordpress.org/" target="_blank">WordPress</a> themes that I work on for <a href="http://ithemes.com/" target="_blank">iThemes</a> are managed as Git repositories. Recently, we moved past the 100 repositories mark. That&#8217;s a lot of repositories to manage, and unfortunately, too many of those repositories contain duplicated information.</p>
<p>Later on, I might delve into how we use Git to manage our theme repos. For today, however, I&#8217;d like to focus on how I quickly and easily pushed up changes to more than a dozen repos in a single, albeit long, Bash command.</p>
<p>I had finished making updates to 16 <a href="http://ithemes.com/purchase/flexx-theme-wordpress-blog-themes/" target="_blank">Flexx</a> repos, and I needed to push all of those changes up. Since I had multiple working repos in that folder, I was lucky that each of these repos began with the text &#8220;Flexx&#8221;. Also, since they are all part of the same series and need to keep the same version number, that simplified the tagging as all could be tagged as 2.5.0.</p>
<p>Given this information, I simply ran the following command from the directory that contained all the repository directories:</p>
<div class="code">for i in `ls|grep Flexx`; do echo &#8220;&#8212; Pushing $i&#8221;; cd $i; git commit -am &#8217;2.5.0&#8242; &amp;&amp; git push &amp;&amp; git tag 2.5.0 &amp;&amp; git push &#8211;tags; cd ..; echo &#8220;&#8212; Finished $i&#8221;; done</div>
<p>There&#8217;s a lot going on here, so I&#8217;ll break it up and explain what I&#8217;m doing.</p>
<p><span id="more-1508"></span></p>
<div class="code">for i in `ls|grep Flexx`</div>
<p>This is basically a compound command. Bash&#8217;s <code>for</code> command is being used to step through the results of <code>ls|grep Flexx</code>. Each result will be stored to the variable <code>i</code> which can be referred to with <code>$i</code>.</p>
<p>In other words, I&#8217;m going to loop through each directory that contains the text &#8220;Flexx&#8221; (technically, it isn&#8217;t limited to just directories, but I don&#8217;t have any files containing &#8220;Flexx&#8221;, so I&#8217;m safe). For each iteration of the loop, the variable <code>$i</code> will contain the name of the folder I&#8217;m interested in.</p>
<div class="code">do echo &#8220;&#8212; Pushing $i&#8221;</div>
<p>The most important bit here is <code>do</code>. <code>do</code> simply begins the functional part of the loop and could be followed by any command that I wanted to start the loop with.</p>
<p>The <code>echo</code> command isn&#8217;t technically necessary. I simply have it there so that I can better determine what output belongs to which repository.</p>
<div class="code">cd $i</div>
<p>This is a very important command. It changes the directory to the repository I wish to run commands on.</p>
<div class="code">git commit -am &#8217;2.5.0&#8242; &amp;&amp; git push &amp;&amp; git tag 2.5.0 &amp;&amp; git push &#8211;tags</div>
<p>This is the command that actually does what I want to do in each repo directory. This adds all the modified files, commits them with a message of &#8220;2.5.0&#8243;, pushes the changes to the remote repository, creates a new tag of 2.5.0, and pushes up the new tag.</p>
<p>I have all of these commands chained together with <code>&amp;&amp;</code> so that if one command fails, the other ones won&#8217;t run. This prevents tagging in the event that the repository couldn&#8217;t be committed or pushed.</p>
<div class="code">cd ..</div>
<p>It&#8217;s a small command but necessary. This switches back to the main directory that holds all the repos, thus returning us back to a state that the next loop iteration can work with.</p>
<div class="code">echo &#8220;&#8212; Finished $i&#8221;</div>
<p>As with the other <code>echo</code> command, this simply allows me to keep track of what is going on more easily.</p>
<div class="code">done</div>
<p>The <code>done</code> command finishes out the loop iteration.</p>
<p>I hope that by sharing this you can gain a bit of insight into how I work with repositories while also learning more about how you can do things with Bash.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/09/15/updating-multiple-git-repositories-easily-using-bash-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Command Line History in Ubuntu Terminal</title>
		<link>http://chrisjean.com/2009/03/09/command-line-history-in-ubuntu-terminal/</link>
		<comments>http://chrisjean.com/2009/03/09/command-line-history-in-ubuntu-terminal/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 06:00:30 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1304</guid>
		<description><![CDATA[I&#8217;ve had a lot of fun recently posting about how to do stuff on the command line in Linux. My focus is specifically for Ubuntu users, but the information and techniques can be used for any Linux distro. Since I&#8217;m probably going to end up with a lot of content under this topic, I&#8217;ve decided [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>I&#8217;ve had a lot of fun recently posting about how to do stuff on the command line in Linux. My focus is specifically for Ubuntu users, but the information and techniques can be used for any Linux distro.</p>
<p>Since I&#8217;m probably going to end up with a lot of content under this topic, I&#8217;ve decided to create a dedicated tag: <a href="http://chrisjean.com/tag/mastering-the-command-line/">Mastering the Command Line</a>. I&#8217;ve gone through my older posts on this topic and tagged them as well. So, make sure to check out <a href="http://chrisjean.com/tag/mastering-the-command-line/" target="_blank">Mastering the Command Line</a> if you want to know how to become a command line power user.</p>
<p>Back to today&#8217;s topic. You&#8217;re starting to learn how to use the command line, but it&#8217;s annoying to always have to type in similar commands over and over. If only there were a way to pull up commands that you&#8217;ve already run to run again as is or to quickly modify. Today, I&#8217;m going to teach you how to do exactly this.</p>
<p><span id="more-1304"></span></p>
<h3>Introduction to BASH Command Line History</h3>
<p><a href="http://www.gnu.org/software/bash/" target="_blank">BASH</a> (Bourne Again SHell) is the default command line shell that is used in Ubuntu, and most other distros for that matter. The instructions that I give here are specific to BASH and may or may not work in other shells. Since most people don&#8217;t modify which shell they access through Terminal, I only point this out to inform those who have changed their shell that their experiences may differ depending on the shell used.</p>
<p>When you close a Terminal session, BASH writes all the commands you&#8217;ve run for that session to your ~/.bash_history file. When you load a new session, this file is read and those commands along with the new commands you execute for that session become your new command history.</p>
<p>It&#8217;s important to keep in mind that commands executed for a session are only written when you exit. If you have two sessions running concurrently, they cannot cross-reference the newly-executed commands run in the other session. Furthermore, if you force a Terminal session to quit, the command history will not be updated.</p>
<h3>Simple Command History Navigation</h3>
<p>The easy way to get started with your command history is to simply navigate with the up and down arrow keys. Using the up key, you can scroll through previous commands one command at a time. To scroll back down the list, you can use the down arrow key.</p>
<p>Once you&#8217;ve found the desired command, you can treat it as if you&#8217;ve typed that command in the command line. You can simply press Enter to execute the command again, or you can modify the command as needed before executing it.</p>
<p>This method is very good for quickly running commands that you have to execute repeatedly without many other commands between.</p>
<h3>Listing and Searching Command History</h3>
<p>BASH provides the history command that when executed with no options will list the entire command history. Each command will be preceeded with a number that represents that command&#8217;s command number. I&#8217;ll tell you how to use these numbers below.</p>
<p>You can also have the history command produce just the last certain number of commands by supplying a number. For example, if you want to see the previous ten commands, you can run the following:</p>
<div class="code">user:~$ <strong>history 10</strong><br />
510  which bash<br />
511  echo $$<br />
512  ps aux|grep 32115<br />
513  which sh<br />
514  history<br />
515  man history<br />
516  history|grep echo<br />
517  vi ~/.bash_history<br />
518  tail -10 ~/.bash_history<br />
519  history 10</div>
<p>Notice that the last command is the same as the command you ran to produce it. So, &#8220;history 1&#8243; will just show you &#8220;[command number] history 1&#8243;.</p>
<h4>Searching History</h4>
<p>If you remember that you ran a really cool command and can&#8217;t quite remember how you did it, combining history with <a href="http://chrisjean.com/2009/01/21/grep-why-i-love-linux/" target="_blank">grep</a> provides an easy way to find that command with just a piece of the command.</p>
<p>For example, let&#8217;s say that I ran a command that listed all the files that end in &#8220;.php&#8221; inside the ~/wordpress folder it then filters for just matches that contain the text &#8220;link&#8221; and then reverses the output. I was quite proud of that command, but I can&#8217;t remember how I did it. So, I search through my history.</p>
<p>Since I don&#8217;t think that I have the word &#8220;link&#8221; in many of my commands, I pick it as the target to search for and run the following:</p>
<div class="code">user:~$ <strong>history|grep link</strong><br />
412  find ~/wordpress -type f | grep \.php$ | grep link | sort -r</div>
<p>That&#8217;s what I was looking for. Now I can bask in the glory of my ingenious command and use it as the inspiration for other inspired words of command line magic.</p>
<h4>Running Previous Commands Again</h4>
<p>Searching through the history is great and all, but I&#8217;d rather not have to copy and paste found commands. I&#8217;d like to just run them immediately. BASH to the rescue again.</p>
<p>If you&#8217;d like to run the previous command again quickly, you can simply run &#8220;!!&#8221;. The first exclamation point tells BASH that you are running a previous commands. The second exclamation point tells BASH that it&#8217;s the last command that you&#8217;d like to run again. It&#8217;s a double exclamation point simply to make it quick and easy to type in.</p>
<p>I&#8217;ve already shown you how to look through your history and find a specific command in your history complete with the command number. Now is the time to make use of that number. If I wanted to execute command number 510 (which bash) again using the history, I can simply run the following:</p>
<div class="code">user:~$ <strong>!510</strong><br />
which bash<br />
/bin/bash</div>
<p>Notice how running the command again first lists the actual command and then the output.</p>
<p>BASH substitutes the !number command with the actual command from the history. This allows you to supply additional parameters to the command. The following is a silly example, but it gets the point across.</p>
<div class="code">user:~$ <strong>!510 dash</strong><br />
which bash dash<br />
/bin/bash<br />
/bin/dash</div>
<p>Notice how the command now has the additional parameter added to the original command. As expected, the full command is listed followed by the command output.</p>
<p>You can also do the standard manipulations such as piping the output, &#8220;!412 | grep wp-admin&#8221;, or redirecting the output, &#8220;!510 &gt; shell_locations.txt&#8221;.</p>
<h4>Searching and Running Previous Commands</h4>
<p>The &#8220;!&#8221; functionality of BASH also allows you to search through the history and execute the first match.</p>
<p>The basic form of this simply searches for matches at the beginning of the command. &#8220;!find&#8221; executes the most recent command that begins with &#8220;find&#8221;.</p>
<p>You can also search the entire command for a match. &#8220;!?link?&#8221; executes the most recent command that contains &#8220;link&#8221;.</p>
<h3>Advanced History with Regular Expressions</h3>
<p>As with many tools in the Linux world, you can attain great power by combining the tool with <a href="http://www.regular-expressions.info/" target="_blank">regular expressions</a>. The history in BASH allows you to run previous commands with a search and replace in Regex fashion.</p>
<p>Let&#8217;s take my searching through the ~/wordpress example, command number 412, and run it again with some modifications. This time, instead of searching ~/wordpress, I&#8217;d like to search through /home/site/public_hmtl/wordpress. I&#8217;d do that with the following:</p>
<div class="code">user:~$ <strong>!412:s|~/wordpress|/home/site/public_html/wordpress|</strong><br />
find /home/site/public_html/wordpress -type f | grep \.php$ | grep link | sort -r<br />
/home/site/public_html/wordpress/wp-links-opml.php<br />
/home/site/public_html/wordpress/wp-includes/link-template.php<br />
/home/site/public_html/wordpress/wp-admin/update-links.php<br />
/home/site/public_html/wordpress/wp-admin/options-permalink.php<br />
/home/site/public_html/wordpress/wp-admin/link.php<br />
/home/site/public_html/wordpress/wp-admin/link-parse-opml.php<br />
/home/site/public_html/wordpress/wp-admin/link-manager.php<br />
/home/site/public_html/wordpress/wp-admin/link-category.php<br />
/home/site/public_html/wordpress/wp-admin/link-add.php<br />
/home/site/public_html/wordpress/wp-admin/edit-link-form.php<br />
/home/site/public_html/wordpress/wp-admin/edit-link-category-form.php<br />
/home/site/public_html/wordpress/wp-admin/edit-link-categories.php</div>
<p>Note that the search and replace is done by adding &#8220;:s|find|replace|&#8221; to the end of the history command. I used the pipes, &#8220;|&#8221;, to deliminate the find and replace portions to make it easier to add the forward slashes, &#8220;/&#8221;, to the terms. You can also use &#8220;:s/find/replace/&#8221;.</p>
<h3>Closing Thoughts</h3>
<p>I hope that you enjoy my Mastering the Command Line series. If you have any command line-specific requests, please leave a comment, and I will see about creating a tutorial on that topic.</p>
<p>Remember, those who forget history, are doomed to repeat it by manual entry.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/03/09/command-line-history-in-ubuntu-terminal/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Convert DOS-Formatted Files to Unix-Format in Ubuntu and CentOS</title>
		<link>http://chrisjean.com/2009/03/08/convert-dos-formatted-files-to-unix-format-in-ubuntu-and-centos/</link>
		<comments>http://chrisjean.com/2009/03/08/convert-dos-formatted-files-to-unix-format-in-ubuntu-and-centos/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 06:00:37 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[CentOS]]></category>
		<category><![CDATA[DOS]]></category>
		<category><![CDATA[dos2unix]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[newline]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[unix2dos]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1297</guid>
		<description><![CDATA[Have you ever seen a bunch of ^M characters in a text file? This odd character at the end of a line can also be represented as a Ctrl+M or &#60;CTRL&#62;M. You don&#8217;t know what it is, and you want it to go away. Today, I&#8217;ll help you understand what that odd ^M character is, [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>Have you ever seen a bunch of ^M characters in a text file? This odd character at the end of a line can also be represented as a Ctrl+M or &lt;CTRL&gt;M. You don&#8217;t know what it is, and you want it to go away.</p>
<p>Today, I&#8217;ll help you understand what that odd ^M character is, why it is in some of your documents, and how to get rid of them.</p>
<p><span id="more-1297"></span></p>
<h3>All About the Newline</h3>
<p>In text documents, lines are separated by what is called a <a href="http://en.wikipedia.org/wiki/Newline" target="_blank">newlin</a>e (also known as a line break or end of line). Different operating systems have different character codes that represent this newline.</p>
<p>For example:</p>
<ul>
<li>DOS-based systems, including Windows, as well as a number of other, older non-Unix OSes use a carriage return (CR) character followed by a line feed (LF) character.</li>
<li>Commodore and the old Apple OSes before OS X used a CR character. Since OS X+ is based partly on BSD, which is in turn based on Unix, the new Apple OSes use the Unix newline method described below.</li>
<li>Unix and its derivatives (Linux, BSD, and others) all use the single LF character to represent a newline.</li>
</ul>
<p>So what does all of this mean to you? It means that text documents that come from a Windows system won&#8217;t always play nice in Linux. The converse is true however. If you create a text file in Linux, many programs will fail to recognize the single LF as a newline and will render the document without any line breaks.</p>
<h3>dos2unix and unix2dos to the Rescue</h3>
<p>Fortunately, there are a couple of very easy to use programs that make dealing with this file format mess much easier. They are dos2unix and unix2dos.</p>
<p>These programs basically do exactly what their name implies: dos2unix takes a file and converts all DOS-style newlines to Unix-style newlines. unix2dos takes a file and converts all Unix-style newlines to DOS-stlye newlines.</p>
<p>I put Ubuntu and CentOS in the title because I&#8217;m going to give instructions for installing these programs on each of these distros. Why just these two? They are the two that I work with most often and are representative of the lion&#8217;s share of what people are using these days. If you need help with a different distro, please let me know in a comment.</p>
<h3>Installing dos2unix and unix2dos in Ubuntu</h3>
<p>There aren&#8217;t any dos2unix or unix2dos packages that can be found in Synaptic; howver, there is a packages that will install them for you. Simply open up Synaptic and install the tofrodos package. If you are like me and prefer to do this from the command line, you can run the following command:</p>
<div class="code">sudo apt-get install tofrodos</div>
<p>That&#8217;s all there is to it. Not only will dos2unix and unix2dos install, but alias programs fromdos and todos will be installed as well. These additional programs work in the same manner, so it&#8217;s purely a matter of preference which ones you use.</p>
<h3>Installing dos2unix and unix2dos in CentOS</h3>
<p>I really thought that I had to install these in CentOS, but amazingly, the programs are already installed by default. I tested this in a Virtual Machine fresh install, and the programs were there on the first boot.</p>
<p>So, CentOS users, you&#8217;re already good to go.</p>
<h3>Using dos2unix</h3>
<p>Fortunately, using these programs couldn&#8217;t be easier.</p>
<p>Let&#8217;s say that you are in a Terminal (Applications &gt; Accessories &gt; Terminal) viewing text files. Maybe you just downloaded a new WordPress plugin and you are reading the readme.txt file. It doesn&#8217;t really matter. However, there is a problem. The readme.txt file has a bunch of ^M characters at the end of each line, and it&#8217;s really distracting.</p>
<p>Simply exit out of the editor you are currently in, since the file will be modified, and run the following command:</p>
<div class="code">dos2unix readme.txt</div>
<p>If there are multiple files, you can specify each one with a space separating each. For example:</p>
<div class="code">dos2unix readme.txt install.txt *.php</div>
<p>The program doesn&#8217;t produce any output. Simply reopen your text file and look at all the beautiful non-existant ^M&#8217;s.</p>
<h3>Using unix2dos</h3>
<p>The unix2dos program has the exact same syntax as dos2unix. However, I thought it might be helpful to describe a situation in which you might need to use it.</p>
<p>You&#8217;re working on a project. You&#8217;ve just sent out your batch of files, and another member on the project complains that you are being a jerk and remove all the newlines in your text files. This other member is most likely using a Windows application that doesn&#8217;t understand the Unix newline format. Rather than getting into a format war, it&#8217;s typically better and quicker to simply convert your text files to a DOS/Windows format.</p>
<p>If you have a folder full of files that all need to be converted, simply run:</p>
<div class="code">unix2dos *</div>
<p>Now you can send these new files to your project group and hopefully avoid any more unproductive drama.</p>
<h3>File Formats in Vi</h3>
<p>If you happen to use Vi, you can change Vi back and forth between DOS and Unix modes for newlines with a simple command. &#8220;:set ff=dos&#8221; sets the editor to use DOS newline encoding and will save the file in a DOS-encoded format. &#8220;:set ff=unix&#8221; sets the editor use Unix newlines and will save the file in a Unix format.</p>
<p>Note that changing the format to dos from unix will always work as expected. This is because any file that contains just LF characters for new lines will be converted to CRLF while lines that already end in CRLF will be left as is.</p>
<p>If your Vi config defaults to a unix format and you open a DOS file, you will see the ^M characters. You can either use the dos2unix conversion utility first or change Vi first to the dos format and then to unix.</p>
<p>If you&#8217;d like Vi to default to DOS or Unix formating each time you start a new Vi session, you can add the setting to your ~/.vimrc file. In that file, either add &#8220;set ff=dos&#8221; or &#8220;set ff=unix&#8221; depending on your needs. Note the lack of the colon, :, in the .vimrc entries.</p>
<p>For more information on the ff or fileformat setting in Vi, check out the <a href="http://www.vim.org/htmldoc/options.html#%27fileformat%27" target="_blank">official documentation</a>.</p>
<h3>Closing Thoughts</h3>
<p>Maybe one day we won&#8217;t have to worry about these types of things. For now however, it&#8217;s good to know the tools that make these problems easily manageable.</p>
<p>BTW, <a href="http://en.wikipedia.org/wiki/Daylight_saving_time" target="_blank">Happy Daylight Savings Day</a> everyone. I hope you enjoyed the loss of an hour of sleep. <img src='http://chrisjean.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/03/08/convert-dos-formatted-files-to-unix-format-in-ubuntu-and-centos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multitasking from the Linux Command Line + Process Prioritization</title>
		<link>http://chrisjean.com/2009/03/06/multitasking-from-the-linux-command-line-plus-process-prioritization/</link>
		<comments>http://chrisjean.com/2009/03/06/multitasking-from-the-linux-command-line-plus-process-prioritization/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 06:00:31 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[process]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1263</guid>
		<description><![CDATA[Did you know that you aren&#8217;t limited to working on one thing at a time while on a Linux command line? You can actually &#8220;minimize&#8221; a program that you are in, get back to the command line, and then return to the program whenever you&#8217;d like. When you run a program or script on the [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>Did you know that you aren&#8217;t limited to working on one thing at a time while on a Linux command line? You can actually &#8220;minimize&#8221; a program that you are in, get back to the command line, and then return to the program whenever you&#8217;d like.</p>
<p>When you run a program or script on the Linux command line (from now on referred to as the shell), you are creating a new job. For those that are used to GUI environments, each of these jobs is somewhat like a window on the desktop. Just as you can have multiple windows and switch between them, the shell is capable of managing multiple jobs and allows you to switch between them.</p>
<p>There is a lot to cover, so let&#8217;s start simple by describing what states a shell job can be in.</p>
<p><span id="more-1263"></span></p>
<h3>Jobs States</h3>
<h4>Foreground Jobs</h4>
<p>Up to now, you may have only worked with foreground jobs in the shell. Foreground jobs are ones that don&#8217;t return you to the command prompt until you exit out of the job or the job finishes.</p>
<p>If you run &#8220;<code>ping yahoo.com</code>&#8221; from the shell, the ping process will become a foreground job and will not return you to the command prompt until you stop it.</p>
<p>Just in case you didn&#8217;t know, you can stop the ping process by pressing <kbd>Ctrl+c</kbd>. Unlike in many applications that you may be used to, <kbd>Ctrl+c</kbd> does not mean &#8220;copy&#8221; in a Linux shell. Instead of copying text, this signals the foreground process to terminate by sending it the <a href="http://en.wikipedia.org/wiki/SIGINT_(POSIX)"><code>SIGINT</code></a> (Signal Interrupt) signal.</p>
<p>An example of a foreground job that won&#8217;t return you to the command prompt until it is finished is the <code>zip</code> command. Imagine that you have a large amount of files inside a folder called &#8220;stuff&#8221;. If I run &#8220;<code>zip -r stuff.zip stuff</code>&#8221; and don&#8217;t know about job management, I won&#8217;t have access to the shell until <code>zip</code> finishes its task and returns me to the prompt.</p>
<h4>Background Jobs</h4>
<p>Putting a job in the background allows the job to run exactly as if it were in the foreground except that it does not receive user input. If a command produces output and is put in the background, it will still produce the output.</p>
<p>Why would putting a job in the background be helpful then you may wonder. There are a number of reasons, and they will become clear in the examples below.</p>
<h4>Stopped Jobs</h4>
<p>There is technically a third state that a job can be in. Jobs that aren&#8217;t in the foreground yet are also not in the background have been stopped.</p>
<p>Like background jobs, stopped jobs do not receive user input. Unlike background jobs, stopped jobs do not produce output. As a matter of fact, they don&#8217;t do anything. They have essentially been put on hold and won&#8217;t do any processing until they are resumed or killed.</p>
<h3>Multitasking 101: Stopping and Resuming Processes</h3>
<p>It may seem like stopped jobs are the odd man out, but they are actually the key to getting started with multitasking on the Linux shell. I use stopped jobs every day, and I couldn&#8217;t imagine using the shell without them.</p>
<p>Imagine that you are in your favorite shell text editor working on a program or modifying config files, and you can&#8217;t remember what the IP address you need is but you could easily look it up with dig. You could easily open up another shell connection and run the command, but it would be quicker to simply stop the editor, run the command quickly, copy the text, and bring your editor to the foreground.</p>
<p>The following is a sequence that illustrates how this example would look on your shell.</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo vi /etc/httpd/conf/httpd.conf</span>
[sudo] password for chris:
<em style="color:white;">Ctrl+z</em>
[1]+  Stopped          sudo vi /etc/httpd/conf/httpd.conf
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">dig +short chrisjean.com</span>
96.125.165.12
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">fg %1</span></pre>
<p>After running these commands, you will be back in your editor.</p>
<p>The key to stopping a running job is the <kbd>Ctrl+z</kbd> key combination. Again, some of you may be used to <kbd>Ctrl+z</kbd> as the shortcut to undo, but in the Linux shell, <kbd>Ctrl+z</kbd> sends the <a href="http://en.wikipedia.org/wiki/SIGTSTP"><code>SIGTSTP</code></a> (Signal Tty SToP) signal to the foreground job. When you press this key combination, the running program will be stopped and you will be returned to the command prompt.</p>
<p>You may notice the &#8220;<code>%1</code>&#8221; after <code>fg</code>. The &#8220;1&#8243; is the job number that is to be brought to the foreground. The job number is listed in brackets when the job was stopped. These numbers are crucial when you have multiple jobs in stopped states. At any time, you can run &#8220;jobs&#8221; to get a list of the current jobs.</p>
<p>I stop jobs every day, and it greatly reduces the number of active shell sessions I have running. Sometimes I have stopped jobs that I set up ahead of time when I first access the shell. For example, I may open up a MySQL session, connect it to the database of the site I&#8217;m working on, and then stop it. Any time I need to run a query, I can load up the job by running &#8220;<code>fg 1</code>&#8220;, run the query, and then stop the job again.</p>
<h3>Multitasking 202: Running Jobs in the Background</h3>
<p>Stopping jobs is great, but what if we want to be returned to the command prompt while still allowing the job to run? Sending a job to the background will do exactly that.</p>
<p>As I talked about in my post about <a href="http://chrisjean.com/2009/03/04/4-great-tools-to-find-files-quickly-in-ubuntu/">finding files</a>, the locate command is extremely helpful. Sometimes you need to manually run the <code>updatedb</code> command in order for the locate database to update. The problem is that the <code>updatedb</code> command can take a very long time to run, sometimes it can take more than an hour.</p>
<p>I&#8217;ll use the example of the <code>updatedb</code> command to show how you can take a foreground process and put it in the background so that you can free up the shell.</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo updatedb</span>
[sudo] password for chris:
<em style="color:white;">Ctrl+z</em>
[1]+  Stopped            sudo updatedb
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">bg %1</span>
[1]+ sudo updatedb &amp;
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>There are a few items of note here.</p>
<ol>
<li>I&#8217;m back at the command prompt and can do anything I&#8217;d like while the job works merrily away in the background.</li>
<li>Notice how I used &#8220;%1&#8243; with bg just as I did with fg. Both commands accept a job number to tell it which job is to be sent to the foreground/background.</li>
<li>After running bg, a line similar to when I pressed Ctrl+z was printed showing the command and job number. However, there is also the addition of the &#8220;&amp;&#8221; at the end. We&#8217;ll get into the reason for that in the next section.</li>
</ol>
<p>When a process that is running in background finishes, you will receive a message the next time your command prompt refreshes. The prompt refreshes when you run another command or simply hit <kbd>Enter</kbd> to get a new prompt.</p>
<p>Continuing from the previous example, I receive output similar to the following when the <code>updatedb</code> command finishes:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span>
[1]+  Done              sudo updatedb
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>As with all the other job-related output, you will be notified of the job number and the command.</p>
<h3>Multitasking 303: Starting Jobs in the Background</h3>
<p>Now that you see the value of having jobs run in the background, you might wonder if you can just start the command in the background to begin with. In fact, you can!</p>
<p>Remember that odd ampersand &#8220;<code>&amp;</code>&#8221; after the command in the output of the <code>bg</code> command? The ampersand means that the job is running in the background. You can run a command with the ampersand at the end to automatically put the job in the background.</p>
<p>Going back to the <code>updatedb</code> example again, we could have simply run the following to have the job in the background and get our command prompt back immediately:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo date</span>
...
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo updatedb &amp;</span>
[sudo] password for chris:
[1] 15231
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>You might wonder what that completely unnecessary &#8220;<code>sudo date</code>&#8221; command is all about. Remember that when you send a job to the background, it cannot receive input from the user. Since <code>sudo</code> prompts for the password when it hasn&#8217;t been run recently, I ran a throwaway command before running the background <code>sudo</code> command to ensure that the password wouldn&#8217;t be prompted for. If you do send a <code>sudo</code> command to the background and <code>sudo</code> prompts for a password, you need to bring it to the foreground first to enter the password and then send it to the background again.</p>
<p>The output when sending the job directly to the background is different than running <code>bg</code>. Instead of telling you the command, it tells you the actual process ID. Every program that runs in Linux is a process and has a process ID. You can use these process IDs to kill a specific process, change the process&#8217; execution priority, and isolate the resource utilization by that process.</p>
<p>Even though you&#8217;ve put a task in the background, you can still bring it to the foreground using the <code>fg</code> command. In the previous example, I can pull the background process to the foreground by running &#8220;<code>fg %1</code>&#8220;.</p>
<h3>Multitasking Graduate School: Killing and Prioritizing Jobs</h3>
<p>Now that you know the fundamentals of managing jobs, it&#8217;s time to get more advanced and tell you how to terminate jobs and modify their execution priority.</p>
<h4>Terminating Jobs</h4>
<p>Sometimes a process goes rogue and either won&#8217;t respond to input, is consuming massive amounts of resources, or both. At times like this, it&#8217;s very important to know how to kill a job which will cease the program&#8217;s execution and free up its used resources.</p>
<p>The command used to kill processes is appropriately called <code>kill</code>. The kill command can accept either process IDs or job numbers (the job number must be preceded by a percent sign, &#8220;%&#8221;).</p>
<p>If my <code>updatedb</code> job from before fails to respond, the job has a job number of &#8220;1&#8243;, and I want to tell the job to stop, I could run the following command:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo kill %1</span>
[sudo] password for chris:
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>Notice that there isn&#8217;t any confirmation that the kill command was successful. You can check to ensure that the job has been terminated by running the &#8220;<code>jobs</code>&#8221; command and looking for that job&#8217;s listing.</p>
<p>By default, the <code>kill</code> command sends a <a href="http://en.wikipedia.org/wiki/SIGTERM"><code>SIGTERM</code></a> (Signal Terminate) signal to the process; however, sometimes this is not enough to stop the execution of an out of control process. To shut down these processes, we need something more powerful.</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo kill -9 %1</span>
[sudo] password for chris:
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>This may not look much different, but the addition of the &#8220;-9&#8243; option tells the command that things are serious now. This option causes a <a href="http://en.wikipedia.org/wiki/SIGKILL"><code>SIGKILL</code></a> (Signal Kill) signal to be sent to the process. A kill signal cannot be intercepted by a process and causes the process to immediately terminate without allowing it to clean up after itself. It&#8217;s for this reason that you should only use <code>SIGKILL</code> after first sending the process a <code>SIGTERM</code>.</p>
<p>As mentioned before, you can also run a <code>kill</code> command on a process ID rather than a job number. If we wished to kill a process with a process ID of 12267, we can run the following command:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo kill 12267</span>
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>Note that you don&#8217;t have to always run the <code>kill</code> command with <code>sudo</code>, you just have to ensure that whatever privilege level you are running the <code>kill</code> command at has the ability to control that process. In other words, if you are trying to kill a process run with <code>sudo</code>, you&#8217;ll need to use <code>sudo</code> to kill it as well.</p>
<h4>Prioritizing Jobs</h4>
<p>Linux does a great job of prioritizing tasks automatically; however, what Linux thinks is best isn&#8217;t always what you want. Basically, Linux always wants to make a process finish as soon as possible, which means that you get a nice bit of lag when you try to do your work as a CPU or disk hungry process churns away. Fortunately, there are tools that will let you tell Linux what you want.</p>
<p>First, let&#8217;s deal with managing a process&#8217; use of CPU time. If you are going to run a command that will eat up large amounts of CPU cycles and you want to make sure that this process doesn&#8217;t keep you from doing your work, you can run the command with <code>nice</code> to give the command a lower-than-default priority.</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo date</span>
[sudo] password for chris:
Fri Mar  6 10:55:11 CST 2009
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo nice updatedb &amp;</span>
[1] 16312
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>As before, a junk sudo command was run first to take care of the password prompt.</p>
<p>Running the command in <code>nice</code> lowers the processes CPU priority and will result in smoother CPU performance for your other applications.</p>
<p>What if you already ran a command whose process is now greedily chewing away at your CPU resources? That&#8217;s where the <code>renice</code> command comes into play.</p>
<p>With <code>renice</code>, you can change the CPU priority of an existing process.</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo date</span>
[sudo] password for chris:
Fri Mar  6 10:56:25 CST 2009
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo updatedb &amp;</span>
[1] 16324
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo renice 10 16324</span>
16324: old priority 0, new priority 10
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>The <code>renice</code> command accepts a priority as the first argument and the process ID as the second argument. The priority argument has a range from -20 to 19 where -20 is the highest priority and 19 is the lowest. In other words, a higher numeric value priority results in lower CPU priority.</p>
<p>That takes care of the CPU, but admittedly, the <code>updatedb</code> command does more damage to the IO or disk traffic than it does to the processor. How can we reign in a process&#8217; use of the disk so that performance for the rest of the system improves? For that, we use the <code>ionice</code> command.</p>
<p>With the <code>ionice</code> command, you can set the IO priority for a process to one of three classes: Idle (3), Best Effort (2), and Real Time (1). The Idle class means that the process will only be able to read and write to the disk when all other processes are not using the disk. The Best Effort class is the default and has eight different priority levels from 0 (top priority) to 7 (lowest priority). The Real Time class results in the process having first access to the disk irregardless of other process and should never be used unless you know what you are doing.</p>
<p>If we wish to run the <code>updatedb</code> process in the background with an Idle IO class priority, we can run the following:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo date</span>
[sudo] password for chris:
Fri Mar  6 11:02:43 CST 2009
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo updatedb &amp;</span>
[1] 16324
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo ionice -c3 -p16324</span>
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>If we&#8217;d rather just lower the Best Effort class priority (defaults to 4) for the command so the process isn&#8217;t limited to idle IO periods, we can run the following:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo date</span>
[sudo] password for chris:
Fri Mar  6 11:07:12 CST 2009
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo updatedb &amp;</span>
[1] 16324
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">sudo ionice -c2 -n7 -p16324</span>
<span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;"></span></pre>
<p>Again, the Real Time class should not be used as it can prevent you from being able to interact with your system.</p>
<p>You may wonder where you can get the process ID if you don&#8217;t know it, can&#8217;t remember it, or didn&#8217;t start the process (an automated script may have launched it). You can find process IDs with the <code>ps</code> command.</p>
<p>For example, if I had an updatedb program running in the background, and I wanted to find its process ID, I can run the following:</p>
<pre class="terminal"><span style="color:#8FED99;">[<span style="color:#BBFF33;">chris@local</span> <span style="color:#729FCF;">~</span>]$</span> <span style="color:#FFF;">ps -C updatedb</span>
PID TTY          TIME CMD
4234 ?        00:00:42 updatedb</pre>
<p>This tells me that the process&#8217; process ID (PID) is 4234.</p>
<p>Another useful command is &#8220;<code>ps -u <em>user</em></code>&#8221; (replace &#8220;<code>user</code>&#8221; with your username) which lists all processes your user has access to. Basically, these are processes that your user or some process using your user&#8217;s permissions started.</p>
<p>Another really helpful use of <code>ps</code> is &#8220;<code>ps aux</code>&#8220;. This command will list all the system&#8217;s processes, which user owns the process, and even detail the resource usage of the process. This is very helpful for finding processes that you don&#8217;t know enough about to find using other methods.</p>
<h3>Mutitasking Doctorate: Using Job Management in Scripts</h3>
<p>Keep in mind that many scripts execute from inside shell environments. This means that you can manage the script&#8217;s job using the tools I&#8217;ve given you here.</p>
<p>If you look in the <code>/etc/cron.daily</code> folder, you&#8217;ll find many scripts that execute once a day. These scripts can create a huge load on a system while they execute. If around the clock performance is crucial, you can prioritize the shells that the scripts run in which causes all the commands run inside that shell to follow the same prioritization rules.</p>
<p>This all sounds very complex, but it&#8217;s actually very simple.</p>
<p>Let&#8217;s open up the <code>/etc/cron.daily/mlocate</code> file (pick another file if you don&#8217;t have this one). At the top, you should see a line like the following:</p>
<div class="code">#!/bin/bash</div>
<p>This <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line tells Linux what interpreter is used to execute the script. My example script uses the bash shell as the interpreter. Other scripts may use the sh shell instead of the bash shell, but that won&#8217;t make any difference.</p>
<p>Below the first line, put the following:</p>
<div class="code">renice 10 $$<br />
ionice -c3 -p$$</div>
<p>The $$ variable is a special variable that contains the shell&#8217;s own process ID. The two commands above cause the CPU priority to lower to 10 and changes the IO priority to class 3 (Idle).</p>
<p>Add this type of customization to your resource hungry scripts, and your system will perform more smoothly around the clock. Keep in mind that this also can result in the scripts taking much longer to complete.</p>
<p>You can use those same commands above using the <code>$$</code> variable to change the CPU and IO priority of shells that you open in order to lower the impact you have while working on the system. This can be a smart thing to do if you need to compile software or run other intensive processes on production machines.</p>
<h3>Important Commands</h3>
<p>Here&#8217;s a list of all the commands, key combos, and other important bits that I went over with a brief description for each. It&#8217;s like the word list at the end of children&#8217;s books.</p>
<ul>
<li><code>fg</code> &#8211; Brings a stopped or background job to the foreground</li>
<li><code>bg</code> &#8211; Sends a stopped job to the background</li>
<li><code>jobs</code> &#8211; Lists the current jobs and their corresponding job number</li>
<li><code>Ctrl+c</code> &#8211; Key combination that sends a <code>SIGINT</code> signal to the foreground process. Use this to close processes that don&#8217;t stop (such as ping or top) or terminate a process that no longer responds or is no longer needed.</li>
<li><code>Ctrl+z</code> &#8211; Key combination that sends the <code>SIGTSTP</code> signal to the foreground process. Use this to send a foreground process to a stopped state.</li>
<li><code>&amp;</code> &#8211; Use an ampersand at the end of a command to send the resulting job to the background immediately</li>
<li><code>kill</code> &#8211; Sends signals to processes based on process ID or job number. Useful to send <code>SIGTERM</code> or <code>SIGKILL</code> signals to forcibly stop processes.</li>
<li><code>nice</code> &#8211; Runs the given command with a lower-than-default CPU priority</li>
<li><code>renice</code> &#8211; Changes an existing process&#8217; CPU priority</li>
<li><code>ionice</code> &#8211; Changes an existing process&#8217; IO priority</li>
</ul>
<h3>Closing Thoughts</h3>
<p>Frankly, I didn&#8217;t expect to make such a long post. If this post has helped you, please leave a comment and let me know what you thought.</p>
<p>I&#8217;d greatly appreciate it if you&#8217;d share the post by using one of the icons below. Or you can <a href="http://digg.com/submit?url=http%3A%2F%2Fgaarai.com%2F2009%2F03%2F06%2Fmultitasking-from-the-linux-command-line-plus-process-prioritization%2F&amp;title=Multitasking+from+the+Linux+Command+Line+%2B+Process+Prioritization">click here</a> to Digg this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/03/06/multitasking-from-the-linux-command-line-plus-process-prioritization/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>4 Great Tools to Find Files Quickly in Ubuntu</title>
		<link>http://chrisjean.com/2009/03/04/4-great-tools-to-find-files-quickly-in-ubuntu/</link>
		<comments>http://chrisjean.com/2009/03/04/4-great-tools-to-find-files-quickly-in-ubuntu/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 06:00:32 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[locate]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[whereis]]></category>
		<category><![CDATA[which]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1225</guid>
		<description><![CDATA[Many of you fellow Ubuntu users will be familiar with the &#8220;Search for Files&#8221; tool that allows you to look for files. As is true with most things in Linux, there are great desktop tools, but more power can be found in Terminal than any streamlined desktop tool can match. Today, I&#8217;d like to introduce [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>Many of you fellow Ubuntu users will be familiar with the &#8220;Search for Files&#8221; tool that allows you to look for files. As is true with most things in Linux, there are great desktop tools, but more power can be found in Terminal than any streamlined desktop tool can match.</p>
<p>Today, I&#8217;d like to introduce you to a few tools that can turn a chore of finding files into an easy process.</p>
<p><span id="more-1225"></span></p>
<h3>locate</h3>
<p>The first tool that you should become comfortable with is also one of the simplest. The locate program works in a similar fashion to most graphical search tools.</p>
<h4>Using</h4>
<p>Let&#8217;s say that you&#8217;ve read up on how to modify <a href="http://www.x.org/" target="_blank">X</a> to provide enhanced video performance. The document that you are reading says to modify the xorg.conf file, but the document doesn&#8217;t say where that is and you don&#8217;t know how to find it. You can use locate to find it for you quickly:</p>
<div class="code">locate xorg.conf</div>
<p>Most likely, your system will return a number of results. For example, on one of my systems, I receive the following output:</p>
<div class="code">/etc/X11/xorg.conf<br />
/usr/share/man/man5/xorg.conf.5.gz<br />
/var/lib/x11/xorg.conf.md5sum<br />
/var/lib/x11/xorg.conf.roster</div>
<p>It&#8217;s clear that /etc/X11/xorg.conf is the file we&#8217;re looking for, but what if dozens or even hundreds of results were returned? How could you manage all of that? If you know a little bit about <a href="http://www.regular-expressions.info/" target="_blank">regular expressions</a> (every Linux user really should learn <a href="http://www.regular-expressions.info/tutorial.html" target="_blank">the basics</a>), you can easily use regular expressions to search for directories or files. For example, we can search for an exact match for xorg.conf with the following command:</p>
<pre style="padding-left:20px;">locate -r '/xorg.conf$'</pre>
<p>If you want to look for files without case sensitivity, you can use the &#8216;-i&#8217; option. For example:</p>
<pre style="padding-left:20px;">locate -i 'monthly report'</pre>
<p>Something that may confuse you when you first start using locate is that it not only returns matches, but if the match is a directory, it also lists all of the directories and files under that directory. To stop this behavior, use the &#8216;-b&#8217; option. For example:</p>
<div class="code">locate -b Desktop</div>
<p>This will return a much shorter list of results than if you did not include the &#8216;-b&#8217; option and all the matches will contain the search term as the final portion of the path.</p>
<h4>Important Note</h4>
<p>The locate command relies on a slocate database to function. This database caches the file system directory and file names and allows the locate command to operate in moments rather than taking long periods of time to search the entire file system.</p>
<p>This ability doesn&#8217;t come without cost though. By default, your system will run a command to update this database once a day at 6:25am. This command will update the database with any changes made to the file system since the last update.</p>
<p>If you&#8217;ve made changes recently, such as added or removed large amounts of files and directories, and you want to use the locate command to search through these new directories and files, you can manually update the slocate database at any time with the following command:</p>
<div class="code">sudo updatedb</div>
<p>Depending on the volume of changes made since the slocate database was last updated, this could take a few seconds to a few minutes to update. Once it&#8217;s done, you&#8217;re ready to search again with locate.</p>
<h3>which</h3>
<p>locate is great for searching the entire file system. However, sometimes you just want to find where a command lives. That&#8217;s where which comes into play.</p>
<p>which only does one thing, but it does it well. Given the name of a command, which will tell you where the file for that command resides in the file system.</p>
<p>Let&#8217;s imagine that we want to find out where there firefox command resides. We can simply run the following to find out:</p>
<div class="code">which firefox</div>
<p>On my system, and most likely on your&#8217;s as well, which will tell us that it is located at /usr/bin/firefox.</p>
<p>Why would you ever want to search specifically for a command? Imagine that you have some code that needs to replace an old command. You really need to remove the the old one (I usually rename it to the same name followed by &#8220;.bak&#8221; to keep it around in case I need it) first before putting the new one in its place. which will help you find the file quickly and easily so you don&#8217;t have to search everywhere to find it.</p>
<h3>whereis</h3>
<p>The whereis command is similar to which but goes one step further. Not only does it tell you where the executable file is located at, but it will also locate the source, man page, and other associated directories as well.</p>
<p>Keeping with the which example, let&#8217;s ask whereis about the firefox command:</p>
<div class="code">whereis firefox</div>
<p>On my system, I get the following output:</p>
<div class="code">firefox: /usr/bin/firefox /usr/lib/firefox /usr/lib64/firefox /usr/share/firefox</div>
<p>This helped me find /usr/lib/firefox/plugins earlier when working on the solution for Flash. That location is where the global add-ons for Firefox are stored.</p>
<h3>find</h3>
<p>find is the ultimate search tool, the Swiss Army knife of Linux search if you will. find can do everything from search for files based on last modified times, owners and groups, permissions, type (file, directory, symbolic link, etc), whether the file is readable/writable/executable by the current user, file size, whether the file or directory is empty, and much more. You can also search for matches by regular expression.</p>
<p>When you use this tool in combination with the xargs command (which I&#8217;ll have to cover some other time), you can do truly amazing things on the command line. Imagine being able to recursively search through the current directory for files ending with &#8220;.bak&#8221; that were last modified more than 100 days ago and then deleting them with one command:</p>
<pre style="padding-left:20px;">find . -name '*.bak' -type f -mtime +100 -print0 | xargs -0 /bin/rm -f</pre>
<p>It will look very complex at first, but when you master the art of using find, it will become second nature. Soon, you&#8217;ll wonder how you ever searched for things without it.</p>
<p>To be frank, there is much more to find than I am willing to cover and there are many, many great introductions to the tool that do a better job than I would. So, I&#8217;ll simply point you to three great references that can be used to get going with find.</p>
<ul>
<li><a href="http://linux.about.com/od/commands/a/blcmdl1_findx.htm" target="_blank">Example uses of the Linux Command find</a></li>
<li>(I apologize for the horrible colors in advance) <a href="http://content.hccfl.edu/pollock/Unix/FindCmd.htm" target="_blank">Unix/Linux &#8220;find&#8221; Command Tutorial</a></li>
<li><a href="http://www.linux.ie/newusers/beginners-linux-guide/find.php" target="_blank">Using the find command</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/03/04/4-great-tools-to-find-files-quickly-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Change Timezone in CentOS</title>
		<link>http://chrisjean.com/2009/03/03/change-timezone-in-centos/</link>
		<comments>http://chrisjean.com/2009/03/03/change-timezone-in-centos/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 06:00:52 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[CentOS]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[Timezone]]></category>
		<category><![CDATA[zoneinfo]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1228</guid>
		<description><![CDATA[You just got your new CentOS dedicated server, and you notice that times in your logs aren&#8217;t quite right. You check the time from the command line (run &#8220;date&#8221;), and find that the timezone is set to US Eastern or some other timezone. How do you get this changed? Unfortunately, this is not an easy [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>You just got your new CentOS dedicated server, and you notice that times in your logs aren&#8217;t quite right. You check the time from the command line (run &#8220;date&#8221;), and find that the timezone is set to US Eastern or some other timezone. How do you get this changed?</p>
<p>Unfortunately, this is not an easy thing to figure out. Fortunately though, it&#8217;s not hard to do with the right directions.</p>
<p><span id="more-1228"></span></p>
<p><em>Please note that you have to have root access to be able to make the changes detailed below.</em></p>
<p>There are a series of time zone files located at /usr/share/zoneinfo. Select the appropriate named timezone for your location. For my location, Oklahoma, USA, I actually have two that I can select from: America/Chicago or US/Central. Make note of the appropriate folder and file for your timezone.</p>
<p>The active timezone used on your system is in the /etc/localtime file. The default will vary depending on your server host but often seems to be EST or EDT (depending on the time of year you are checking). We simply need to replace this file with the file we selected in the previous step.</p>
<p>Now, I say replace, but I actually recommend creating a link to the pertinent file rather than actually making a copy. Let me explain the reasoning for this quickly.</p>
<p>A crucial part of the timezone shift calculations is the daylight savings calculations. Many people don&#8217;t realize this, but the daylight savings days were changed in 2007. When this change happened, all servers needed to be updated with new zoneinfo files.</p>
<p>If your server has an automated process to update these files when daylight savings calculations change, your /usr/share/zoneinfo files will be updated but the /etc/localtime file will not be. So, if you simply made a copy of the file, you&#8217;ll have to know when these updates come out and manually copy the file over. If you create a link, everything will take care of itself.</p>
<p>Enough of the banter. On with the show.</p>
<p>First, make a backup of the existing localtime file. It&#8217;s always good practice to make backups of original config files.</p>
<div class="code">mv /etc/localtime /etc/localtime.bak</div>
<p>Next, create the link:</p>
<div class="code">ln -s /usr/share/zoneinfo/<strong>America/Chicago</strong> /etc/localtime</div>
<p>Make sure to replace &#8220;America/Chicago&#8221; with the directory (if your zone has one) and filename of the timezone you wish to use.</p>
<p>Now you just need to test your change. Run &#8220;date&#8221; from the command line, and ensure that the appropriate time, date, and timezone are reported.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/03/03/change-timezone-in-centos/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Using Vi/Vim as a Command Line Editor</title>
		<link>http://chrisjean.com/2009/02/27/using-vi-vim-as-a-command-line-editor/</link>
		<comments>http://chrisjean.com/2009/02/27/using-vi-vim-as-a-command-line-editor/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 17:10:40 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[Vi]]></category>
		<category><![CDATA[VIM]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1207</guid>
		<description><![CDATA[In many of my Tips &#8216;n Tricks or tutorial posts, I have commands that involve editing/creating files in Vi. For those that have never used Vi, it can be a daunting experience trying Vi for the first time. There are many holy wars fought over the best editor to use from the *nix command line. [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>In many of my <a href="http://chrisjean.com/category/tips-n-tricks/" target="_blank">Tips &#8216;n Tricks</a> or tutorial posts, I have commands that involve editing/creating files in Vi. For those that have never used Vi, it can be a daunting experience trying Vi for the first time.</p>
<p>There are many holy wars fought over the best editor to use from the *nix command line. I won&#8217;t claim that Vi is the best; rather, Vi is simply what I&#8217;m used to, it&#8217;s powerful, and it is always available on servers that I work on.</p>
<p>In order to best help those that don&#8217;t know how to use Vi but are interested in learning, this will serve as a very brief tutorial on how to get started.</p>
<p><span id="more-1207"></span></p>
<h3>Vi and Vim</h3>
<p>The <a href="http://en.wikipedia.org/wiki/Vi" target="_blank">original Vi</a> first saw development in 1976. Thus, it&#8217;s an editor that is more than thirty years old. It is older than Linux, and it is older than me. The fact that it is still so prevalent, is testament to the power and smart design that can be found in Vi. That said, I don&#8217;t technically use Vi; rather, I use <a href="http://www.vim.org/" target="_blank">Vim</a>: Vi IMproved.</p>
<p>Vim has <a href="http://en.wikipedia.org/wiki/Vim_(text_editor)#Features_and_improvements_over_vi" target="_blank">many features</a> that extend the original functionality of Vi. By far, my two favorite features are unlimited undo (Vi only supports undo last command) and syntax highlighting. When you see images on my site that have syntax highlighted code, those are screenshots of Vim running in Terminal.</p>
<p>Many recent distros have Vim installed by default. In addition, most distros that I&#8217;ve worked with also have Vim in place of Vi. You can check this by running &#8220;vi&#8221; from the command line. If Vim is running in place of Vi, you&#8217;ll see a greeting screen that says &#8220;VIM &#8211; Vi IMproved&#8221; at the top.</p>
<h3>Installing Vim</h3>
<p>If you run &#8220;vim&#8221; from the command line and receive an error such as &#8220;-bash: vim: command not found&#8221;, then you don&#8217;t have Vim. Since features that Vim has are greatly beneficial, I recommend installing it if you don&#8217;t have it. In addition to the advanced features, Vim has enhanced arrow key support which most new users will appreciate as they get accustomed to the unintuitive nature of Vim.</p>
<p>Vim can be installed quickly using your distro&#8217;s package manger. If you are using Ubuntu or another APT distro, you can simply run &#8220;sudo apt-get install vim&#8221;. On CentOS and other RPM-based distros, gain access to a root shell and run &#8220;yum install vim&#8221;.</p>
<p>Once you have Vim installed, you will probably want to use Vim whenever you type &#8220;vi file&#8221;. You can do this easily by setting up an alias. Modify the /etc/bashrc file with whatever editor you are currently comfortable with and add the following line:</p>
<div class="code">alias vi=&#8217;vim&#8217;</div>
<p>Save the file and exit the editor. Once you log into your shell again, this alias will be in place and all commands to vi will be routed to vim.</p>
<h3>Getting Started With Vi</h3>
<p>As I&#8217;m sure you&#8217;ve seen many times before, you can open a file for editing in Vi by running &#8220;vi filename&#8221;. This will indeed load the file for editing, but how do we actually modify the file?</p>
<h4>Modifying Text</h4>
<p>Vi is a modal editor. This means that the mode you are in when you first open the file is not an editing mode; rather, it&#8217;s more of a command mode where you can issue commands to Vi. In order to actually edit the file, you have to enter an editing mode.</p>
<p>Most of the time, the insert edit mode will be used. To enter the insert edit mode, simply press the &#8216;i&#8217; key. You&#8217;ll notice that the last line of the interface will now display &#8220;&#8211; INSERT &#8211;&#8221; to indicate that we&#8217;ve changed into the insert mode.</p>
<p>Now that you are in the insert mode, you can simply delete and insert text as if you were in a normal editor. Once you are done making changes, hit the ESC key to exit out of the insert mode and return to the command mode, commonly referred to as &#8220;normal mode&#8221;.</p>
<h4>Saving the File</h4>
<p>Now that the file has been modified, we need to save our changes.</p>
<p>In order to save the file, you first need to be in normal mode (press ESC). You can then save the file by using the &#8216;:w&#8217; command. This means that you need to press the &#8216;:&#8217; key, the &#8216;w&#8217; key, and Enter.</p>
<p>This command simply tells Vi to write the contents to the file.</p>
<p>If you wish to save the contents as a different file, give the write file command a file name. For example: &#8220;:w filename&#8221;.</p>
<h4>Exiting Vi</h4>
<p>Okay, we&#8217;ve opened a file, modified it, and saved it. Now we just need to figure out how we get out of Vi.</p>
<p>Similar to the write command, we just need to issue the quit command. This is done with &#8216;:q&#8217;.</p>
<p>Since Vi supports chaining commands, you can save the file and exit Vi with the &#8216;:wq&#8217; command. Notice that &#8216;:qw&#8217; will result in &#8220;E492: not an editor command: qw&#8221;. So, make sure that you put commands together in the correct order. Another command, &#8216;:x&#8217;, does the same thing as &#8216;:wq&#8217;.</p>
<h3>More than Basic Editing</h3>
<p>Frankly, this is just scratching the surface of what Vi is and how to use it. It will be enough to get most people started however.</p>
<p>I will revist the topic of Vi to give more details of how to do advanced uses and how to configure options to get the most out of it. For now, I have some links if you&#8217;d like to delve into the topic deeper.</p>
<ul>
<li><a href="http://www.vim.org/docs.php" target="_blank">Vim Documentation</a></li>
<li><a href="http://www.viemu.com/a-why-vi-vim.html" target="_blank">Introduction to Advanced Vi</a></li>
<li><a href="http://www.viemu.com/vi-vim-cheat-sheet.gif" target="_blank">Vi Cheatsheet</a></li>
<li><a href="http://thomer.com/vi/vi.html" target="_blank">Vi Lovers&#8217; Homepage</a></li>
<li><a href="http://jmcpherson.org/editing.html" target="_blank">Efficient Editing with Vim</a></li>
<li><a href="http://unix.t-a-y-l-o-r.com/VBsr.html" target="_blank">Search and Replace in Vi</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/02/27/using-vi-vim-as-a-command-line-editor/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unzip Multiple Files from Linux Command Line</title>
		<link>http://chrisjean.com/2009/02/26/unzip-multiple-files-from-linux-command-line/</link>
		<comments>http://chrisjean.com/2009/02/26/unzip-multiple-files-from-linux-command-line/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 06:00:45 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[unzip]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1196</guid>
		<description><![CDATA[Here&#8217;s a quick tip that will help you work with multiple zip files on the command line. If you are in a folder and have three zip files in it (a.zip, b.zip, c.zip) that you want to unzip, &#8220;no problem,&#8221; you think, &#8220;I can take care of that with one command.&#8221; So, you quickly run [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>Here&#8217;s a quick tip that will help you work with multiple zip files on the command line.</p>
<p>If you are in a folder and have three zip files in it (a.zip, b.zip, c.zip) that you want to unzip, &#8220;no problem,&#8221; you think, &#8220;I can take care of that with one command.&#8221; So, you quickly run the following:</p>
<div class="code">unzip *.zip</div>
<p>However, rather than having the unzip program nicely unzip each file one after another, you receive the following:</p>
<div class="code">Archive:  a.zip<br />
caution: filename not matched:  b.zip<br />
caution: filename not matched:  c.zip</div>
<p>I&#8217;m sure that this is not what you were expecting. I know I certainly wasn&#8217;t expecting this when I first tried it. However, this problem can help us understand more of how the command line works.<br />
<span id="more-1196"></span></p>
<h3>The Problem</h3>
<p>Whenever you use a wildcard (*), the shell itself will expand that and pass the results to the program rather than the program handling the expansion itself. That means that our previous command was actually expanded to the following before being executed:</p>
<div class="code">unzip a.zip b.zip c.zip</div>
<p>Again, this may not look odd since other programs (such as mkdir, chmod, etc) can take one or more arguments and repeat the process for each. However, unzip is different and actually has use for the additional arguments. If you specify additional arguments after the zip file name, unzip will try to only extract those specific files from the archive rather than all the files.</p>
<p>The previous command told unzip that it should extract b.zip and c.zip from inside the a.zip archive. Since those files don&#8217;t exist inside a.zip, we get the nasty output seen earlier.</p>
<p>You might think that you will have to manually unzip each archive one at a time, but you&#8217;d be wrong.</p>
<h3>The Solution</h3>
<p>Just because the shell expands out wildcard characters automatically doesn&#8217;t mean that programs can&#8217;t as well. The simple solution to this problem is to quote the argument to prevent the shell from interpreting it:</p>
<div class="code">unzip &#8216;*.zip&#8217;</div>
<p>That&#8217;s all there is to it. I&#8217;ve seen solutions from coding loops in bash script to running find combined with xargs, but none of that is necessary. Simply let unzip itself take care of things for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/02/26/unzip-multiple-files-from-linux-command-line/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>SSH Tutorial for Ubuntu Linux</title>
		<link>http://chrisjean.com/2009/02/19/ssh-tutorial-for-ubuntu-linux/</link>
		<comments>http://chrisjean.com/2009/02/19/ssh-tutorial-for-ubuntu-linux/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 06:00:24 +0000</pubDate>
		<dc:creator>Chris Jean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips 'n Tricks]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Mastering The Command Line]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://gaarai.com/?p=1097</guid>
		<description><![CDATA[I touched on this topic in my First Day with Ubuntu at the Office post under Accessing Remove File Systems; however, I thought that this deserved its own topic. For those that don&#8217;t know SSH stands for Secure SHell. In very non-technical terms, SSH allows you to access a system running a SSH server over [...]]]></description>
			<content:encoded><![CDATA[<!-- filtered -->
<p>I touched on this topic in my <a href="http://chrisjean.com/2009/01/03/first-day-with-ubuntu-at-the-office/" target="_blank">First Day with Ubuntu at the Office</a> post under <strong>Accessing Remove File Systems</strong>; however, I thought that this deserved its own topic.</p>
<p>For those that don&#8217;t know <a href="http://en.wikipedia.org/wiki/Secure_Shell" target="_blank">SSH</a> stands for Secure SHell. In very non-technical terms, SSH allows you to access a system running a SSH server over a network. This other system can be another computer in your home or a remote system on the other side of the planet. SSH will allow you to connect to that other system and communicate with it securely. All the data passed back and forth is encrypted, so you don&#8217;t have to worry about people sniffing your passwords or valuable data.</p>
<p>You may be asking what all of that means to you. What it means is that you can connect to and control a computer that is somewhere else with the computer that is sitting right in front of you. If you have a remote webserver running Linux, SSH will let you install software, edit files, change the server configuration, access the database, and more. Did you forget a file on your computer at home? No problem, just SSH into your home computer and send the file to your email account or copy it directly to your office computer.</p>
<p>Hopefully those quick examples of what you can do is enough to whet your appetite.</p>
<p><span id="more-1097"></span></p>
<h3>Making SSH Work for You</h3>
<p>When working on a Linux system, connecting to other Linux systems via SSH becomes so easy. Everything you need is bundled directly into the OS. All you need to do is load up the Terminal and run something that looks like the following:</p>
<div class="code">ssh username@hostname</div>
<p>After supplying the password, you&#8217;re connected.</p>
<p>So, if this is so easy, you might wonder what could make this any simpler. While the process I just described is definitely easy, there is always some server out there that requires a ssh command that is just difficult enough to be annoying. For example, imagine having to type the following each time you wanted to connect:</p>
<div class="code">ssh -p 2222 -2 -c blowfish-cbc,aes256-cbc really_long_username@locutus.borg.domain.com</div>
<p>While it&#8217;s possible to remember this, who wants to?</p>
<h4>Host-Based Config Options</h4>
<p>Fortunately, ssh has a way to store options for each host.</p>
<div class="code">mkdir -p ~/.ssh<br />
gedit ~/.ssh/config</div>
<p>This will load up the config file for editing in <a href="http://projects.gnome.org/gedit/" target="_blank">Gedit</a>.</p>
<p>The format for setting options is as follows:</p>
<div class="code">Host hostname<br />
option value<br />
option value</div>
<p>Using the config file, you can lose all those additional options and quickly connect by just using the following command:</p>
<div class="code">ssh hostname</div>
<p>Doesn&#8217;t that look a lot better?</p>
<p>Take the previous nasty example I provided. I can add the following to my config file for that specific host:</p>
<pre style="padding-left:20px;">Host locutus.borg.domain.com
    User really_long_username
    Port 2222
    Protocol 2
    Cipher blowfish-cbc,aes256-cbc</pre>
<p>After adding this to my ~/.ssh/config file, I can now connect with:</p>
<div class="code">ssh locutus.borg.domain.com</div>
<p>Easy cheesy.</p>
<p>There are a large variety of options available for the host-based config. For a full list of these options, run &#8220;man ssh_config&#8221; from Terminal.</p>
<h4>Creating Hostname Aliases</h4>
<p>Given the previous example, the hostname is locutus.borg.domain.com. That&#8217;s quite a bit to type, and it isn&#8217;t easy to remember how to spell locutus properly. Fortunately, the config file comes to the rescue again.</p>
<p>We can update our previous host definition to the following:</p>
<pre style="padding-left:20px;">Host locutus.borg.domain.com locutus borg loc domain.com 192.168.1.105
    Hostname locutus.borg.domain.com
    User really_long_username
    Port 2222
    Protocol 2
    Cipher blowfish-cbc,aes256-cbc</pre>
<p>Now I can easily connect to the system using any of the following commands:</p>
<div class="code">ssh locutus.borg.domain.com<br />
ssh locutus<br />
ssh borg<br />
ssh loc<br />
ssh domain.com<br />
ssh 192.168.1.105</div>
<p>Simply set up all the alias names you&#8217;d like to have after &#8220;Host&#8221; while seperating each one with a space. I always keep the original hostname in the list and add an additional alias to is easy to remember and type.</p>
<h4>Keep the Connection Alive</h4>
<p>Due to issues either caused by certain routers&#8217; NAT firewalls or due to odd server configurations, I have found that my SSH connections will die if I leave then idle for too long. This is very annoying as it leaves me with a frozen session that I can&#8217;t do anything with. Fortunately, config comes to the rescue again. On each and every one of my host options in my config files, I have a ServerAliveInterval option that tells ssh to send a small keep-alive packet to the server at an interval that I specify.</p>
<p>For example:</p>
<pre style="padding-left:20px;">Host domain.com
    User chris
    ServerAliveInterval 240</pre>
<p>This tells ssh to send a keep-alive packet every 240 seconds, or four minutes. Now the only thing that terminates my connection is an actual loss of internet connection.</p>
<p>If you find that your connection still drops when idle, lower the number until it stabilizes. Even though the packet is small, you don&#8217;t want to send it too much. So, only lower the value to below a minute if you must.</p>
<h3>Using Keypairs to Increase Security</h3>
<p>SSH has the ability to use security keypairs to authenticate your session with the server. A security key can replace the need to supply your password when connecting to your server.</p>
<p>If you have access to modify your server&#8217;s SSH daemon settings, you can use your authentication key to disable password authentication. This means that an attacker would have to break your key&#8217;s encryption in order to connect rather than just having to guess your password.</p>
<p>Another great feature of keys is that you can use the same key for multiple server accounts. Since the private part of your key only resides on your local system, using the same key for multiple systems doesn&#8217;t decrease security in the same way that using the same password on multiple servers can. For example, if someone broke into one of your server accounts, they could get your public key, but this key will only give them the ability to give your key the ability to authenticate on other servers rather than giving them the ability to authenticate on the rest of your&#8217;s.</p>
<h4>What This Means for Shared Servers</h4>
<p>If your server is a shared host where you don&#8217;t have access to root, you typically can&#8217;t disable password authentication; however, keys can still greatly increase your security. Since you won&#8217;t need to supply your user&#8217;s password each time you connect since the key is doing the authentication, you can change your user&#8217;s password to something extremely strong and complex. This will make it very difficult for anyone to break into your account using <a href="http://en.wikipedia.org/wiki/Brute_force_attack" target="_blank">brute-force</a> methods.</p>
<h4>Generating a SSH Authentication Key</h4>
<p>Generating a key is extremely simple. Ubuntu, and most major distributions, have all the software you need already installed. Simply run the following command in Terminal:</p>
<div class="code">ssh-keygen -t dsa</div>
<p>Accept the defaults for all the options except the password.</p>
<p>Supplying the password is optional. If you simply press enter without supplying a password, your key won&#8217;t require a password to use. Let me share some information about what having a password on your key means to you.</p>
<ul>
<li>Unlike some password protection methods where the password is easily cracked, your key&#8217;s password is an integral part of the key. Without your password, the key is useless.</li>
<li>If your key does not have a password, anyone who manages to gain access to your private key will have the ability to connect to every server your key is authenticated for without any password or other authentication required.</li>
<li>On many systems, you will have to supply the password each and every time the key is used. Fortunately, for us Ubuntu users, we can simply opt to have the system remember the password for us as long as we are logged into our desktop. I&#8217;m sure that this feature is present on other systems, such as Linux Mint, but I haven&#8217;t tested which ones have this feature yet.</li>
</ul>
<p>As you can see, there are great benefits to using a password on your key. There really aren&#8217;t any good reasons to not put a password on the key since Ubuntu will remember it for you.</p>
<p>By now, you should have generated your key. If you accepted the default location, you should have two new files in your ~/.ssh directory: id_dsa and id_dsa.pub.</p>
<p>The ~/.ssh/id_dsa file is your private key. This file should remain safe and secure on your system.</p>
<p>The ~/.ssh/id_dsa.pub file is your public key. The contents of this file will be used to authenticate your key with servers.</p>
<p>The permissions on the ~/.ssh/id_dsa file should be set up correctly by ssh-keygen. I like to make sure however as the wrong permission settings can cause the key to be ignored when authenticating. You can set the correct permissions by running:</p>
<div class="code">chmod 0600 ~/.ssh/id_dsa</div>
<p>Now it&#8217;s time to set up the server.</p>
<h4>Setting up Your Server</h4>
<p>Now you need to log into the server you want to use your key to authenticate with.</p>
<p>You need to create a ~/.ssh folder if one doesn&#8217;t already exist:</p>
<div class="code">mkdir -p ~/.ssh</div>
<p><em>The -p will prevent an error from being thrown if the folder already exists.</em></p>
<p>Exit out of your server connection. We&#8217;re going to do a slick command from our Ubuntu Terminal to add our public key to the server.</p>
<p>From our desktop&#8217;s Terminal (not the server&#8217;s command line), run the following:</p>
<div class="code">cat ~/.ssh/id_dsa.pub | ssh <strong>hostname</strong> &#8220;cat &gt;&gt; ~/.ssh/authorized_keys&#8221;</div>
<p>Replace <strong>hostname</strong> with the hostname that you use to connect to your server with. I&#8217;m assuming that you followed my instructions earlier and set up your ~/.ssh/config file to not require anything more than a hostname to set all the options needed to connect. If you haven&#8217;t done this, make sure you specify the port number (if other than 22), username, and any other necessary options to make the connection.</p>
<h4>Testing</h4>
<p>Now we just need to connect to the server again and see if it asks for the password.</p>
<div class="code">ssh <strong>hostname</strong></div>
<p>If you are asked for a password, your authentication key was either not recognized, had improper permissions, or was not able to match with the server&#8217;s public key setting. A good way of checking on a potential problem is to run:</p>
<div class="code">ssh -v <strong>hostname</strong></div>
<p>This will cause a large amount of verbose debug data to be printed during the connection phase. If the key is used properly, you should see the lines listed:</p>
<div class="code">debug1: identity file /home/username/.ssh/id_dsa type 2<br />
&#8230;<br />
debug1: Authentications that can continue: publickey,keyboard-interactive<br />
debug1: Next authentication method: publickey<br />
debug1: Offering public key: /home/username/.ssh/id_dsa<br />
&#8230;<br />
debug1: Authentication succeeded (publickey)</div>
<p>If you don&#8217;t see that the server can see the publickey authentication method or if that method isn&#8217;t the first one, your server isn&#8217;t set up to do key authentication. Contact your hosting provider and ask them if they can add this as it will increase server security.</p>
<p>If you can&#8217;t figure out the problem, please let me know. You can send me the output of your &#8220;ssh -v hostname&#8221; command via the <a href="http://chrisjean.com/contact/" target="_blank">Contact</a> page, and I&#8217;ll do my best to help you out.</p>
<h3>Conclusion</h3>
<p>I hope that I helped you use ssh more efficiently and make your servers more secure at the same time. However, I&#8217;ve just scratched the surface on this topic. There are dozens of ways to use SSH in ways that I haven&#8217;t touched on.</p>
<p>Here are just a few additional things that you can do with SSH:</p>
<ul>
<li>Log into a visual desktop remotely by tunneling X</li>
<li>Secure FTP alternative: use scp to easily transfer files between systems</li>
<li>Mount remote file systems as local mount points</li>
<li>Browse securely on insecure networks or bypass network restrictions to browse freely</li>
<li>Easily run a command on remote servers directly. This is great for scripting status programs.</li>
<li>And many more possibilities</li>
</ul>
<p>I&#8217;m sure that I&#8217;ll touch on some of these subjects later on. If you&#8217;d like me to get to specific ones ASAP, please leave a coment and let me know what you&#8217;d like to see.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisjean.com/2009/02/19/ssh-tutorial-for-ubuntu-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

