<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Linux References</title>
	<atom:link href="http://indolunix.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://indolunix.wordpress.com</link>
	<description>Just another Linux References</description>
	<lastBuildDate>Thu, 31 Mar 2011 03:53:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='indolunix.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Linux References</title>
		<link>http://indolunix.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://indolunix.wordpress.com/osd.xml" title="Linux References" />
	<atom:link rel='hub' href='http://indolunix.wordpress.com/?pushpress=hub'/>
		<item>
		<title>What are the SUID, SGID and the Sticky Bits?</title>
		<link>http://indolunix.wordpress.com/2008/09/03/what-are-the-suid-sgid-and-the-sticky-bits/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/what-are-the-suid-sgid-and-the-sticky-bits/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 13:07:28 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=33</guid>
		<description><![CDATA[Linux has some special attributes associated with all files. Often in X Windows when you check the properties of any file (by right clicking on it and viewing its properties) you would get to see 3 special attributes besides the common read/write/execute rights for the owner/group/others . The 3 extra attributes are known as SUID, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=33&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Linux                    has some special attributes associated with all files. Often                    in X Windows when you check the properties of any file (by right                    clicking on it and viewing its properties) you would get to                    see 3 special attributes besides the common read/write/execute                    rights for the owner/group/others . The 3 extra attributes are                    known as SUID, SGID and Sticky Bits</span><br />
<span id="more-33"></span><br />
<span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Sticky                    Bit</strong></span></p>
<p>Lets start with Sticky bit first. Since this is the most simplest                    to explain. <em>Setting the sticky bit tells Unix that once the                    concerned application is executed, it should remain in memory</em>.                    Remember that Unix is a multi-user OS and was mainly designed                    so that multiple users can work simultaneously. Thus the logic                    used is that a program that exists in memory requires lesser                    time to start when a new user requests for the same program.                    Thus when one user has just used a program and then a new user                    wants to use the same program, the second user doesn&#8217;t have                    to face a time delay for the program to initialize itself. It                    would be readily available to him. The concept of the sticky                    bit was a very useful one, long back when fast disk access and                    other memory access technologies weren&#8217;t around. But in today&#8217;s                    age the concept of sticky bit is obsolete, since modern day                    technology is advanced enough to reduce the time delay while                    loading applications into the memory. Thus currently the sticky                    bit is of very little significance. Sticky bit is only associated                    with executables.</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
<strong>SUID (Set User ID) Bit</strong></span></p>
<p>Sometime you may faced an error while trying to run any application                    stating that the application must be &#8216;SUID root&#8217; . You might                    have been confused that time, but now once you read this article                    you would no longer find it confusing.</p>
<p>SUID stands for Set User ID. <em>This means that if the SUID                    bit is set for any application then your user ID would be set                    as that of the owner of application/file rather than the current                    user, while running that application.</em> That means in case                    I have an application whose owner is &#8216; root &#8216; and it has its                    SUID bit set, then when I run this application as a normal user,                    that application would <em>still run as root</em>. Since the SUID                    bit tells Linux that the the User ID root is set for this application                    and whenever this application executes it must execute as if                    root was executing it (since root owns this file).</p>
<p>In case you have really understood the above you may be wondering                    &#8211; isnt this a major security risk? If users are able to run                    applications as root, then it must be definitely posing as a                    threat to the security of the system. Actually the SUID is used                    to increase the security in a way. Let me explain this with                    my own example I use on my machine.</p>
<p><strong><br />
One way I use SUID on my machine</strong></p>
<p>I have a few files that I modify through Linux and then before                    I shutdown Linux I have to transfer them to my Windows partition                    for further use there. As a normal user I do not have write                    access to the Windows partitions that I have mounted. So I have                    to be the superuser to be able to write to that partition. I                    have created a simple shell script that copies my files to the                    Windows partitions. This script was created by root user and                    the SUID bit was set. Access rights to this script have been                    given to all users. Now whenever I want to copy my files I simply                    run this script. Even though I have logged in as a normal user,                    the SUID bit which is set causes this script to execute as if                    the root was executing it and it allows me to write to the Windows                    partitions.</p>
<p>Had the SUID bit not been set, I would have to type &#8216; su &#8216; at                    the prompt and get temporary superuser access to get write access                    to the Windows partitions. Hope you got the point..</p>
<p><strong>Note :</strong> In case                    you do not know how to access your Windows partitions through                    Linux, refer to <a href="http://www.codecoffee.com/tipsforlinux/articles/3.html">Article No. 3</a></p>
<p>You may be thinking that since these applications would run                    as root they can do harmful things and destroy the system. The                    concept behind SUID bit is that you as the superuser would be                    able to allow certain applications / scripts to be run by the                    users as if they were the superuser for the time being. What                    these application / scripts do when they execute should be completely                    known to you. Even though the users would be allowed to execute                    these programs as root they would be able to do ONLY THOSE<em> </em>things that these programs were designed to do. So in case                    a script was designed to copy 5 files from one place to another.                    Then the user who would run that script would be able to ONLY                    copy those 5 files from one place to another. He would not be                    able to modify that script in any way since he would <em>not                    have write access</em> to the script. He would only be having                    execute rights for that script. Hence its an excellent idea                    to allow users to do some important backup using a script that                    does only that and by setting the SUID bit for that script.                    This way the users don&#8217;t have to know the superuser password                    but can still use some facilities that are only available to                    the superuser</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Important                    :</strong> Think twice before setting the SUID bit for scripts (owned                    by root) that take arguments at the command line. Since you                    never know what parameters a malicious user may pass to your                    script. Since the script would run as root it could do great                    damage if misused.</span></p>
<p><strong>SGID (Set Group ID) bit</strong></p>
<p>Just like SUID, setting the SGID bit for a file sets your group                    ID to the file&#8217;s group while the file is executing. IT is really                    useful in case you have a real multi-user setup where users                    access each others files. As a single homeuser I haven&#8217;t really                    found a lot of use for SGID. But the basic concept is the same                    as the SUID, the files whose SGID bit are set would be used                    as if they belong to that group rather than to that user alone.</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Note                    :</strong> Making SUID and SGID programs completely safe is very                    difficult (or maybe impossible) thus in case you are a system                    administrator it is best to consult some professionals before                    giving access rights to <em>root owned</em> applications by setting                    the SUID bit. As a home user (where you are both the normal                    user and the superuser) the SUID bit helps you do a lot of things                    easily without having to log in as the superuser every now and                    then. </span></p>
<p>source : http://www.codecoffee.com</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=33&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/what-are-the-suid-sgid-and-the-sticky-bits/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>How to find &#8211; Size of a directory &amp; Free disk space</title>
		<link>http://indolunix.wordpress.com/2008/09/03/how-to-find-size-of-a-directory-free-disk-space/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/how-to-find-size-of-a-directory-free-disk-space/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 13:05:00 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=31</guid>
		<description><![CDATA[This article explains 2 simple commands that most people want to know when they start using Linux. They are finding the size of a directory and finding the amount of free disk space that exists on your machine. The command you would use to find the directory size is &#8216; du &#8216;. And to find [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=31&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">This                    article explains 2 simple commands that most people want to                    know when they start using Linux. They are finding the size                    of a directory and finding the amount of free disk space that                    exists on your machine. The command you would use to find the                    directory size is &#8216;<em> du </em>&#8216;. And to find the free disk space                    you could use &#8216;<em> df </em>&#8216;.</span></p>
<p>All the information present in this article is available in                    the man pages for du and df. In case you get bored reading the                    man pages and you want to get your work done quickly, then this                    article is for you.</p>
<p>-<span id="more-31"></span></p>
<p><strong>&#8216;du&#8217; &#8211; Finding the size of a directory</strong></p>
<p><strong>$ du</strong><br />
Typing the above at the prompt gives you a list of directories                    that exist in the current directory along with their sizes.                    The last line of the output gives you the total size of the                    current directory including its subdirectories. The size given                    includes the sizes of the files and the directories that exist                    in the current directory as well as all of its subdirectories.                    Note that by default the sizes given are in kilobytes.</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>$                    du /home/david</strong><br />
The above command would give you the directory size of the directory                    /home/david</span></p>
<p><strong>$ du -h</strong><br />
This command gives you a better output than the default one.                    The option &#8216;-h&#8217; stands for <em>human readable format</em>. So                    the sizes of the files / directories are this time suffixed                    with a &#8216;k&#8217; if its kilobytes and &#8216;M&#8217; if its Megabytes and &#8216;G&#8217;                    if its Gigabytes.</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong><br />
$ du -ah</strong><br />
This command would display in its output, not only the directories                    but also all the files that are present in the current directory.                    Note that &#8216;du&#8217; always counts all files and directories while                    giving the final size in the last line. But the &#8216;-a&#8217; <em>displays</em> the filenames along with the directory names in the output.                    &#8216;-h&#8217; is once again human readable format.</span></p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>$                    du -c</strong><br />
This gives you a <em>grand total</em> as the last line of the                    output. So if your directory occupies 30MB the last 2 lines                    of the output would be</span></p>
<p>30M .<br />
30M total</p>
<p>The first line would be the default last line of the &#8216;du&#8217; output                    indicating the total size of the directory and another line                    displaying the same size, followed by the string &#8216;<em>total</em>&#8216;.                    This is helpful in case you this command along with the grep                    command to only display the final total size of a directory                    as shown below.</p>
<p><strong>$ du -ch | grep total</strong><br />
This would have only one line in its output that displays the                    total size of the current directory including all the subdirectories.</p>
<p><strong>Note :</strong> In case you are not familiar with                    pipes (which makes the above command possible) refer to <a href="http://www.codecoffee.com/tipsforlinux/articles/24.html">Article                    No. 24</a> . Also grep is one of the most important commands                    in Unix. Refer to <a href="http://www.codecoffee.com/tipsforlinux/articles/25.html">Article No. 25</a> to know                    more about grep.</p>
<p><strong>$ du -s</strong><br />
This displays a summary of the directory size. It is the simplest                    way to know the total size of the current directory.</p>
<p><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">$                    du -S</span></strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
This would display the size of the current directory excluding                    the size of the subdirectories that exist within that directory.                    So it basically shows you the total size of <em>all the files</em> that exist in the current directory.</span></p>
<p><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">$                    du &#8211;exculde=mp3</span></strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
The above command would display the size of the current directory                    along with all its subdirectories, <em><strong>but</strong></em> it would                    exclude all the files having the given pattern present in their                    filenames. Thus in the above case if there happens to be any                    mp3 files within the current directory or any of its subdirectories,                    their size <em>would not be included</em> while calculating the                    total directory size.</span></p>
<p>-</p>
<p><strong> &#8216;df&#8217; &#8211; finding the disk free space / disk usage</strong></p>
<p><strong>$ df</strong><br />
Typing the above, outputs a table consisting of 6 columns. All                    the columns are very easy to understand. Remember that the &#8216;Size&#8217;,                    &#8216;Used&#8217; and &#8216;Avail&#8217; columns use kilobytes as the unit. The &#8216;Use%&#8217;                    column shows the usage as a percentage which is also very useful.</p>
<p><strong>$ df -h</strong><br />
Displays the same output as the previous command but the &#8216;-h&#8217;                    indicates <em>human readable format</em>. Hence instead of kilobytes                    as the unit the output would have &#8216;M&#8217; for Megabytes and &#8216;G&#8217;                    for Gigabytes.</p>
<p>Most of the users don&#8217;t use the other parameters that can be                    passed to &#8216;df&#8217;. So I shall not be discussing them.</p>
<p>I shall in turn show you an example that I use on my machine.                    I have actually stored this as a script named <em>&#8216;usage</em>&#8216;                    since I use it often.</p>
<p><strong> Example :<br />
</strong><br />
I have my Linux installed on /dev/hda1 and I have mounted my                    Windows partitions as well (by default every time Linux boots).                    So &#8216;df&#8217; by default shows me the disk usage of my Linux as well                    as Windows partitions. And I am only interested in the disk                    usage of the Linux partitions. This is what I use :</p>
<p><strong>$ df -h | grep /dev/hda1 | cut -c 41-43</strong></p>
<p>This command displays the following on my machine</p>
<p><strong>45%</strong></p>
<p>Basically this command makes &#8216;df&#8217; display the disk usages of                    all the partitions and then extracts the lines with /dev/hda1                    since I am only interested in that. Then it cuts the characters                    from the 41st to the 43rd column since they are the columns                    that display the usage in % , which is what I want.</p>
<p><strong>Note :</strong> In case you are not familiar with pipes (which                    is used in the above command) then refer to <a href="http://www.codecoffee.com/tipsforlinux/articles/24.html">Article                    No. 24</a> . &#8216;cut&#8217; is another tool available in Unix. The above                    usage of cut gets the the characters that are present in the                    specified columns. If you are interested in knowing how to mount                    you Windows partitions under Linux, please refer to <a href="http://www.codecoffee.com/tipsforlinux/articles/3.html">Article                    No. 3</a> .</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">There                    are a few more options that can be used with &#8216;du&#8217; and &#8216;df&#8217; .                    You could find them in the man pages.</span></p>
<p>source : http://www.codecoffee.com</p>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/31/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/31/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=31&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/how-to-find-size-of-a-directory-free-disk-space/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding file permissions and modifying them using chmod</title>
		<link>http://indolunix.wordpress.com/2008/09/03/understanding-file-permissions-and-modifying-them-using-chmod/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/understanding-file-permissions-and-modifying-them-using-chmod/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 12:35:06 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=24</guid>
		<description><![CDATA[Linux is basically a multi-user system. But nowadays there has been an increase in the usage of Linux as an OS for single user home computers as well. But since it was originally meant to be a multi-user OS, a lot of things that would be necessary in a multi-user system are built into the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=24&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Linux                  is basically a multi-user system. But nowadays there has been                  an increase in the usage of Linux as an OS for single user home                  computers as well. But since it was originally meant to be a multi-user                  OS, a lot of things that would be necessary in a multi-user system                  are built into the Linux core. One such thing is file / directory                  permissions.</span></p>
<p>In Linux every file present on the disk has associated permissions                  with it. These permissions decide on who and in what manner these                  files should be used. The rest of this article explains these                  file / directory permissions in details.</p>
<p><span id="more-24"></span></p>
<p>In order to view the permissions associated with a file, you could                  use the &#8216; ls &#8216; command. On executing &#8216; ls &#8216; you would be presented                  with a directory listing with one filename per line. I shall explain                  file permissions with the help of a sample output as shown below</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="14%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">drwxr-xr-x<br />
frwxr-xr-x<br />
frwx&#8212;&#8212; </span></td>
<td width="5%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">4<br />
4<br />
4</span></td>
<td width="8%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david<br />
david<br />
david</span></td>
<td width="9%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david<br />
david<br />
david</span></td>
<td width="9%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">512<br />
1240<br />
4109 </span></td>
<td width="18%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Jan                        15 11:31<br />
Jan 15 08:12<br />
Jan 15 08:12 </span></td>
<td width="37%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">tutorials<br />
viewresume<br />
privatedata.txt</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Yours                    would obviously be different from this one. But this output                    should be enough to explain file permissions. The above output                    shows that within the current directory there are 3 entries.                    Lets start with the 2nd line.<br />
</span></p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="14%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">frwxr-xr-x </span></strong></td>
<td width="5%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">4 </span></strong></td>
<td width="8%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david </span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david </span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">1240<br />
</span></strong></td>
<td width="18%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Jan                        15 08:12<br />
</span></strong></td>
<td width="37%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">viewresume </span></strong></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
The first character &#8216;<strong>f</strong>&#8216; indicates that &#8216; viewresume &#8216; is                  a file. In case it was the name of a directory there would have                  been a &#8216;<strong>d</strong>&#8216; instead of a &#8216;<strong>f</strong>&#8216;</span></p>
<p>The next part <strong>rwxr-xr-x</strong> (a total of 9 characters) should                  be spilt into 3 parts each consisting of 3 consecutive letters</p>
<table border="1" cellspacing="0" cellpadding="0" width="100%" bgcolor="#f5f5f5">
<tbody>
<tr>
<td width="33%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Part                        1 (User) : rwx</span></strong></td>
<td width="33%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Part                        2 (Group) : r-x</span></strong></td>
<td width="33%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Part                        3 (World) : r-x</span></strong></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">The                    meaning of these 3 characters which form this 9 character sequence                    is shown in the table below.<br />
</span></p>
<table border="1" cellspacing="1" cellpadding="3" width="100%">
<tbody>
<tr>
<td colspan="2" bgcolor="#ebebeb">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Description                          of r,w,x for Files</strong></span></div>
</td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>r</strong></span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Read                        permissions for the file (whether the contents of the file                        can be read or not)</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>w</strong></span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Write                        permissions for the file (whether a file can be modified                        or not)</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>x</strong></span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Execute                        permissions (whether a file is an executable/script or not)</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">In                    Part 1 r,w and x, all the 3 permissions exist. This means that                    the the concerned file, &#8216; viewresume &#8216; can be read, written                    to as well as executed. Thus in case you want to just read the                    contents of that file you could do so. In case you want to modify                    the file that too would be allowed. Assuming that &#8216; viewresume                    &#8216; is some kind of a script it also has execute permissions assigned                    to it. So you could execute this program from the shell prompt                    as well.</span></p>
<p>Some of you&#8217;ll who are really smart must have already started                    thinking about how you could protect your data from others (in                    a multi-user system) when you have provided r,w and x permissions                    to the file. That is exactly why there is a 9 character sequence                    present instead of just 3 characters.</p>
<p><strong>Part 1 decides the permissions for the User (the owner of                    the file)<br />
Part 2 decides the permissions for other users who belong to                    the same Group as the file<br />
Part 3 decides the permissions for Others (rest of the world)                    who might access your folder</strong></p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">As                    the owner any file you create would be having the r and w permissions                    present. In case its a script you should also add the execute                    permission. This is explained in a later section of this article.</span></p>
<p>In case you are a part of a project involving other users, you                    should ask the administrator to create a separate group and                    include all the project members in that group. Then you could                    create all your programs as a part of that group and use the                    group permissions so that only those members belonging to your                    project group can read, modify your files that concern to that                    project.</p>
<p>For others (rest of the world) it is always best to leave the                    default permissions which would be generally r and x. Never                    ever give w permissions to all, else anyone would be able to                    modify your files.</p>
<p>The rest of the fields don&#8217;t have anything to with file permissions                    as such. So they shall not be dealt with in this article. Now                    lets consider the 3rd line in the &#8216; ls &#8216; output.</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="14%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">frwx&#8212;&#8212;</span></strong></td>
<td width="5%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">4</span> </strong></td>
<td width="8%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david</span> </strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david</span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">4109 </span></strong></td>
<td width="18%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Jan                        15 08:12 </span></strong></td>
<td width="37%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">privatedata.txt</span> </strong></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
This shows the permissions for a file aptly named &#8216; privatedata.txt                  &#8216;. The name itself suggests that this is some important file that                  only the owner of the file should be allowed to read, write or                  execute. Thus no one else (group or others) should be allowed                  to even view the contents of this file. Thus you can see that                  the permissions for the file are <strong>rwx&#8212;&#8212;</strong></span></p>
<p><strong></strong>Dividing it into 3 parts you would get &#8216; <strong>rwx</strong> &#8216; and                  &#8216; <strong>&#8212;</strong> &#8216; and &#8216; <strong>&#8212;</strong> &#8216;</p>
<p><strong> The &#8211; (hyphen) indicates that the particular property is not                  existing for that file or directory</strong>.</p>
<p>Thus in this case the 2nd and 3rd Part only consist of hyphens                  thus indicating that neither the Group members nor Others would                  be allowed to either read, write or execute this file. You on                  the other hand have all these 3 properties set so that you are                  free to do anything with the file.</p>
<p>Now consider the 1st line in the &#8216; ls &#8216; output</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="14%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">drwxr-xr-x</span></strong></td>
<td width="5%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">4</span></strong></td>
<td width="8%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david</span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david</span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">512 </span></strong></td>
<td width="18%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Jan                        15 11:31</span></strong></td>
<td width="37%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">tutorials </span></strong></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
Note that the first character on the line is a &#8216; <strong>d</strong> &#8216; which                  indicates that &#8216; tutorials &#8216; is the name of a directory and not                  a file.</span></p>
<p><strong>Important :</strong> The permissions for directories take on a slightly                  different meaning than those for files. This is explained in some                  detail.</p>
<table border="1" cellspacing="1" cellpadding="3" width="100%">
<tbody>
<tr bgcolor="#ebebeb">
<td colspan="2" valign="top">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Description                          of r,w,x for Directories</strong></span></div>
</td>
</tr>
<tr>
<td width="8%" valign="top">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>r</strong></span></div>
</td>
<td width="92%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">This                        means that a directory has read permission, so you are allowed                        to see what files are present within that directory.</span></td>
</tr>
<tr>
<td width="8%" valign="top">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>w</strong></span></div>
</td>
<td width="92%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">This                        means that you could either add , delete or rename the files                        that exist within that particular directory.</span></td>
</tr>
<tr>
<td width="8%" valign="top">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>x</strong></span></div>
</td>
<td width="92%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Execute                          would allow you to use the directory name when accessing                          files inside that directory. For example (not really the                          simplest example), In <a href="http://www.codecoffee.com/tipsforlinux/articles/029-1.html">Article No.                          29</a> I have explained how users could set a directory                          for their homepage so that other users can come and see                          their homepage. For this to be possible, suppose the other                          user types the name of the particular file with the directory                          name, unless you have the x permissions set for all the                          directories present in the request , that user would not                          be able to view the contents of the file he requests.                          In case you are confused try experimenting with this.                          If you are single home user, create another user account                          and try accessing the first users files. Whenever you                          get an access denied error, remember to check if you have                          the x permission set for the directory within which the                          file you are accessing exists.<br />
Sometimes people call the x permission for directories                          as Enter permission. Indicating whether or not you are                          allowed to Enter the directory. This is correct to a certain                          extent.</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
In our example the &#8216; tutorials &#8216; directory has r and x permissions                  set for group and world. So basically all the users could view                  the files that are present within that directory. Since the w                  permission is missing for group and world, they cannot modify                  add or delete any of the files within the tutorials directory                  (unless there is a situation as described in the Note below).                  You on the other hand as usual are allowed to do as you wish.                  Doesn&#8217;t Linux make you feel powerful !!</span></p>
<p>Now for some technical language. Though I have been calling these                  r,w and x as permissions, you would generally call them as bits.                  So don&#8217;t look surprised when a Group members asks you to set the                  read bit for a directory. It basically means, set the read permissions                  for that directory.</p>
<p><strong>Important :</strong> As beginners until you are completely familiar                  with file permissions, remember one important rule. Never give                  a directory lesser privileges and the files within that directory                  more privileges. I mean in case you do not set the <em>x bit</em> for a directory and set the <em>w bit</em> for the files within                  that directory. Though you may expect that since the directory                  doesn&#8217;t have the <em>x bit</em> set, users cannot enter the directory                  and so they wont be able to modify your files. Actually the meaning                  of the <em>x bit</em> for directories is not so simple to understand.                  Setting permissions as above would allow anyone to delete all                  your files in that directory. <em>So always give the equal or lesser                  privileges to the files within a directory as that to the directory                  itself.</em> If you don&#8217;t want the users to have write permissions                  to your files, remember <em>not to</em> set the w permissions for                  the files rather than trying to restrict write access using the                  directory permissions.</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Now                  that you have learnt what file permissions are, the next obvious                  part is to learn how to modify them. You have to use the <em>chmod</em> command to change the permissions of a file or directory. To run                  chmod on a file you should either own the file or you should be                  the superuser .</span></p>
<p>The way to use chmod is</p>
<p><strong>$ chmod [newpermissions] [filenames]</strong></p>
<p><strong></strong>Now comes a bit tricky part for beginners (more so for those                  who don&#8217;t have a mathematical background), but I shall try to                  explain the problem. For chmod the newpermissions have to set                  using an octal number rather than a decimal number. In case you                  understood the previous sentence, then you have no problems. If                  you didn&#8217;t then read the next paragraph.</p>
<p><strong>Note : </strong>In case you don&#8217;t want to understand the octal system                  method there is a simpler method stated at the end of this article.                  But the octal number method is almost used by all (atleast by                  those who consider themselves to be powerusers)</p>
<p>I will not explain the concepts behind octal numbers. I shall                  only talk about the octal numbers that could be used with chmod.                  Below are the octal numbers representing different permissions</p>
<table border="1" cellspacing="1" cellpadding="3" width="70%" align="center">
<tbody>
<tr bgcolor="#ebebeb">
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;color:#000000;"><strong>r,                          w, x Permissions</strong></span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;color:#000000;"><strong>Binary</strong></span></div>
</td>
<td width="33%" bgcolor="#ebebeb">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;color:#000000;"><strong>Octal</strong></span></div>
</td>
</tr>
<tr>
<td width="31%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">&#8212;</span></td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">000</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">0</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">&#8211;x</span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 001</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 1</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">-w-</span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 010</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 2</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">-wx </span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 011</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 3</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">r&#8211;</span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 100</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 4</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">r-x</span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 101</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 5</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">rw-</span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 110</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 6</span></div>
</td>
</tr>
<tr>
<td width="31%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">rwx</span></div>
</td>
<td width="36%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 111</span></div>
</td>
<td width="33%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> 7</span></div>
</td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> You have learnt that there are 9 bits associated with every                    file / directory (split into 3 parts) to decide the permissions.                    So in case you have the r,w,x permissions set for a file translate                    that to a 111 which you should further translate to the number                    7 using the above table.</span></p>
<p>Suppose there is a file with the following permissions as shown                    in this sample &#8216; ls &#8216; output</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="14%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">frwxr-xr-x </span></strong></td>
<td width="5%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">4</span> </strong></td>
<td width="8%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david</span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">david</span></strong></td>
<td width="9%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">1240</span></strong></td>
<td width="18%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Jan                        15 08:12 </span></strong></td>
<td width="37%"><strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">viewresume</span></strong></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
The existing permissions for the above file in octal numbers could                  be represented as follows<br />
<strong><br />
rwxr-xr-x </strong>==&gt;<strong> 111101101 </strong>==&gt;<strong> 755</strong></span></p>
<p><strong></strong>That&#8217;s it!! I guess it wasn&#8217;t so tough after all. Use the                  above table and figure out the permissions for other files as                  well. Once you get used to these conversions, you would be able                  to do it in no time.</p>
<p>Now in case you want to change the permissions so that group members                  and others can neither read nor execute this file, you would require                  the new permissions to look something like the following</p>
<p><strong>rwx&#8212;&#8212; </strong>==&gt;<strong> 111000000 </strong>==&gt;<strong> 700</strong></p>
<p><strong></strong>So the exact command that you would be typing at the prompt                  would be</p>
<p><strong>$ chmod 700 viewresume</strong></p>
<p><strong></strong>Now check the permissions of the file once again with an &#8216;                  ls &#8216; command and you would see the changes that you just made.</p>
<p>For your quick reference here are a few standard numeric codes                  (that&#8217;s what it is called) that are often used..</p>
<table border="1" cellspacing="1" cellpadding="3" width="100%">
<tbody>
<tr bgcolor="#ebebeb">
<td colspan="2">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Frequently                          used numeric parameters for chmod</strong></span></div>
</td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">755</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> The general preferred permissions for almost all the files                        on your disk </span></td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">700</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Extremely                        private data</span></td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">500</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Extremely                        private data that you would not like to accidentally modify.                        So write protect it</span></td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">775</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">General                        files used when working as a Group (Others can only view/execute                        your files)</span></td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">770</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Important                        files used when working as a Group (Others cannot do anything                        with your files)</span></td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">750</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Allowing                        group to view your files but no write access (Others cannot                        do anything with your files)</span></td>
</tr>
<tr>
<td width="11%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">777</span></div>
</td>
<td width="89%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Something                        you should never want to do <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </span></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
There&#8217;s another method to change the permissions of files rather                    than using these octal numbers (in case you just didn&#8217;t get                    the hang of it). I prefer the octal number method. Others may                    prefer the following method</span></p>
<p><strong>$ chmod g-r,g-x,o-r,o-x viewresume</strong></p>
<p><strong></strong>The above command does exactly the same thing that &#8216; chmod                    700 &#8216; command did. Yeah this one is lengthier but its simpler                    to understand. Its explained below in case you couldn&#8217;t figure                    it..</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="12%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>g-r</strong></span></td>
<td width="25%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">g                        = group</span></td>
<td width="29%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">-                        (hyphen) = remove</span></td>
<td colspan="2" width="34%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">r                        = read permission</span></td>
</tr>
<tr>
<td width="12%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>o-x</strong></span></td>
<td width="25%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">o                        = other(world)</span></td>
<td width="29%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">-                        (hyphen) = remove</span></td>
<td colspan="2" width="34%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">x                        = execute permission</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
I guess you got the point.. the other 2 parameters (g-x,o-r) can                  also be expanded in the same way. Thus the above command asks                  Linux to <strong>remove the r and x permission for both the group members                  and others</strong> <strong>(rest of world).</strong></span></p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Here                    is a quick reference if you prefer to use this method (its called                    the symbolic method)</span></p>
<table border="1" cellspacing="1" cellpadding="3" width="50%" align="center">
<tbody>
<tr bgcolor="#ebebeb">
<td colspan="2" height="17">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>Symbolic                          parameters for chmod</strong></span></div>
</td>
</tr>
<tr>
<td width="12%" height="17">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">u</span></div>
</td>
<td width="88%" height="17"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> User (yourself)</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">g</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"> Group (members of the same group) </span></td>
</tr>
<tr>
<td width="12%" height="20">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">o</span></div>
</td>
<td width="88%" height="20"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Others                          (rest of world)</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">a</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">All                        of the above 3 (u,g and o)</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">-</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Remove                          this permission </span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">+</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Add                          this permission</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">=</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Set                          to this permission</span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">r</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Read                          access </span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">w</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Write                        access </span></td>
</tr>
<tr>
<td width="12%">
<div><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">x</span></div>
</td>
<td width="88%"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Execute                        access. </span></td>
</tr>
</tbody>
</table>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
Here is another example to make things more clear.</span></p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>$                    chmod g=rwx myprogram.c</strong></span></p>
<p><strong></strong>The above command would give the group that the file belongs                    to, read-write-execute permissions irrespective of what the                    previous permissions were (for the file named myprogram.c)</p>
<p>I have discussed how to use chmod with parameters in numeric                    mode(755,700, etc.) in more detail than using it with parameters                    in the symbolic mode (u,g,o, etc.). This is because I have never                    used the symbolic mode of chmod. I had to refer to my books                    to get the technical details for this article. I have been using                    the octal numeric mode since the first time I used chmod.</p>
<p>source : http://www.codecoffee.com/</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=24&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/understanding-file-permissions-and-modifying-them-using-chmod/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>How to set Shell Environment Variables (bash shell)</title>
		<link>http://indolunix.wordpress.com/2008/09/03/how-to-set-shell-environment-variables-bash-shell/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/how-to-set-shell-environment-variables-bash-shell/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 12:29:36 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=21</guid>
		<description><![CDATA[In Linux the command line interpreter is known as the shell. Whatever you type at the command line is understood and interpreted by a program and then that program gives you an output after executing your command. This program that understands what you type is called the shell. Linux comes with quite a few shells [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=21&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">In                    Linux the command line interpreter is known as the shell. Whatever                    you type at the command line is understood and interpreted by                    a program and then that program gives you an output after executing                    your command. This program that understands what you type is                    called the shell.</span></p>
<p>Linux comes with quite a few shells such as Bourne Shell, Bourne                    Again Shell, C Shell, Korn Shell, etc. The default shell for                    Redhat Linux is &#8216; bash &#8216; which is very popular since being the                    default, most users start by learning bash. I shall talk about                    the bash shell only in this article.</p>
<p>Windows users would be familiar with a program called command.com                    which had to be present for the OS to boot. Command.com is the                    Windows equivalent of the Linux shell.<span id="more-21"></span></p>
<p>Typing the following at the shell</p>
<p><strong>$ echo $SHELL</strong></p>
<p><strong></strong>would give you the name of the current shell you are using.                    It would most probably be the bash shell in case you are a new                    user and have been assigned the default shell.</p>
<p>The bash shell is actually a program that is located at /bin/bash                    and is executed by Linux the moment a user successfully logs                    in after entering his user-pass. Once this shell starts, it                    takes over control and accepts all further user commands. The                    bash shell presents a $ prompt by default (for normal user accounts).                    You can change this prompt to whatever you like but leaving                    it at the default is best. Other shells present different prompts.                    Changing the prompt is explained below.</p>
<p><strong>-</strong></p>
<p><strong>Shell Environment</strong></p>
<p><strong></strong>All the programs that run under Linux are called as processes.                    Processes run continuously in Linux and you can kill or suspend                    different processes using various commands. When you start a                    program a new process is created. This process runs within what                    is called an environment. This particular environment would                    be having some characteristics which the program/process may                    interact with. Every program runs in its own environment. You                    can set parameters in this environment so that the running program                    can find desired values when it runs.</p>
<p>Setting a particular parameter is as simple as typing <em>VARIABLE=value</em> . This would set a parameter by the name <em>VARIABLE</em> with                    the <em>value</em> that you provide.</p>
<p>To see a list of the environment variables that are already                    set on your machine, type the following</p>
<p><strong>$ env</strong></p>
<p>This would produce a long list. Just go through the list before                    reading the next part of the article. Linux by default sets                    many environment variables for you. You can modify the values                    of most of these variables. A few of the variables that are                    set are</p>
<p><strong>HOME=/home/david </strong></p>
<p>would set the home directory to /home/david. This is perfect                    in case your login name is david and you have been given a directory                    named /home/david . In case you don&#8217;t want this to be your home                    directory but some other one you could indicate so by typing                    the new directory name. The HOME directory is always the directory                    that you are put in when you login.</p>
<p>There are many advantages of using the HOME variable. You can                    always reach your home directory by only typing &#8216; cd &#8216; at the                    prompt, irrespective of which directory you are presently within.                    This would immediately transfer you to your HOME directory.                    Besides in case you write scripts that have $HOME present in                    them to refer to the current HOME directory, these scripts can                    be used by other users as well since $HOME in their case would                    refer to their home directories.</p>
<p><strong>-</strong></p>
<p><strong>PATH=/usr:/bin/:usr/local/bin:.</strong></p>
<p>This is a very important environment variable. This sets the                    path that the shell would be looking at when it has to execute                    any program. It would search in all the directories that are                    present in the above line. Remember that entries are separated                    by a &#8216; : &#8216; . You can add any number of directories to this list.                    The above 3 directories entered is just an example.</p>
<p><strong>Note :</strong> The last entry in the PATH command is a &#8216; . &#8216;                    (period). This is an important addition that you could make                    in case it is not present on your system. The period indicates                    the current directory in Linux. That means whenever you type                    a command, Linux would search for that program in all the directories                    that are in its PATH. Since there is a period in the PATH, Linux                    would also look in the current directory for program by the                    name (the directory from where you execute a command). Thus                    whenever you execute a program which is present in the current                    directory (maybe some scripts you have written on your own)                    you don&#8217;t have to type a &#8216; ./programname &#8216; . You can only type                    &#8216; programname &#8216; since the current directory is already in your                    PATH.</p>
<p>Remember that the PATH variable is a very important variable.                    In case you want to add some particular directory to your PATH                    variable and in case you try typing the following</p>
<p><strong>PATH =/newdirectory</strong></p>
<p>This would replace the current PATH value with the new value                    only. What you would want is to append the new directory to                    the existing PATH value. For that to happen you should type</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>PATH=$PATH:/newdirectory</strong></span></p>
<p>This would add the new directory to the existing PATH value.                    Always a $VARIABLE is substituted with the current value of                    the variable.</p>
<p><strong>-</strong></p>
<p><strong>PS1=boss</strong></p>
<p>PS1 is the shell prompt. It defines what you want your shell                    prompt to look like. By default it looks like a &#8216; $ &#8216; in bash                    shell. The above case would replace the default &#8216; $ &#8216; with a                    new &#8216;boss&#8217; . Hence an ls command would look something like</p>
<p><strong>boss&gt; ls</strong></p>
<p>All your commands would now be typed at a &#8216; boss &#8216; prompt instead                    of a &#8216; $ &#8216; prompt.</p>
<p>-</p>
<p><strong>SHELL=/bin/bash</strong></p>
<p>This tells where the program that represents your shell is to                    be found. In case you typed /bin/ksh in the above, then your                    bash shell would be replaced with the ksh shell (korn shell).                    So in case you are not happy with the bash shell, you could                    replace the bash with some other shell.</p>
<p>-</p>
<p><strong>LOGNAME=david</strong></p>
<p>The LOGNAME is automatically set for you as the same as your                    login name. This variable is used in case you want to use your                    own login name in any script. This is the simplest way of getting                    your login name from within a script. Thus in case you use $LOGNAME                    in any script the script would work for all users since the                    LOGNAME always holds the name of the current user.</p>
<p>A good use is in case you have been given a temporary directory                    to work with and to make temporary files then you would want                    to delete the files that you created. You could use a command                    with the $LOGNAME in it to locate files that were created by                    you and then you could pass the result of this command to a                    rm command. This would be a neat way to delete all the files                    at one go, rather than find them one at a time.</p>
<p><strong> Note :</strong> Finding files based on a particular criteria                    and then passing those files to another program is explained                    in Article No. 21</p>
<p>-</p>
<p>There are more environment variables then the ones that are                    mentioned here. But most users would find the ones given here                    to be useful.<strong></strong></p>
<p><strong>Important : </strong>To make the above changes permanent (that is                    it should work every time you login) make the changes to the                    <em>.profile</em> file that exists in your HOME directory. Simply                    type the required commands one line for each. And there you                    go. It will be available every time you login. You could check                    the variables using &#8216;env&#8217; <span style="font-family:Verdana,Arial,Helvetica,sans-serif;">command.</span></p>
<p>source : http://www.codecoffee.com</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=21&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/how-to-set-shell-environment-variables-bash-shell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>Wildcards, Quotes, Back Quotes and Apostrophes in shell commands ( * ? [] &#8221; ` &#8216;)</title>
		<link>http://indolunix.wordpress.com/2008/09/03/wildcards-quotes-back-quotes-and-apostrophes-in-shell-commands/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/wildcards-quotes-back-quotes-and-apostrophes-in-shell-commands/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 12:24:21 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=17</guid>
		<description><![CDATA[This article deals with 2 issues which are many a times used with each other. The first one is about wildcards and the other one is the use of special characters while typing commands at the shell prompt. I call this a Special Guide since the information in this article helps you with many of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=17&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">This                    article deals with 2 issues which are many a times used with                    each other. The first one is about wildcards and the other one                    is the use of special characters while typing commands at the                    shell prompt.</span></p>
<p><em>I call this a Special Guide since the information in this                    article helps you with many of your commands and is not restricted                    to any particular application as such. Thus reading this article                    is a must for all Linux users.<br />
</em><br />
-</p>
<p>In Linux whenever you are not sure about the name of a file                    and you want to do something with files such as either search                    for them or copy them or delete some files based on some knowledge                    you have about the filenames then you can use Wildcards. Wildcards                    are basically an indicator to the shell that some particular                    part of the filename is not known to you and the shell can insert                    a combination of characters in those places and then work on                    all the newly formed filenames. This concept would be clear                    by the end of this article</p>
<p><span id="more-17"></span></p>
<p>There are 3 types of wildcards that can be used Linux. They                    are the * ? and [] . All the 3 shall be explained in detail.</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>*                    (Asterisk) Wildcard</strong></span></p>
<p>This represents any sequence of characters. This means that                    if you include a * in your filename then that part of the filename                    can be formed using any sequence of characters. The example                    below explains this concept</p>
<p><strong>$ cat article* &gt; combinedarticle</strong><br />
Would find all the files that begin with the letter sequence                    &#8216; article &#8216; and can have anything following those letters. I                    mean article01 or article10 or articlenew.. and any such file                    would be considered. All these files would be merged and would                    be written to a file named combinedarticle</p>
<p><strong>$ ls *gif</strong><br />
Would list all the files having the letters &#8216; gif &#8216; in them.                    So a file named somegifs.txt as well as a file named 123.gif                    would be listed in the output.</p>
<p><strong>$ ls *.gif</strong><br />
Would list all the files having &#8216; gif &#8216; as the extension of                    their filename. Thus in this case a file named somegifs.txt                    file would <em>NOT</em> be listed since its extension is not &#8216;                    gif &#8216;. Whereas a file named 123.gif would be listed.</p>
<p><strong>$ ls *day*</strong><br />
Would list all the files that have the letters &#8216; day &#8216; anywhere                    in their filenames. Thus files such as today.txt , dayone.txt                    and lastday.gif would all be listed in the output.</p>
<p><strong>$ ls .*gif*</strong><br />
Would list all the hidden files in the current directory that                    have the letters &#8216; gif &#8216; in their filenames. Hidden files in                    Linux begin with a . (period) in their filenames.</p>
<p><strong>$ pl * a.txt</strong><br />
Notice that there is a space between the * and &#8216; a.txt &#8216; . It                    is this space that causes the command to act as if 2 parameters                    have been passed to &#8216; <em> pl</em> &#8216; rather than one. The above                    command would print all the files that are present in the current                    directory. Once that is done it would proceed to the next file                    named a.txt and would print that also if it exists.</p>
<p><strong>Note :</strong> The * would not work with the &#8216; . &#8216; (period) that                    exists in filenames. Thus in case you use &#8216; *a &#8216; and there is                    a file whose name begins with &#8216; .a &#8216; , it would not be listed.                    When a . (period) is the first character in a filename then                    the file becomes a hidden file in Linux.</p>
<p>-</p>
<p><strong>? (Question Mark) Wildcard</strong></p>
<p>This represents only one character which can be any character.                    Thus in case you know a part of a filename but not one letter,                    then you could use this wildcard. If there are many files that                    are named such as article10, article11 and so on till article19                    you could get all these files by using article1? . In this case                    the ? would be interpreted as any one character and it would                    find all the files like article10, article11..and so on till                    article19, since all these files differ in their names by only                    the last letter.</p>
<p><strong>$ ls article1?</strong><br />
Would list all the files that begin with &#8216; article1 &#8216; and have                    one more character in their names which can be any one valid                    character.</p>
<p><strong>$ ls ??.gif</strong><br />
Would list all the .gif files in the current directory that                    have only 2 characters before the extension. Thus files such                    as ab.gif or xy.gif would be listed but 123.gif would not be                    listed</p>
<p><strong>Remember that the ? means any ONE character to be substituted                    in the place of the ?<br />
</strong><br />
<strong>$ ls *.???</strong><br />
Would list all the files in the current directory that have                    a file extension of 3 characters in length. Thus files having                    extensions such as .gif , .jpg , .txt would be listed.</p>
<p>-</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>[]                    (Square Brackets) Wildcard</strong></span></p>
<p>This represents a range of characters. So in case you have files                    that are named article10, article11, article12..all the way                    till article19 then you could select all the first 5 of them                    using the above wildcard as shown below</p>
<p><strong>$ ls article1[0-4]</strong><br />
Remember that [] represents a range from which any character                    can be present. This range can be something like [0-4] or [1-9]                    or anything like that in case of numbers. Letters could also                    be selected such as [a-g] or [F-Z] or [A-Z].</p>
<p><strong>Note :</strong> Remember that in Linux the filenames are case                    sensitive, Thus a range of [a-z] is different from [A-Z]</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>$                    ls beckham[123].jpg</strong><br />
Would list all the files that begin with the letter sequence                    &#8216; beckham &#8216; and end with either a 1, 2 or 3. Thus the possible                    filenames that could be listed (if they exists) are beckham1.jpg                    , beckham2.jpg and beckham3.jpg</span></p>
<p><strong> $ ls [a-d,A-D]*.jpg</strong><br />
This would list all the files that have an extension as .jpg                    and have as their first letter either a,b,c,d,A,B,C or D . The                    [ , ] imply that this entire range indicates <em>ONLY ONE</em> letter which can be from any of the two given sub-ranges. A                    comma is used to merge different range of letters or numbers.</p>
<p>Note : I would once again like to mention that [a-d,p-z] does                    <em>NOT</em> mean that there can be two letters, the first one                    from a to d and the second one from p to z. It means that there                    is <em>ONLY ONE</em> letter, and that letter can be from either                    a to d or from p to z.</p>
<p>In case you want to specify the range for 2 characters in the                    filenames then use the following as follows<br />
<strong>$ ls beckham[0-9][0-9]<br />
</strong><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><br />
Read the second part of this article to know about the use of                    special characters such as quotes and apostrophe marks.</span></p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;">Now                  I shall discuss the use of special characters such as &#8221; &#8216;                  and ` in various commands that you type at the shell prompt. The                  information given here is general and has to be followed when                  typing any command.</span></p>
<p><strong><br />
&#8221; &#8221; (Double Quotes) : Suppress Expansion</strong></p>
<p>Whenever you use double quotes (&#8221; &#8220;) the shell <em>suppress                  the filename expansion</em>. Thus even if you use a wildcard such                  as * but enclose it within double quotes you would not get the                  standard feature of matching for all characters. I mean a command                  such as</p>
<p><strong> $ ls c* </strong><br />
would list all the files with the names beginning with the letter                  &#8216; c &#8216; . But a command such as</p>
<p><strong> $ ls &#8220;c*&#8221; </strong><br />
would search for a file named &#8216; c* &#8216;. There would be no expansion                  of the * to match other letter sequences. The shell would expect                  the filename to have the actual character * in its name. Thus                  you would mostly get a &#8216;No File Found error&#8217; message.</p>
<p>-</p>
<p><strong>` (Back Quotes) : Command Substitution</strong></p>
<p>The ` character (found on the key with the ~) is very important                  when used in shell commands. This ` indicates that <em>command                  substitution</em> is required wherever it is used. Hence whenever                  ` is used, whatever part of the command is enclosed by these Backquotes                  marks would be executed (as if it was the only command) and then                  the result of that command would be substituted in the original                  shell command that you typed. The following explains this clearly</p>
<p><strong>$ echo &#8220;The contents of this directory are &#8221; `ls                  -l` &gt; dir.txt</strong><br />
<em><br />
Note : Remember to use the ` (found on the key with the ~ and                  NOT the one found next to the Enter button)</em></p>
<p>The above command would basically execute the &#8216; ls -l &#8216;part first                  and then substitute the result after the string &#8220;The contents                  of this directory are &#8221; and both of these together (directory                  listing + the string) would be written to a file named dir.txt</p>
<p>Basically after command substitution the new substituted value                  would act as additional parameter to the main command that was                  present in your initial statement.</p>
<p>-</p>
<p><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"><strong>&#8216;                  (Apostrophe Marks) : No Change</strong></span></p>
<p>The &#8216; character (found on the button next to the Enter button)                  is a very powerful character whenever used in any shell command.                  Basically the &#8216; (apostrophe marks) disables all kinds of transformations                  or modifications. It would consider whatever is enclosed with                  the &#8216; marks as a single entity i.e. a single parameter. Absolutely                  no sort of substitution or expansion would take place.</p>
<p><strong>$ echo &#8216;$HOME&#8217;</strong><br />
would produce at the output the string $HOME itself and would                  not print the path to your home directory. Since the single quotes                  prevents any sort of expansion, substitution and simple considers                  whatever to be present as a simple parameter in itself.</p>
<p>Just so that you remember in case you had typed the following<br />
<strong><br />
$ echo `$HOME`</strong><br />
(with the backquotes) you would get an error stating that the                  command not found. Since in this case the $HOME would be substituted                  with the path to your home directory (suppose /home/david) and                  the shell would try to execute the path as such. It would search                  for a program named (such as) /home/david. Remember that backquotes                  cause it to consider the part within the quotes to be considered                  as separate command and the output of that command would be substituted                  here. Hence in this case there is no command / program named as                  /home/david. Thus you would get an error when bash tries to execute                  that command</p>
<p>On the other hand when you type<br />
<strong><br />
$ echo &#8220;$HOME&#8221;</strong> or <strong>$ echo $HOME</strong><br />
You would get the expected output. i.e the path to your home directory                  would be printed at the output.</p>
<p>Thus you are now familiar with forming various filenames using                  wildcards. You would generally end up using the special characters                  such as quotes when trying to make complex shell commands. With                  this knowledge I hope you can get the shell to do some real good                  stuff for you.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=17&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/wildcards-quotes-back-quotes-and-apostrophes-in-shell-commands/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>A Quick Introduction to Bash Programming &#8211; Part 2</title>
		<link>http://indolunix.wordpress.com/2008/09/03/a-quick-introduction-to-bash-programming-part-2/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/a-quick-introduction-to-bash-programming-part-2/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 12:11:30 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=15</guid>
		<description><![CDATA[In this second article, Harold continues with his fast paced, excellent introduction to Bash Programming. This time he explains how to perform arithmetic operations in your bash scripts. He also explains how to define functions in your programs. Finally he concludes with an introduction to advanced concepts such as reading user inputs in your bash [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=15&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span class="big_black">In this second article, Harold continues                    with his fast paced, excellent introduction to Bash Programming.                    This time he explains how to perform arithmetic operations in                    your bash scripts. He also explains how to define functions                    in your programs. Finally he concludes with an introduction                    to advanced concepts such as reading user inputs in your bash                    scripts, accepting arguments to the scripts, trapping signals                    and also understanding return values of programs.</span></p>
<p>This is definitely much more than you must have expected&#8230;                    Once you read this you would no longer be a beginner.. you would                    already be on your way to master Bash programming!!<span id="more-15"></span></p>
<p><strong>Arithmetic with Bash</strong></p>
<p><span class="big_black">bash allows you to perform arithmetic                    expressions. As you have already seen, arithmetic is performed                    using the expr command. However, this, like the true command,                    is considered to be slow. The reason is that in order to run                    true and expr, the shell has to start them up. A better way                    is to use a built in shell feature which is quicker. So an alternative                    to true, as we have also seen, is the &#8220;:&#8221; command.                    An alternative to using expr, is to enclose the arithmetic operation                    inside $((&#8230;)). This is different from $(&#8230;). The number of                    brackets will tell you that. Let us try it</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr bgcolor="#f0f0f0">
<td><span class="big_black">#!/bin/bash<br />
x=8     # initialize x to 8<br />
y=4     # initialize y to 4<br />
# now we assign the sum of x and y to z:<br />
z=$(($x + $y))<br />
echo &#8220;The sum of $x + $y is $z&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> As always, whichever one you choose,                    is purely up to you. If you feel more comfortable using expr                    to $((&#8230;)), by all means, use it. </span></p>
<p><span class="big_black">bash is able to perform, addition,                    subtraction, multiplication, division, and modulus. Each action                    has an operator that corresponds to it</span></p>
<table border="1" cellspacing="1" cellpadding="3" width="50%" align="center">
<tbody>
<tr class="big_black_bold" bgcolor="#ebebeb">
<td width="50%">
<div>ACTION</div>
</td>
<td>
<div>OPERATOR</div>
</td>
</tr>
<tr>
<td width="50%">
<div class="big_black">Addition</div>
</td>
<td>
<div>+</div>
</td>
</tr>
<tr>
<td width="50%">
<div class="big_black">Subtraction</div>
</td>
<td>
<div>-</div>
</td>
</tr>
<tr>
<td width="50%">
<div class="big_black">Multiplication</div>
</td>
<td>
<div>*</div>
</td>
</tr>
<tr>
<td width="50%">
<div class="big_black">Division</div>
</td>
<td>
<div>/</div>
</td>
</tr>
<tr>
<td width="50%">
<div class="big_black">Modulus</div>
</td>
<td>
<div class="big_black"><span class="big_black"> %</span></div>
</td>
</tr>
</tbody>
</table>
<p><span class="big_black"> Everyone should be familiar with the                    first four operations. If you do not know what modulus is, it                    is the value of the remainder when two values are divided. Here                    is an example of arithmetic in bash</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr bgcolor="#f0f0f0">
<td>
<p class="big_black">#!/bin/bash<br />
x=5   # initialize x to 5<br />
y=3   # initialize y to 3<br />
add=$(($x + $y))   # add the values of x and y and                          assign it to variable add<br />
sub=$(($x &#8211; $y))   # subtract the values of x and                          y and assign it to variable sub<br />
mul=$(($x * $y))   # multiply the values of x and                          y and assign it to variable mul<br />
div=$(($x / $y))   # divide the values of x and y                          and assign it to variable div<br />
mod=$(($x % $y))   # get the remainder of x / y and                          assign it to variable mod</p>
<p># print out the answers:<br />
echo &#8220;Sum: $add&#8221;<br />
echo &#8220;Difference: $sub&#8221;<br />
echo &#8220;Product: $mul&#8221;<br />
echo &#8220;Quotient: $div&#8221;<br />
echo &#8220;Remainder: $mod&#8221;</td>
</tr>
</tbody>
</table>
<p><span class="big_black"> Again, the above code could have been                    done with expr. For instance, instead of add=$(($x + $y)), you                    could have used add=$(expr $x + $y), or, add=`expr $x + $y`. </span></p>
<p><span class="big_black"><strong>Reading User Input</strong></span></p>
<p><span class="big_black">Now we come to the fun part. You can                    make your program so that it will interact with the user, and                    the user can interact with it. The command to get input from                    the user, is read. read is a built in bash command that needs                    to make use of variables, as you will see</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr bgcolor="#f0f0f0">
<td><span class="big_black">#!/bin/bash<br />
# gets the name of the user and prints a greeting<br />
echo -n &#8220;Enter your name: &#8220;<br />
read user_name<br />
echo &#8220;Hello $user_name!&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> The variable here is user_name. Of                    course you could have called it anything you like. read will                    wait for the user to enter something and then press ENTER. If                    the user does not enter anything, read will continue to wait                    until the ENTER key is pressed. If ENTER is pressed without                    entering anything, read will execute the next line of code.                    Try it. Here is the same example, only this time we check to                    see if the user enters something</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td>
<p class="big_black">#!/bin/bash<br />
# gets the name of the user and prints a greeting<br />
echo -n &#8220;Enter your name: &#8220;<br />
read user_name<br />
# the user did not enter anything:<br />
if [ -z "$user_name" ]; then<br />
echo &#8220;You did not tell me your name!&#8221;<br />
exit<br />
fi<br />
echo &#8220;Hello $user_name!&#8221;</p>
</td>
</tr>
</tbody>
</table>
<p><span class="big_black">Here, if the user presses the ENTER                    key without typing anything, our program will complain and exit.                    Otherwise, it will print the greeting. Getting user input is                    useful for interactive programs that require the user to enter                    certain things.</span></p>
<p><span class="big_black"><strong>Functions</strong></span></p>
<p><span class="big_black">Functions make scripts easier to maintain.                    Basically it breaks up the program into smaller pieces. A function                    performs an action defined by you, and it can return a value                    if you wish. Before I continue, here is an example of a shell                    program using a function</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
# function hello() just prints a message<br />
hello()<br />
{<br />
echo &#8220;You are in function hello()&#8221;<br />
} </span>&nbsp;</p>
<p><span class="big_black">echo &#8220;Calling function hello()&#8230;&#8221;<br />
# call the hello() function:<br />
hello<br />
echo &#8220;You are now out of function hello()&#8221; </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Try running the above. The function                    hello() has only one purpose here, and that is, to print a message.                    Functions can of course be made to do more complicated tasks.                    In the above, we called the hello() function by name by using                    the line</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td class="big_black">hello</td>
</tr>
</tbody>
</table>
<p><span class="big_black">When this line is executed, bash searches                    the script for the line hello(). It finds it right at the top,                    and executes its contents. </span></p>
<p><span class="big_black">Functions are always called by their                    function name, as we have seen in the above. When writing a                    function, you can either start with function_name(), as we did                    in the above, or if you want to make it more explicit, you can                    use the function function_name(). Here is an alternative way                    to write function hello()</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">function hello()<br />
{<br />
echo &#8220;You are in function hello()&#8221;<br />
} </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Functions always have an empty start                    and closing brackets: &#8220;()&#8221;, followed by a starting                    brace and an ending brace: &#8220;{&#8230;}&#8221;. These braces mark                    the start and end of the function. Any code enclosed within                    the braces will be executed and will belong only to the function.                    Functions should always be defined before they are called. Let                    us look at the above program again, only this time we call the                    function before it is defined</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
echo &#8220;Calling function hello()&#8230;&#8221;<br />
# call the hello() function:<br />
hello<br />
echo &#8220;You are now out of function hello()&#8221; #                          function hello() just prints a message<br />
hello()<br />
{<br />
echo &#8220;You are in function hello()&#8221;<br />
} </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Here is what we get when we try to                    run it</span></p>
<p><span class="big_black_bold">$ ./hello.sh<br />
Calling function hello()&#8230;<br />
./hello.sh: hello: command not found<br />
You are now out of function hello()</span></p>
<p><span class="big_black"><br />
As you can see, we get an error. Therefore, always have your                    functions at the start of your code, or at least, before you                    call the function. Here is another example of using functions</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
# admin.sh &#8211; administrative tool<br />
# function new_user() creates a new user account<br />
new_user()<br />
{<br />
echo &#8220;Preparing to add a new user&#8230;&#8221;<br />
sleep 2<br />
adduser     # run the adduser                          program<br />
} </span>&nbsp;</p>
<p><span class="big_black">echo &#8220;1. Add user&#8221;<br />
echo &#8220;2. Exit&#8221;</span></p>
<p><span class="big_black">echo &#8220;Enter your choice:                          &#8220;<br />
read choice</span></p>
<p><span class="big_black"><br />
case $choice in<br />
1) new_user     # call the new_user()                          function<br />
;;<br />
*) exit<br />
;;<br />
esac </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">In order for this to work properly,                    you will need to be the root user, since adduser is a program                    only root can run. Hopefully this example (short as it is) shows                    the usefulness of functions. </span></p>
<p><span class="big_black"><strong>Trapping</strong></span></p>
<p><span class="big_black">You can use the built in command trap                    to trap signals in your programs. It is a good way to gracefully                    exit a program. For instance, if you have a program running,                    hitting CTRL-C will send the program an interrupt signal, which                    will kill the program. trap will allow you to capture this signal,                    and will give you a chance to either continue with the program,                    or to tell the user that the program is quitting. trap uses                    the following syntax</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td class="big_black_italic">trap action signal</td>
</tr>
</tbody>
</table>
<p><span class="big_black">action is what you want to do when                    the signal is activated, and signal is the signal to look for.                    A list of signals can be found by using the command trap -l.                    When using signals in your shell programs, omit the first three                    letters of the signal, usually SIG. For instance, the interrupt                    signal is SIGINT. In your shell programs, just use INT. You                    can also use the signal number that comes beside the signal                    name. For instance, the numerical signal value of SIGINTis 2.                    Try out the following program</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
# using the trap command<br />
# trap CTRL-C and execute the sorry() function:<br />
trap sorry INT </span>&nbsp;</p>
<p><span class="big_black"># function sorry() prints a message<br />
sorry()<br />
{<br />
echo &#8220;I&#8217;m sorry Dave. I can&#8217;t do that.&#8221;<br />
sleep 3<br />
}</span></p>
<p><span class="big_black"># count down from 10 to 1:<br />
for i in 10 9 8 7 6 5 4 3 2 1; do<br />
echo $i seconds until system failure.&#8221;<br />
sleep 1<br />
done<br />
echo &#8220;System failure.&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> Now, while the program is running                    and counting down, hit CTRL-C. This will send an interrupt signal                    to the program. However, the signal will be caught by the trap                    command, which will in turn execute the sorry() function. You                    can have trap ignore the signal by having &#8220;&#8221;&#8221; in                    place of the action. You can reset the trap by using a dash:                    &#8220;-&#8221;. For instance</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black"># execute the sorry() function                          if SIGINT is caught:<br />
trap sorry INT<br />
# reset the trap:<br />
trap &#8211; INT</span>&nbsp;</p>
<p><span class="big_black"><br />
# do nothing when SIGINT is caught:<br />
trap &#8221; INT </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">When you reset a trap, it defaults                    to its original action, which is, to interrupt the program and                    kill it. When you set it to do nothing, it does just that. Nothing.                    The program will continue to run, ignoring the signal. </span></p>
<p><span class="big_black"><strong>Boolean AND &amp; OR</strong></span></p>
<p><span class="big_black">We have seen the use of control structures,                    and how useful they are. There are two extra things that can                    be added. The AND: &#8220;&amp;&amp;&#8221; and the OR &#8220;||&#8221;                    statements. The AND statement looks like this</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr>
<td class="big_black_italic">condition_1 &amp;&amp; condition_2</td>
</tr>
</tbody>
</table>
<p><span class="big_black">The AND statement first checks the                    leftmost condition. If it is true, then it checks the second                    condition. If it is true, then the rest of the code is executed.                    If condition_1 returns false, then condition_2 will not be executed.                    In other words</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr>
<td class="big_black_italic">if condition_1 is true, AND if                        condition_2 is true, then&#8230;</td>
</tr>
</tbody>
</table>
<p><span class="big_black">Here is an example making use of the                    AND statement</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=5<br />
y=10<br />
if [ "$x" -eq 5 ] &amp;&amp; [ "$y"                          -eq 10 ]; then<br />
echo &#8220;Both conditions are true.&#8221;<br />
else<br />
echo &#8220;The conditions are not true.&#8221;<br />
fi </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Here, we find that x and y both hold                    the values we are checking for, and so the conditions are true.                    If you were to change the value of x=5 to x=12, and then re-run                    the program, you would find that the condition is now false. </span></p>
<p><span class="big_black">The OR statement is used in a similar                    way. The only difference is that it checks if the leftmost statement                    is false. If it is, then it goes on to the next statement, and                    the next</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr>
<td class="big_black_italic">condition_1 || condition_2</td>
</tr>
</tbody>
</table>
<p><span class="big_black">In pseudo code, this would translate                    to the following</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr>
<td class="big_black_italic">if condition_1 is true, OR if                        condition_2 is true, then&#8230;</td>
</tr>
</tbody>
</table>
<p><span class="big_black">Therefore, any subsequent code will                    be executed, provided at least one of the tested conditions                    is true</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=3<br />
y=2<br />
if [ "$x" -eq 5 ] || [ "$y" -eq 2                          ]; then<br />
echo &#8220;One of the conditions is true.&#8221;<br />
else<br />
echo &#8220;None of the conditions are true.&#8221;<br />
fi </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Here, you will see that one of the                    conditions is true. However, change the value of y and re-run                    the program. You will see that none of the conditions are true. </span></p>
<p><span class="big_black">If you think about it, the if structure                    can be used in place of AND and OR, however, it would require                    nesting the if statements. Nesting means having an if structure                    within another if structure. Nesting is also possible with other                    control structures of course. Here is an example of a nested                    if structure, which is an equivalent of our previous AND code</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=5<br />
y=10<br />
if [ "$x" -eq 5 ]; then<br />
if [ "$y" -eq 10 ]; then<br />
echo &#8220;Both conditions                          are true.&#8221;<br />
else<br />
echo &#8220;The conditions                          are not true.&#8221;<br />
fi<br />
fi </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">This achieves the same purpose as using                    the AND statement. It is much harder to read, and takes much                    longer to write. Save yourself the trouble and use the AND and                    OR statements. </span></p>
<p><span class="big_black"><strong>Using Arguments</strong></span></p>
<p><span class="big_black"> You may have noticed that most programs                    in Linux are not interactive. You are required to type arguments,                    otherwise, you get a &#8220;usage&#8221; message. Take the more                    command for instance. If you do not type a filename after it,                    it will respond with a &#8220;usage&#8221; message. It is possible                    to have your shell program work on arguments. For this, you                    need to know the &#8220;$#&#8221; variable. This variable stands                    for the total number of arguments passed to the program. For                    instance, if you run a program as follows:</span></p>
<p><span class="big_black_bold">$ foo argument</span></p>
<p><span class="big_black">$# would have a value of one, because                    there is only one argument passed to the program. If you have                    two arguments, then $# would have a value of two. In addition                    to this, each word on the command line, that is, the program&#8217;s                    name (in this case foo), and the argument, can be referred to                    as variables within the shell program. foo would be $0. argument                    would be $1. You can have up to 9 variables, from $0 (which                    is the program name), followed by $1 to $9 for each argument.                    Let us see this in action</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
# prints out the first argument<br />
# first check if there is an argument:<br />
if [ "$#" -ne 1 ]; then<br />
echo &#8220;usage: $0 &lt;argument&gt;&#8221;<br />
fi echo &#8220;The argument is $1&#8243; </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">This program expects one, and only                    one, argument in order to run the program. If you type less                    than one argument, or more than one, the program will print                    the usage message. Otherwise, if there is an argument passed                    to the program, the shell program will print out the argument                    you passed. Recall that $0 is the program&#8217;s name. This is why                    it is used in the &#8220;usage&#8221; message. The last line makes                    use of $1. Recall that $1 holds the value of the argument that                    is passed to the program. </span></p>
<p><span class="big_black"><strong>Temporary Files</strong></span></p>
<p><span class="big_black">Often, there will be times when you                    need to create a temporary file. This file may be to temporarily                    hold some data, or just to work with a program. Once the program&#8217;s                    purpose is completed, the file is often deleted. When you create                    a file, you have to give it a name. The problem is, the file                    you create, must not already existing in the directory you are                    creating it in. Otherwise, you could overwrite important data.                    In order to create a unique named temporary file, you need to                    use the &#8220;$$&#8221; symbol, by either prefixing, or suffixing                    it to the end of the file name. Take for example, you want to                    create a temporary file with the name hello. Now there is a                    chance that the user who runs your program, may have a file                    called hello, so that would clash with your program&#8217;s temporary                    file. By creating a file called hello.$$, or $$hello instead,                    you will create a unique file. Try it</span></p>
<p><span class="big_black_bold">$ touch hello<br />
$ ls<br />
hello<br />
$ touch hello.$$<br />
$ ls<br />
hello     hello.689</span></p>
<p><span class="big_black"><br />
There it is, your temporary file. </span></p>
<p><span class="big_black"><strong>Return Values</strong></span></p>
<p><span class="big_black">Most programs return a value depending                    upon how they exit. For instance, if you look at the manual                    page for grep, it tells us that grep will return a 0 if a match                    was found, and a 1 if no match was found. Why do we care about                    the return value of a program? For various reasons. Let us say                    that you want to check if a particular user exists on the system.                    One way to do this would be to grep the user&#8217;s name in the /etc/passwd                    file. Let us say the user&#8217;s name is foobar</span></p>
<p><span class="big_black_bold">$ grep &#8220;foobar&#8221; /etc/passwd<br />
$ </span></p>
<p><span class="big_black">No output. That means that grep could                    not find a match. But it would be so much more helpful if a                    message saying that it could not find a match was printed. This                    is when you will need to capture the return value of the program.                    A special variable holds the return value of a program. This                    variable is $?. Take a look at the following piece of code</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
# grep for user foobar and pipe all output to /dev/null:<br />
grep &#8220;foobar&#8221; /etc/passwd &gt; /dev/null 2&gt;&amp;1<br />
# capture the return value and act accordingly:<br />
if [ "$?" -eq 0 ]; then<br />
echo &#8220;Match found.&#8221;<br />
exit<br />
else<br />
echo &#8220;No match found.&#8221;<br />
fi </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Now when you run the program, it will                    capture the return value of grep. If it equals to 0, then a                    match was found and the appropriate message is printed. Otherwise,                    it will print that there was no match found. This is a very                    basic use of getting a return value from a program. As you continue                    practicing, you will find that there will be times when you                    need the return value of a program to do what you want.</span></p>
<p><span class="big_black">If you happen to be wondering what                    2&gt;&amp;1 means, it is quite simple. Under Linux, these numbers                    are file descriptors. 0 is standard input (eg: keyboard), 1                    is standard output (eg: monitor) and 2 is standard error (eg:                    monitor). All normal information is sent to file descriptor                    1, and any errors are sent to 2. If you do not want to have                    the error messages pop up, then you simply redirect it to /dev/null.                    Note that this will not stop information from being sent to                    standard output. For example, if you do not have permissions                    to read another user&#8217;s directory, you will not be able to list                    its contents</span></p>
<p><span class="big_black_bold">$ ls /root<br />
ls: /root: Permission denied<br />
$ ls /root 2&gt; /dev/null<br />
$ </span></p>
<p><span class="big_black">As you can see, the error was not printed                    out this time. The same applies for other programs and for file                    descriptor 1. If you do not want to see the normal output of                    a program, that is, you want it to run silently, you can redirect                    it to /dev/null. Now if you do not want to see either standard                    input or error, then you do it this way</span></p>
<p><span class="big_black_bold">$ ls /root &gt; /dev/null 2&gt;&amp;1 </span></p>
<p><span class="big_black">This means that the program will send                    any output or errors that occur to /dev/null so you never ever                    see them. </span></p>
<p><span class="big_black">Now what if you want your shell script                    to return a value upon exiting? The exit command takes one argument.                    A number to return. Normally the number 0 is used to denote                    a successful exit, no errors occurred. Anything higher or lower                    than 0 normally means an error has occurred. This is for you,                    the programmer to decide. Let us look at this program</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
if [ -f "/etc/passwd" ]; then<br />
echo &#8220;Password file exists.&#8221;<br />
exit 0<br />
else<br />
echo &#8220;No such file.&#8221;<br />
exit 1<br />
fi </span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">By specifying return values upon exit,                    other shell scripts you write making use of this script will                    be able to capture its return value. </span></p>
<p><span class="big_black"><strong>Porting your Bash Scripts</strong></span></p>
<p><span class="big_black">It is important to try and write your                    scripts so that they are portable. This means that if your script                    works under Linux, then it should work in another Unix system                    with as little modification as possible, if any. In order to                    do this, you should be careful when calling external programs.                    First consider the question, &#8220;Is this program going to                    be available in this other Unix variant?&#8221;. Recall that                    if you have a program foo that works in the same way as echo,                    you can use it in echo&#8217;s place. However, if it so happens that                    your script is used on a Unix system without the foo program,                    then your script will start reporting errors. Also, keep in                    mind that different versions of bash may have new ways to do                    different things. For instance, the VAR=$(ps) does the same                    thing as VAR=`ps`, but realize that older shells, for instance                    the Bourne shell, only recognizes the latter syntax. Be sure                    that if you are going to distribute your scripts, you include                    a README text file which warns the user of any surprises, including,                    the version of bash the script was tested on, as well as any                    required programs or libraries needed by the script to run. </span></p>
<p><span class="big_black"><strong>Conclusion</strong></span></p>
<p><span class="big_black">That completes the introduction to                    bash scripting. Your scripting studies are not complete however.                    There is more to cover. As I said, this is an introduction.                    However, this is enough to get you started on modifying shell                    programs and writing your own. If you really want to master                    shell scripting, I recommend buying Learning the bash shell,                    2nd Edition by O&#8217;Reilly &amp; Associates, Inc. bash scripting                    is great for your everyday administrative use. But if you are                    planning on a much bigger project, you will want to use a much                    more powerful language like C or Perl.</span></p>
<p>Good luck&#8230;</p>
<p>source : http://www.codecoffee.com</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=15&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/a-quick-introduction-to-bash-programming-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>A Quick Introduction to Bash Programming &#8211; Part 1</title>
		<link>http://indolunix.wordpress.com/2008/09/03/a-quick-introduction-to-bash-programming-part-1/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/a-quick-introduction-to-bash-programming-part-1/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 12:03:58 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=9</guid>
		<description><![CDATA[Bash programming is a topic that can be dealt with in a couple of pages or hundreds of pages. Harold Rodriguez explains Bash programming in this 2 Part tutorial. His slick and excellent style of writing has enabled him to cover all the essential features of bash programing in a few pages. If you have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=9&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span class="big_black">Bash programming is a topic that can                    be dealt with in a couple of pages or hundreds of pages. <span class="big_black_italic">Harold                    Rodriguez</span> explains Bash programming in this 2 Part tutorial.                    His slick and excellent style of writing has enabled him to</span><span class="big_black"> cover all the essential features of bash programing in a few                    pages.</span></p>
<p>If you have never programmed in Bash before, this is the best                    place to begin. In case you have a little knowledge of bash,                    then too you could have a look.. a lot of interesting scripts                    have been explained by Harold.<span id="more-9"></span></p>
<p><span class="big_black_bold">Introduction</span></p>
<p><span class="big_black">Like all the shells available in Linux,                    the Bourne Again SHell is not only an excellent command line                    shell, but a scripting language in itself. Shell scripting,                    allows you to fully utilize the shell&#8217;s abilities and to automate                    a lot of tasks that would otherwise require a lot of commands                    to perform. A lot of programs lying around your Linux box are                    shell scripts. If you are interested in learning how they work,                    or in modifying them, it is essential that you understand the                    bash syntax and semantics. In addition, by understanding the                    bash language, you will be able to write your own programs to                    do things exactly the way you want them done. </span></p>
<p><span class="big_black_bold">Programming or Scripting?</span></p>
<p><span class="big_black">People who are new to programming are                    generally confused as to what the difference is between a programming                    and scripting language. Programming languages are generally                    a lot more powerful and a lot faster than scripting languages.                    Examples of programming languages are C, C++, and Java. Programming                    languages generally start from source code (a text file containing                    instructions on how the final program is to be run) and are                    compiled (built) into an executable. This executable is not                    easily ported into different operating systems. For instance,                    if you were to write a C program in Linux, you would not be                    able to run that C program in a Windows 98 system. In order                    to do so, you would have to recompile the source code under                    the Windows 98 system. A scripting language also starts from                    source code, but is not compiled into an executable. Rather,                    an interpreter reads the instructions in the source file and                    executes each instruction. Unfortunately, because the interpreter                    has to read each instruction, interpreted programs are generally                    slower than compiled programs. The main advantage is that you                    can easily port the source file to any operating system and                    have it interpreted there right on the spot. bash is a scripting                    language. It is great for small programs, but if you are planning                    on doing major applications, a programming language might be                    more beneficial to you. Other examples of scripting languages                    are Perl, Lisp, and Tcl. </span></p>
<p><span class="big_black_bold">What do you need to know? </span></p>
<p><span class="big_black">Writing your own shell scripts requires                    you to know the very basic everyday Linux commands. For example,                    you should know how to copy, move, create new files, etc. The                    one thing you must know how to do is to use a text editor. There                    are three major text editors in Linux, vi, emacs and pico. If                    you are not familiar with vi or emacs, go for pico or some other                    easy to use text editor. </span></p>
<p><span class="big_black_bold">Warning!!!!</span></p>
<p><span class="big_black">Do not practice scripting as the root                    user! Anything could happen! I will not be held responsible                    if you accidentally make a mistake in the coding and screw up                    your system. You have been warned! Use a normal user account                    with no root privileges. You may even want to create a new user                    just for scripting practice. This way the worst thing that can                    happen is the user&#8217;s directory gets blown away.<br />
</span></p>
<p><span class="big_black_bold">Your first Bash program</span></p>
<p><span class="big_black">Our first program will be the classical                    &#8220;Hello World&#8221; program. Yes, if you have programmed                    before, you must be sick of this by now. However, this is traditional,                    and who am I to change tradition? The &#8220;Hello World&#8221;                    program simply prints the words &#8220;Hello World&#8221; to the                    screen. So fire up your text editor, and type the following                    inside it:</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr bgcolor="#f0f0f0">
<td class="big_black">#!/bin/bash<br />
echo &#8220;Hello World&#8221;</td>
</tr>
</tbody>
</table>
<p><span class="big_black">The first line tells Linux to use the                    bash interpreter to run this script. In this case, bash is in                    the /bin directory. If bash is in a different directory on your                    system, make the appropriate changes to the line. Explicitly                    specifying the interpreter is very important, so be sure you                    do it as it tells Linux which interpreter to use to run the                    instructions in the script. The next thing to do is to save                    the script. We will call it hello.sh. With that done, you need                    to make the script executable</span></p>
<p><span class="big_black_bold">$ chmod 700 ./hello.sh</span></p>
<p><span class="big_black">Refer to the manual page for chmod                    if you do not know how to change permissions for a file. Once                    this is done, you will be able to run your program just by typing                    its name:</span></p>
<p><span class="big_black"><span class="big_black_bold">$ ./hello.sh<br />
Hello World </span></span></p>
<p><span class="big_black">There it is! Your first program! Boring                    and useless as it is, this is how everyone starts out. Just                    remember the process here. Write the code, save the file, and                    make it executable with chmod.<br />
</span></p>
<p><span class="big_black_bold">Commands, Commands and Commands</span></p>
<p><span class="big_black">What exactly did your first program                    do? It printed the words &#8220;Hello World&#8221; to the screen.                    But how did it do that? It used commands. The only line of code                    you wrote in the program was echo &#8220;Hello World&#8221;. Well,                    which one is the command? echo. The echo program takes one argument                    and prints that argument to the screen. </span></p>
<p><span class="big_black">An argument is anything that follows                    after you type the program name. In this case, &#8220;Hello World&#8221;                    was the argument that you passed to echo When you type the command                    ls /home/root, the argument to ls is /home/root. So what does                    this all mean? It means that if you have a program that takes                    an argument and prints it to the screen, you can use that instead                    of using echo. Let us assume that we have a program called foo                    This program will take a single argument, a string of words,                    and print them to the screen. We can rewrite our program as                    such</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td class="big_black">#!/bin/bash<br />
foo &#8220;Hello World&#8221;</td>
</tr>
</tbody>
</table>
<p><span class="big_black">Save it and chmod it and then run it</span></p>
<p><span class="big_black"><span class="big_black_bold">$ ./hello<br />
Hello World </span></span></p>
<p><span class="big_black">The exact same result. Was there any                    unique code at all? No. Did you really write anything? Not unless                    you are the author of the echo program. All you did, was to                    embed the echo program into your shell program, with a argument                    already given. A real world example of an alternative to the                    echo command is the printf command. printf offers more control,                    especially if you are familiar with C programming. In fact,                    the exact same result could have been done without making a                    shell program</span></p>
<p><span class="big_black"><span class="big_black_bold">$ echo                    &#8220;Hello World&#8221;<br />
Hello World </span></span></p>
<p><span class="big_black">bash shell scripting offers a wide                    variety of control and is easy to learn. As you have just seen,                    you use Linux commands to write your shell programs with. Your                    shell program is a collection of other programs, specially put                    together to perform a task. </span></p>
<p><span class="big_black"><br />
<span class="big_black_bold"> A More Useful Program</span></span></p>
<p><span class="big_black">We will write a program that will move                    all files into a directory, and then delete the directory along                    with its contents, and then recreate the directory. This can                    be done with the following commands</span></p>
<p><span class="big_black"><span class="big_black_bold">$ mkdir                    trash<br />
$ mv * trash<br />
$ rm -rf trash<br />
$ mkdir trash</span></span></p>
<p><span class="big_black"><br />
Instead of having to type all that interactively on the shell,                    write a shell program instead</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
mkdir trash<br />
mv * trash<br />
rm -rf trash<br />
mkdir trash<br />
echo &#8220;Deleted all files!&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Save it as clean.sh and now all you                    have to do is to run clean.sh and it moves all files to a directory,                    deletes them, recreates the directory, and even prints a message                    telling you that it successfully deleted all files. So remember,                    if you find that you are doing something that takes a while                    to type over and over again, consider automating it with a shell                    program. </span></p>
<p><span class="big_black_bold">Comments</span></p>
<p><span class="big_black">Comments help to make your code more                    readable. They do not affect the output of your program. They                    are specially made for you to read. All comments in bash begin                    with the hash symbol: &#8220;#&#8221;, except for the first line                    (#!/bin/bash). The first line is not a comment. Any lines after                    the first line that begin with a &#8220;#&#8221; is a comment.                    Take the following piece of code</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
# this program counts from 1 to 10:<br />
for i in 1 2 3 4 5 6 7 8 9 10; do<br />
echo $i<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"><br />
Even if you do not know bash scripting, you immediately know                    what the above program does, because of the comment. It is good                    practice to make use of comments. You will find that if you                    need to maintain your programs in the future, having comments                    will make things easier. </span></p>
<p><span class="big_black_bold">Variables</span></p>
<p><span class="big_black">Variables are basically &#8220;boxes&#8221;                    that hold values. You will want to create variables for many                    reasons. You will need it to hold user input, arguments, or                    numerical values. Take for instance the following piece of code</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=12<br />
echo &#8220;The value of variable x is $x&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"><br />
What you have done here, is to give x the value of 12. The line                    echo &#8220;The value of variable x is $x&#8221; prints the current                    value of x. When you define a variable, it must not have any                    whitespace in between the assignment operator: &#8220;=&#8221;.                    Here is the syntax</span></p>
<p><span class="big_black_italic">variable_name=this_value</span></p>
<p><span class="big_black">The values of variables can be accessed                    by prefixing the variable name with a dollar symbol: &#8220;$&#8221;.                    As in the above, we access the value of x by using echo $x. </span></p>
<p><span class="big_black">There are two types of variables. Local                    variables, and environmental variables. Environmental variables                    are set by the system and can usually be found by using the                    env command. Environmental variables hold special values. For                    instance, if you type</span></p>
<p><span class="big_black_bold">$ echo $SHELL<br />
/bin/bash </span></p>
<p><span class="big_black">You get the name of the shell you are                    currently running. Environmental variables are defined in /etc/profile                    and ~/.bash_profile. The echo command is good for checking the                    current value of a variable, environmental, or local.</span></p>
<p><span class="big_black"><span class="big_black_bold"><span class="medium_black"><span class="medium_black_bold">Note                    :</span></span></span><span class="medium_black"> Setting of                    Environmental variables are explained in detail in <a href="http://www.codecoffee.com/tipsforlinux/articles/030.html"></a><a href="http://indolunix.wordpress.com/2008/09/03/how-to-set-shell-environment-variables-bash-shell/" target="_blank"><span class="medium_black" style="color:#3333ff;">Article  How to set Shell Environment</span></a><span class="medium_black" style="color:#3333ff;"> Variables</span> .                    The</span></span><span class="medium_black"> article also explains                    some features of the bash shell. </span></p>
<p>If you are still having problems understanding why we need variables,                    here is a good example</p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
echo &#8220;The value of x is 12.&#8221;<br />
echo &#8220;I have 12 pencils.&#8221;<br />
echo &#8220;He told me that the value of x is 12.&#8221;<br />
echo &#8220;I am 12 years old.&#8221;<br />
echo &#8220;How come the value of x is 12?&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Okay, now suppose you decide that you                    want the value of x to be 8 instead of 12. What do you do? You                    have to change all the lines of code where it says that x is                    12. But wait&#8230; there are other lines of code with the number                    12. Should you change those too? No, because they are not associated                    with x. Confusing right? Now, here is the same example, only                    it is using variables</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=12     # assign the value 12 to variable x<br />
echo &#8220;The value of x is $x.&#8221;<br />
echo &#8220;I have 12 pencils.&#8221;<br />
echo &#8220;He told me that the value of x is $x.&#8221;<br />
echo &#8220;I am 12 years old.&#8221; echo &#8220;How come                          the value of x is $x?&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Here, we see that $x will print the                    current value of variable x, which is 12. So now, if you wanted                    to change the value of x to 8, all you have to do, is to change                    the line x=12 to x=8, and the program will automatically change                    all the lines with $x to show 8, instead of 12. The other lines                    will be unaffected. Variables have other important uses as well,                    as you will see later on. </span></p>
<p><span class="big_black_bold">Control Structures</span></p>
<p><span class="big_black">Control structures allow your program                    to make decisions and to make them more compact. More importantly                    as well, it allows us to check for errors. So far, all we have                    done is write programs that start from the top, and go all the                    way to the bottom until there are no more commands left in the                    program to run. For instance</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
cp /etc/foo .<br />
echo &#8220;Done.&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">This little shell program, call it                    bar.sh, copies a file called /etc/foo into the current directory                    and prints &#8220;Done&#8221; to the screen. This program will                    work, under one condition. You must have a file called /etc/foo.                    Otherwise here is what happens</span></p>
<p><span class="big_black"><span class="big_black_bold">$ ./bar.sh<br />
cp: /etc/foo: No such file or directory<br />
Done.</span></span></p>
<p><span class="big_black"><br />
So you can see, there is a problem. Not everyone who runs your                    program will have /etc/foo in their system. It would perhaps                    be better if your program checked if /etc/foo existed, and then                    if it did, it would proceed with the copying, otherwise, it                    would quit. In pseudo code, this is what it would look like</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center">
<tbody>
<tr>
<td><span class="big_black_italic">if /etc/code exists, then<br />
copy /etc/code to the current directory<br />
print &#8220;Done.&#8221; to the screen.<br />
otherwise,<br />
print &#8220;This file does not exist.&#8221;                        to the screen<br />
exit</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> Can this be done in bash? Of course!                    The collection of bash control structures are, if, while, until,                    for and case. Each structure is paired, meaning it starts with                    a starting &#8220;tag&#8221; and ends with an ending &#8220;tag&#8221;.                    For instance, the if structure starts with if, and ends with                    fi. Control structures are not programs found in your system.                    They are a built in feature of bash. Meaning that from here                    on, you will be writing your own code, and not just embedding                    programs into your shell program. </span></p>
<p><span class="big_black_bold">if &#8230; else &#8230; elif &#8230; fi</span></p>
<p><span class="big_black">One of the most common structures is                    the if structure. This allows your program to make decisions,                    like, &#8220;do this if this conditions exists, else, do something                    else&#8221;. To use the if structure effectively, we must make                    use of the test command. test checks for conditions, that is,                    existing files, permissions, or similarities and differences.                    Here is a rewrite on bar.sh</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
if test -f /etc/foo<br />
then<br />
# file exists, so copy and print a message.<br />
cp /etc/foo .<br />
echo &#8220;Done.&#8221;<br />
else<br />
# file does NOT exist, so we print a message                          and exit.<br />
echo &#8220;This file does not exist.&#8221;<br />
exit<br />
fi</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Notice how we indent lines after then                    and else. Indenting is optional, but it makes reading the code                    much easier in a sense that we know which lines are executed                    under which condition. Now run the program. If you have /etc/foo,                    then it will copy the file, otherwise, it will print an error                    message. test checks to see if the file /etc/foo exists. The                    -f checks to see if the argument is a regular file. Here is                    a list of test&#8217;s options</span></p>
<table border="1" cellspacing="1" cellpadding="3" width="50%" align="center">
<tbody>
<tr bgcolor="#ebebeb">
<td colspan="2">
<div class="big_black_bold">Command Line                          Parameters for &#8216; test &#8216;</div>
</td>
</tr>
<tr>
<td width="18%" height="19">
<div><span class="big_black">-d </span></div>
</td>
<td width="82%" height="19"><span class="big_black">check                        if the file is a directory</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-e </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        exists</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-f </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        is a regular file</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-g </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        has SGID permissions</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-r </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        is readable</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-s </span></div>
</td>
<td width="82%"><span class="big_black">check if the file&#8217;s                        size is not 0</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-u </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        has SUID permissions</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-w </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        is writetable</span></td>
</tr>
<tr>
<td width="18%">
<div><span class="big_black">-x </span></div>
</td>
<td width="82%"><span class="big_black">check if the file                        is executable</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> else is used when you want your program                    to do something else if the first condition is not met. There                    is also the elif which can be used in place of another if within                    the if. Basically elif stands for &#8220;else if&#8221;. You use                    it when the first condition is not met, and you want to test                    another condition. </span></p>
<p><span class="big_black">If you find that you are uncomfortable                    with the format of the if and test structure, that is</span></p>
<p><span class="big_black"><span class="big_black_bold">if test                    -f /etc/foo<br />
then</span></span></p>
<p><span class="big_black">then, you can do it like this</span></p>
<p><span class="big_black_bold">if [ -f /etc/foo ]; then </span></p>
<p><span class="big_black">The square brackets form test. If you                    have experience in C programming, this syntax might be more                    comfortable. Notice that there has to be white space surrounding                    both square brackets. The semicolon: &#8220;;&#8221; tells the                    shell that this is the end of the command. Anything after the                    semicolon will be run as though it is on a separate line. This                    makes it easier to read basically, and is of course, optional.                    If you prefer, just put then on the next line. </span></p>
<p><span class="big_black">When using variables with test, it                    is a good idea to have them surrounded with quotation marks.                    Example:</span></p>
<p><span class="big_black_bold">if [ "$name" -eq 5 ];                    then<br />
</span></p>
<p><span class="big_black_bold"><span class="big_black">the -eq                    operator will be explained later on in this article.</span><br />
</span></p>
<p><span class="big_black_bold">while &#8230; do &#8230; done</span></p>
<p><span class="big_black">The while structure is a looping structure.                    Basically what it does is, &#8220;while this condition is true,                    do this until the condition is no longer true&#8221;. Let us                    look at an example</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
while true; do<br />
echo &#8220;Press CTRL-C to quit.&#8221;<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> true is actually a program. What this                    program does is continuously loop over and over without stopping.                    Using true is considered to be slow because your shell program                    has to call it up first and then run it. You can use an alternative,                    the &#8220;:&#8221; command</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
while :; do<br />
echo &#8220;Press CTRL-C to quit.&#8221;<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> This achieves the exact same thing,                    but is faster because it is a built in feature in bash. The                    only difference is you sacrifice readability for speed. Use                    whichever one you feel more comfortable with. Here is perhaps,                    a much more useful example, using variables</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=0;     # initialize x to 0<br />
while [ "$x" -le 10 ]; do<br />
echo &#8220;Current value of x: $x&#8221;<br />
# increment the value of x:<br />
x=$(expr $x + 1)<br />
sleep 1<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">As you can see, we are making use of                    the test (in its square bracket form) here to check the condition                    of the variable x. The option -le checks to see if x is less                    than, or equal to the value 10. In English, the code above says,                    &#8220;While x is less than 10 or equal to 10, print the current                    value of x, and then add 1 to the current value of x.&#8221;.                    sleep 1 is just to get the program to pause for one second.                    You can remove it if you want. As you can see, what we were                    doing here was testing for equality. Check if a variable equals                    a certain value, and if it does, act accordingly. Here is a                    list of equality tests</span></p>
<table border="1" cellspacing="1" cellpadding="3" width="50%" align="center">
<tbody>
<tr bgcolor="#ebebeb">
<td colspan="2" height="20">
<div><span class="big_black_bold">Checks                          equality between numbers</span></div>
</td>
</tr>
<tr>
<td>
<div><span class="big_black">x -eq y</span></div>
</td>
<td><span class="big_black">Check is x is equal to y</span></td>
</tr>
<tr>
<td>
<div><span class="big_black">x -ne y</span></div>
</td>
<td><span class="big_black">Check if x is not equal to y</span></td>
</tr>
<tr>
<td>
<div><span class="big_black">x -gt y</span></div>
</td>
<td><span class="big_black">Check if x is greater than y</span></td>
</tr>
<tr>
<td>
<div><span class="big_black">x -lt y</span></div>
</td>
<td><span class="big_black">Check if x is less than y</span></td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="1" cellpadding="3" width="50%" align="center">
<tbody>
<tr bgcolor="#ebebeb">
<td colspan="2">
<div><span class="big_black_bold">Checks                          equality between strings</span></div>
</td>
</tr>
<tr>
<td height="13">
<div><span class="big_black">x = y</span></div>
</td>
<td height="13"><span class="big_black">Check if x is the                        same as y</span></td>
</tr>
<tr>
<td>
<div><span class="big_black">x != y</span></div>
</td>
<td><span class="big_black">Check if x is not the same as                        y</span></td>
</tr>
<tr>
<td>
<div><span class="big_black">-n x</span></div>
</td>
<td><span class="big_black">Evaluates to true if x is not                        null</span></td>
</tr>
<tr>
<td>
<div><span class="big_black">-z x</span></div>
</td>
<td><span class="big_black">Evaluates to true if x is null</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> The above looping script we wrote                    should not be hard to understand, except maybe for this line</span></p>
<p><span class="big_black_bold">x=$(expr $x + 1) </span></p>
<p><span class="big_black">The comment above it tells us that                    it increments x by 1. But what does $(&#8230;) mean? Is it a variable?                    No. In fact, it is a way of telling the shell that you want                    to run the command expr $x + 1, and assign its result to x.                    Any command enclosed in $(&#8230;) will be run</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
me=$(whoami)<br />
echo &#8220;I am $me.&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">Try it and you will understand what                    I mean. The above code could have been written as follows with                    equivalent results</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
echo &#8220;I am $(whoami).&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> You decide which one is easier for                    you to read. There is another way to run commands or to give                    variables the result of a command. This will be explained later                    on. For now, use $(&#8230;). </span></p>
<p><span class="big_black_bold">until &#8230; do &#8230; done</span></p>
<p><span class="big_black">The until structure is very similar                    to the while structure. The only difference is that the condition                    is reversed. The while structure loops while the condition is                    true. The until structure loops until the condition is true.                    So basically it is &#8220;until this condition is true, do this&#8221;.                    Here is an example</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=0<br />
until [ "$x" -ge 10 ]; do<br />
echo &#8220;Current value of x: $x&#8221;<br />
x=$(expr $x + 1)<br />
sleep 1<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">This piece of code may look familiar.                    Try it out and see what it does. Basically, until will continue                    looping until x is either greater than, or equal to 10. When                    it reaches the value 10, the loop will stop. Therefore, the                    last value printed for x will be 9. </span></p>
<p><span class="big_black_bold">for &#8230; in &#8230; do &#8230; done</span></p>
<p><span class="big_black">The for structure is used when you                    are looping through a range of variables. For instance, you                    can write up a small program that prints 10 dots each second</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
echo -n &#8220;Checking system for errors&#8221;<br />
for dots in 1 2 3 4 5 6 7 8 9 10; do<br />
echo -n &#8220;.&#8221;<br />
done<br />
echo &#8220;System clean.&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">In case you do not know, the -n option                    to echo prevents a new line from automatically being added.                    Try it once with the -n option, and then once without to see                    what I mean. The variable dots loops through values 1 to 10,                    and prints a dot at each value. Try this example to see what                    I mean by the variable looping through the values</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
for x in paper pencil pen; do<br />
echo &#8220;The value of variable x is: $x&#8221;<br />
sleep 1<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">When you run the program, you see that                    x will first hold the value paper, and then it will go to the                    next value, pencil, and then to the next value, pen. When it                    finds no more values, the loop ends. </span></p>
<p><span class="big_black">Here is a much more useful example.                    The following program adds a .html extension to all files in                    the current directory</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
for file in *; do<br />
echo &#8220;Adding .html extension to $file&#8230;&#8221;<br />
mv $file $file.html<br />
sleep 1<br />
done</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> If you do not know, * is a wild card                    character. It means, &#8220;everything in the current directory&#8221;,                    which is in this case, all the files in the current directory.                    All files in the current directory are then given a .html extension.                    Recall that variable file will loop through all the values,                    in this case, the files in the current directory. mv is then                    used to rename the value of variable file with a .html extension. </span></p>
<p><span class="big_black_bold">case &#8230; in &#8230; esac</span></p>
<p><span class="big_black">The case structure is very similar                    to the if structure. Basically it is great for times where there                    are a lot of conditions to be checked, and you do not want to                    have to use if over and over again. Take the following piece                    of code</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=5     # initialize x to 5<br />
# now check the value of x:<br />
case $x in<br />
0) echo &#8220;Value of x is 0.&#8221;<br />
;;<br />
5) echo &#8220;Value of x is 5.&#8221;<br />
;;<br />
9) echo &#8220;Value of x is 9.&#8221;<br />
;;<br />
*) echo &#8220;Unrecognized value.&#8221;<br />
esac</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">The case structure will check the value                    of x against 3 possibilities. In this case, it will first check                    if x has the value of 0, and then check if the value is 5, and                    then check if the value is 9. Finally, if all the checks fail,                    it will produce a message, &#8220;Unrecognized value.&#8221;.                    Remember that &#8220;*&#8221; means &#8220;everything&#8221;, and                    in this case, &#8220;any other value other than what was specified&#8221;.                    If x holds any other value other than 0, 5, or 9, then this                    value falls into the *&#8217;s category. When using case, each condition                    must be ended with two semicolons. Why bother using case when                    you can use if? Here is the equivalent program, written with                    if. See which one is faster to write, and easier to read</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=5     # initialize x to 5<br />
if [ "$x" -eq 0 ]; then<br />
echo &#8220;Value of x is 0.&#8221;<br />
elif [ "$x" -eq 5 ]; then<br />
echo &#8220;Value of x is 5.&#8221;<br />
elif [ "$x" -eq 9 ]; then<br />
echo &#8220;Value of x is 9.&#8221;<br />
else<br />
echo &#8220;Unrecognized value.&#8221;<br />
fi</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black_bold">Quotation Marks</span></p>
<p><span class="big_black">Quotation marks play a big part in                    shell scripting. There are three types of quotation marks. They                    are the double quote: &#8220;, the forward quote: &#8216;, and the                    back quote: `. Does each of them mean something? Yes. </span></p>
<p class="medium_black"><span class="medium_black_bold">Note :</span><a href="http://www.codecoffee.com/tipsforlinux/articles/26-1.html"></a><a href="http://indolunix.wordpress.com/2008/09/03/wildcards-quotes-back-quotes-and-apostrophes-in-shell-commands/"><span style="color:#3333ff;"><span class="medium_black">Wildcards, Quotes, Back Quotes, Apostrophes in shell                    commands ( * ? [] &#8221; ` &#8216;)</span></span></a> deals with special                    characters in great detail. Please refer to that article in                    case you are not familiar with the use of these special characters                    in shell commands. Below is a quick explanation of some of those                    features</p>
<p><span class="big_black">The double quote is used mainly to                    hold a string of words and preserve whitespace. For instance,                    &#8220;This string contains whitespace.&#8221;. A string enclosed                    in double quotes is treated as one argument. For instance, take                    the following examples</span></p>
<p><span class="big_black"><span class="big_black_bold">$ mkdir                    hello world<br />
$ ls -F<br />
hello/     world/ </span></span></p>
<p><span class="big_black">Here we created two directories. mkdir                    took the strings hello and world as two arguments, and thus                    created two directories. Now, what happens when you do this:</span></p>
<p><span class="big_black"><span class="big_black_bold">$ mkdir                    &#8220;hello world&#8221;<br />
$ ls -F<br />
hello/     hello world/     world/ </span></span></p>
<p><span class="big_black">It created a directory with two words.                    The quotation marks made two words, into one argument. Without                    the quotation marks, mkdir would think that hello was the first                    argument, and world, the second. </span></p>
<p><span class="big_black">Forward quotes are used primarily to                    deal with variables. If a variable is enclosed in double quotes,                    its value will be evaluated. If it is enclosed in forward quotes,                    its value will not be evaluated. To make this clearer, try the                    following example</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">#!/bin/bash<br />
x=5     # initialize x to 5<br />
# use double quotes<br />
echo &#8220;Using double quotes, the value of x is: $x&#8221;<br />
# use forward quotes<br />
echo &#8216;Using forward quotes, the value of x is: $x&#8217;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black"> See the difference? You can use double                    quotes if you do not plan on using variables for the string                    you wish to enclose. In case you are wondering, yes, forward                    quotes can be used to preserve whitespace just like double quotes</span></p>
<p><span class="big_black"><span class="big_black_bold">$ mkdir                    &#8216;hello world&#8217;<br />
$ ls -F<br />
hello world/ </span></span></p>
<p><span class="big_black">Back quotes are completely different                    from double and forward quotes. They are not used to preserve                    whitespace. If you recall, earlier on, we used this line</span></p>
<p><span class="big_black_bold">x=$(expr $x + 1) </span></p>
<p><span class="big_black">As you already know, the result of                    the command expr $x + 1 is assigned to variable x. The exact                    same result can be achieved with back quotes:</span></p>
<p><span class="big_black_bold">x=`expr $x + 1` </span></p>
<p><span class="big_black">Which one should you use? Whichever                    one you prefer. You will find the back quote used more often                    than the $(&#8230;). However, I find $(&#8230;) easier to read, especially                    if you have something like this</span></p>
<table border="0" cellspacing="2" cellpadding="1" width="80%" align="center" bgcolor="#f0f0f0">
<tbody>
<tr>
<td><span class="big_black">$!/bin/bash<br />
echo &#8220;I am `whoami`&#8221;</span></td>
</tr>
</tbody>
</table>
<p><span class="big_black">This was just the beginning. You will                  learn lots of more concepts in the concluding part of this article.                  Till then happy shell scripting..</span></p>
<p>source : http://www.codecoffee.com</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=9&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/a-quick-introduction-to-bash-programming-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>About File Permissions</title>
		<link>http://indolunix.wordpress.com/2008/09/03/about-file-permissions/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/about-file-permissions/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 11:49:37 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=7</guid>
		<description><![CDATA[Preliminary Each file in Linux inherits a set of properties.  One vital set of properties is the file&#8217;s permissions.  Permissions determine what any particular user (or group of users) is able to do that file.  File permissions help prevent unwanted deletion and safeguard your data.  In order to use Linux&#8217;s file permissions, you need to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=7&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Preliminary</h2>
<p>Each file in Linux inherits a set of properties.  One vital set of properties is the file&#8217;s permissions.  Permissions determine what any particular <a href="http://www.linuxsa.org.au/tips/file-permissions.html#userdef">user</a> (or <a href="http://www.linuxsa.org.au/tips/file-permissions.html#groupdef">group</a> of users) is able to do that file.  <a>File permissions</a> help prevent unwanted deletion and safeguard your data.  In order to use Linux&#8217;s file permissions, you need to understand Linux&#8217;s categories of <a href="http://www.linuxsa.org.au/tips/file-permissions.html#userdef">users</a> and <a href="http://www.linuxsa.org.au/tips/file-permissions.html#groupdef">groups.</a></p>
<h2><a name="#userdef">Categories of Users</a></h2>
<p>You are asked to enter a login name and password when you first log into Linux.  When we talk of a <em>user</em>, we refer to the account issuing commands to the operating system at the time and <em>not</em> to the actual person operating the computer.  As soon as Linux authenticates your login name and password you &#8220;become&#8221; that user and operate using that user account.</p>
<p><a name="#groupdef">Users belong to one or more groups. </a> (The SuperUser allocates Users to particular groups.)  Each user has a default group.<span id="more-7"></span></p>
<p>Linux organises users into three broad categories (the values in brackets are Linux&#8217;s accepted abbreviations):</p>
<ol>
<li><strong>user (u)</strong> The owner of the file.  A user who creates a file automatically owns it.  Only the owner and the SuperUser (alias root) can change the permissions of a file.</li>
<li><strong>group (g)</strong> The group of a file.  One <a href="http://www.linuxsa.org.au/tips/file-permissions.html#groupdef">group</a> of users is given special access to a file.  This is determined by the file owner.</li>
<li><strong>others (o)</strong> All other users on the system.  In other words, every account except the file&#8217;s owner, or users in the file&#8217;s group.</li>
</ol>
<h2><a name="#permdef">File access attributes</a></h2>
<p>Each file has a set of attributes specifying what the user in each category (user, group, others) can do with the file.  Here are the three types of access available in Linux:</p>
<ol>
<li><strong>read (r)</strong> This category of users can display, but not necessarily alter, the file.</li>
<li><strong>write (w)</strong> This category of users can alter the file (but not necessarily read it.)</li>
<li><strong>execute (x)</strong> This category of user can execute (i.e. run) the file.</li>
</ol>
<h2>Displaying file permissions</h2>
<p>You can display a file&#8217;s permissions by executing the <em>ls -l</em> command.  Here is a sample output:</p>
<pre>lloy0076@localhost bin2dec]$ ls -l
total 23
-rw-r-----  1 lloy0076 root    286 Aug 28 02:17 b2d.lex
-rwxr-xr-x  1 lloy0076 root  20390 Aug 28 02:17 b2d
-rw-r--r--  1 lloy0076 root     49 Aug 27 22:08 Makefile</pre>
<p>You can see the file permissions in the left-most column.  The first character is usually a `-&#8217; or `d&#8217;.  This actually refers to the type of the file, and does not refer to the file permissions; a `-&#8217; indicates the file is a &#8220;normal&#8221; file, and a `d&#8217; indicates the it is a directory.  Other letters indicate files with special meanings to Linux.  The next nine characters refer to file permissions.</p>
<p>The first three (of the nine permission characters) shows what access to the file is permitted for the owner; the next three shows the permissions for anyone in the file&#8217;s group; and the last three are for those classified as other.  A letter (r, w or x) indicates that the permission for that particular user, group or other is set, and a `-&#8217; indicates that the permission is not available.</p>
<p>Schematically you could represent it like this:</p>
<table border="1">
<tbody>
<tr align="center">
<td>-</td>
<td>rwx</td>
<td>rwx</td>
<td>rwx</td>
</tr>
<tr align="center">
<td>type</td>
<td>user</td>
<td>group</td>
<td>other</td>
</tr>
</tbody>
</table>
<p>[-rwxrwxrwx]</p>
<p>The b2d.lex file is owned by the user lloy0076, who can read and write it; anyone in the root group can read the file; and nobody else is permitted any access at all.  The b2d file is also owned by lloy0076, who has read, write and execute permission on it.  Anyone in the &#8220;root&#8221; group has read and execute permissions for the file; and so does everyone else.</p>
<h2>Changing file permissions</h2>
<h3>chmod</h3>
<p>To change permissions use the command <em>chmod</em> from the command-line.  You must be the owner of the file (or you must be the SuperUser.)  Take care when changing a file&#8217;s permissions and be especially careful when you are working with any system files.</p>
<p>The basic format for chmod is:</p>
<ul>
<li><em>chmod [OPTION]&#8230; MODE&#8230; FILE&#8230;</em></li>
</ul>
<p>FILE is a file or directory, which will have its permissions set.  MODE is the permissions being set on the [FILE].</p>
<p>You can use a number of OPTIONs with chmod. Two useful ones are:</p>
<ul>
<li><strong>-v</strong> chmod produces verbose output; useful to see exactly what chmod is doing</li>
<li><strong>-R</strong> chmod will descend (recursively) into all subdirectories, changing all file permissions contained within.  This means that it will iterate through all the files in all the specified FILE&#8217;s subdirectories (if it has any) changing them at it goes.  <em>This option should be used with care</em>.</li>
</ul>
<p>You use the `+&#8217;, &#8216;-&#8217; or &#8216;=&#8217; action symbols to add, subtract or set file permissions.  Here is how you do it:</p>
<ol>
<li>Specify the category of users with the abbreviations for the categories (u, g or o).  A special category ,&#8217;a', also exists which means all users.  You can add these together like ug, which means the user and the group</li>
<li>Specify an appropriate action symbol (&#8216;+&#8217;, &#8216;-&#8217; or &#8216;=&#8217;)</li>
<li>Specify a file access attribute (r, w, or x).  As with the specification for users, you can add these together like rw, which means read and write permissions</li>
</ol>
<p>Here are two examples of how to use chmod on a file called `test&#8217;; for our purposes we will assume that `test&#8217; has absolutely <em>no</em> access permited at the start:</p>
<ol>
<li><tt>chmod ug+rx test</tt> This gives read and execute permissions to the user and group, the permissions are now -r-xr-x&#8212;</li>
<li><tt>chmod a-x test</tt> This removes execute permissions from all users, after these two steps, the permissions are -r&#8211;r&#8212;&#8211;</li>
<li><tt>chmod u=x test</tt> This sets execute permission, and removes all others, for the owner.  After these three steps the permissions are &#8212;xr&#8212;&#8211;</li>
</ol>
<p>An easy way to determine what the mode string, ug+rx for example, means is by actually saying it <em>fully</em> out loud.  This example would be user; group; add; read permissions; execute permission.  Although it is terrible English, it should be plain what this particular mode is trying to achieve.</p>
<h2>Conclusion</h2>
<p><em>info chmod</em> and <em>man chmod</em> are both good reference points for chmod.  Whilst chmod also understands another way of specifying modes &#8211; the <em>octal</em> method &#8211; I find it easier to explain this method to new Linux Users.  The <em>octal</em> method is adequately explained in the man pages for chmod.</p>
<p>Please submit any suggestions to:  lloy0076@senet.com.au</p>
<p>source : http://www.linuxsa.org.au</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=7&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/about-file-permissions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>Disk Partitioning</title>
		<link>http://indolunix.wordpress.com/2008/09/03/disk-partitioning/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/disk-partitioning/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 11:48:20 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=5</guid>
		<description><![CDATA[By Alex Garner. How do I know what size to make my disk partitions? This is one of the more often asked questions I hear. Usually the answer is &#8220;It depends&#8221;, so here is my experience with partitioning Linux boxes for various applications over the last few years. First of all it helps to know [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=5&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>By <span style="font-size:xx-small;"> <a href="mailto:alex@netcraft.com.au">Alex Garner</a>.</span></p>
<p><tt>How do I know what size to make my disk partitions? </tt></p>
<p>This is one of the more often asked questions I hear. Usually the answer is &#8220;It depends&#8221;, so here is my experience with partitioning Linux boxes for various applications over the last few years.</p>
<p>First of all it helps to know exactly what the file systems are all used for and where stuff goes. A good reference for this sort of thing is in the Linux Documentation Project&#8217;s &#8220;System Administrator&#8217;s Guide&#8221; or SAG. You can find a good bit of info on the file system <a href="http://www.tcm.hut.fi/%7Eviu/linux/sag/sag-0.6.2.html/c218.html">here</a></p>
<p>Alternatively, if you have a copy of &#8220;A Practical Guide to Linux&#8221;, then check out page 74.<span id="more-5"></span></p>
<p>Here is a brief rundown&#8230;</p>
<pre>/       Root file system. Should just contain /bin, /sbin, /dev,
        /root,
        /lib, and /etc.
/usr    Programmes and source code.
/var    Variable data, such as spools, man pages, news and mail
        queues, database data.
/boot   Boot kernels.
/home   User data and "stuff".
/tmp    Temporary file locations</pre>
<p>The / file system will never need to be more than 100Meg. Make it that.</p>
<p>The /usr file system will vary depending on how big your initial installation is and how much extra software you download. For a RedHat 6.2 minimal install you&#8217;ll be needing about 250 to 300 Meg (typical server), and for a full install you need around 1.5 Gig (typical workstation). Other distributions will need more or less, but this is a good guide. Any extra software you download may also go in this file system, so if you are planning installing an office suite or a cad package, be aware it that it may go in here.</p>
<p>If you are installing software to build from a tar ball or installing software that isn&#8217;t part of a vendor&#8217;s distribution, like an RPM or a DEB is, you will probably want to install it in the /usr/local file system. This file system is usually left untouched by the installation or upgrade process of a linux distribution and is ideal for installing third party software. If you plan on doing a lot of this, a separate partition is a great idea, because if you want to do a re-install rather than an upgrade, you can simply tell your distribution not to format the /usr/local file system when installing and you will leave your third party software in tact. The format of the /usr/local file system is almost identical to the / file system.  Handy huh?</p>
<p>/usr/local/bin and /usr/local/sbin are also the correct place to put any scripts you may write after you have your system up and running. This is preferable to placing them in /usr/bin and /usr/sbin or even /bin and /sbin, as these should really be static and left the way the distribution intended them. It also makes backing up a system much easier if all your locally created scripts are in one convenient place.</p>
<p>The /var file system is the most varying file system, hence its name. The function of the machine will determine how much you need. For a vanilla system, I recommend 400 Meg. This is usually sufficient for a workstation. If you are building a proxy server, you will need a separate partition, but preferably a separate disk, for /var/spool/squid. The same goes for a mail server, except the file systems of interest are /var/spool/mqueue and /var/spool/mail. The size of /var/spool/mail will depend on how much storage you want for user&#8217;s mailboxes, and the size /var/spool/mqueue will depend on how much mail &#8216;in transit&#8217; you wish to spool. Mail server&#8217;s acting as a secondary MX might need a lot here.</p>
<p>There are other smaller directories in /var/spool that are of interest, so I would recommend a /var/spool of 300 to 500 Meg for any server application in conjunction with the /var of 400 Meg. For a workstation you may be able to use the 400Meg /var partition to house your /var/spool as well, but it may pay to enlarge it a bit.</p>
<p>/var/log, as the name suggests, is the final resting place for logs. Once again the size of this will depend on the function of the system, but as a general rule it is highly recommended that you have a separate /var/log to your /var partition, regardless of the machine&#8217;s function. This way any stray system logs that fill up will have no effect on your system other than stopping logging. This goes for both servers and workstations alike. If you are running a heavily loaded proxy, mail or web server, you will need heaps and heaps of disk space here. Fully loaded proxy servers in peering arrangements can easily generate hundreds of thousands of bytes of log files an hour. The same goes for mail servers. The mail can come in and go out very quickly on a fast link, but the log files stay around.  You also don&#8217;t want a slash-dotted web site to fill up your logging directory, so careful thought here will pay off in the future.</p>
<p>The /var file system is also often used for the storage of database data. /var/db or /var/lib is the file system that is used, and you will need to keep this big enough to hold your data. Often a separate fast SCSI disk or RAID will make your database much faster. IO is often the biggest bottleneck in database systems, and an IDE drive in /var/db or /var/lib wont help.</p>
<p>The /boot directory is probably the most useful file system, and often the most forgotten. Having your kernels on a separate partition will make rescuing a system that has crashed a whole lot easier. This means that booting the system and recovering the partitions can be attacked as two separate tasks. Having a small /boot in a primary partition is also the best cure for the famous &#8220;I just installed linux and now all I get is &#8216;LI&#8217;&#8221; LILO installation problems. LILO still has issues with hard drive space above 1024 cylinders. A small 20 Meg /boot partition as the first primary partition on the system will alleviate this. Some distributions, such as RedHat, are smart enough to assign automatically the first primary partition to /boot for just this reason.</p>
<p>/home is where you hang your hat. It is also where you &#8220;keep your stuff&#8221;. Files you download, projects, mail, documents, mp3&#8242;s, everything.  This is the equivalent of Windows&#8217; &#8220;My Documents&#8221;, &#8220;C:/download&#8221;, the desktop, etc. Even if the system is only used by you at your desk, and no-one else, you should still have your own home directory in the /home file system. Don&#8217;t be tempted to add partitions to the root file system such as /scripts, /downloads, etc. You are breaking stuff when you do that. Linux is still a true multiuser operating system, even if you are the only person using it. Try to keep this in mind when building a partition table. This all starts to make sense when you stop logging in as root, and start logging in as a regular user. It never ceases to amaze me how many people run X as root. *sigh*.</p>
<p>Many distributions nowadays are geared towards easy and quick upgrades and everything has it&#8217;s place. If you keep you stuff in /home/yourname and no-where else, you can be sure that when your next upgrade of linux comes, you can just chuck in the CD and hit &#8220;upgrade&#8221; and your Metallica mp3&#8242;s will still be there when your system comes back on-line.</p>
<p>/home is also where the storage file system for a file server should go. The same is true for web server pages, and ftp server data. Obviously if you are building a web server, have a separate /home/httpd file system on a nice fast SCSI disk. Same with /home/ftp.</p>
<p>Sometimes it&#8217;s a great idea to have a separate /tmp directory, because temporary files can get out of control. Having /tmp on the same partition as the root file system can cause problems if you scan a 60 Meg picture into a graphics manipulation programme and it decides to store it in /tmp.</p>
<p>The only other partition of major interest is the swap partition. It is often a good idea to place this in the physical middle of the drive. Then the heads have less far to travel to swap out data when the system gets loaded. Alternatively you can just throw more memory at the problem.</p>
<p>Now I&#8217;ll give you a few &#8216;real life&#8217; examples of servers that I maintain. The names have been changed to protect the innocent.</p>
<p>Here is my bog standard workstation. It runs X. It may get used for some server functions in the future, so there is lot&#8217;s of space ready. I even have a big block of space hanging of /mnt/tmp, and one day I&#8217;m sure I&#8217;ll think of a use for it.</p>
<pre>[alex@workstation alex]$ df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda13               85530     34264     46850  42% /
/dev/hda1               101089      6802     89068   7% /boot
/dev/hda6              1517920    154616   1286196  11% /home
/dev/hda12             2150420        20   2041160   0% /mnt/tmp
/dev/hda10              248895        27    236018   0% /tmp
/dev/hda5              2016016   1292380    621224  68% /usr
/dev/hda7               758936     37592    682792   5% /var
/dev/hda9               497829       657    471470   0% /var/log
/dev/hda8               758936       292    720092   0% /var/spool</pre>
<p>This next beast is a mail server. Note the use of separate drives for critical server file systems.</p>
<pre>[alex@mail alex]$ df
Filesystem        1k-blocks      Used Available Use% Mounted on
/dev/sda12            79941     39339     36474  52% /
/dev/sda1             21011      5463     14463  27% /boot
/dev/sda11           701636     43332    622664   7% /home
/dev/sda9            202031        13    191587   0% /tmp
/dev/sda5           1210800    456856    692436  40% /usr
/dev/sda7            496695      7069    463981   2% /var
/dev/sda6           1009724    197880    760552  21% /var/log
/dev/sda8            496695       982    470068   0% /var/spool
/dev/sdb1           4382932    766640   3393648  18% /var/spool/mail</pre>
<p>Here is a proxy server. Mix of SCSI and IDE.</p>
<pre>[root@proxy /root]# df
Filesystem        1k-blocks      Used Available Use% Mounted on
/dev/hda11           101485     28799     67446  30% /
/dev/hda1             23393      2647     19538  12% /boot
/dev/hda7            199085      2101    186704   1% /home
/dev/hda8             81954       985     76737   1% /tmp
/dev/hda5            809556    170444    597988  22% /usr
/dev/hda6            199085      4243    184562   2% /var
/dev/hda10          1611224     10408   1518968   1% /var/log
/dev/sda1          17654736    354220  16403692   2% /var/spool/squid</pre>
<p>When partitioning a machine for use, it is often a bad idea to install everything into a single / partition. Even if you don&#8217;t need separate partitions, the practice you get from partitioning disks and learning how much space each partition needs in a given situation will be invaluable when someone asks you to build a server for them. Spend a few minutes before installation considering the functions of the machine you are building and this will yield a useful and efficient partition table.  The more often you do it the more of a feel you will get for how much space your distribution needs for different tasks. Now that you have the above information there is no excuse for poor partitioning, and you can help make the world a safer place for data!</p>
<p>Next time you see&#8230;</p>
<pre>[lame@nothought /]$ df
Fileystem      1k-blocks      Used Available Use% Mounted on
/dev/hda1       17654736   1354220  15403692   8% /</pre>
<p>&#8230;you can do something about it!</p>
<p>source : http://www.linuxsa.org.au/</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=5&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/disk-partitioning/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
		<item>
		<title>Linux, Clocks, and Time</title>
		<link>http://indolunix.wordpress.com/2008/09/03/linux-clocks-and-time/</link>
		<comments>http://indolunix.wordpress.com/2008/09/03/linux-clocks-and-time/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 11:46:50 +0000</pubDate>
		<dc:creator>indolunix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://indolunix.wordpress.com/?p=3</guid>
		<description><![CDATA[ntroduction This document explains how to set your computer&#8217;s clock from Linux, how to set your timezone, and other stuff related to Linux and how it does its time-keeping. Your computer has two timepieces; a battery-backed one that is always running (the &#8220;hardware&#8221;, &#8220;BIOS&#8221;, or &#8220;CMOS&#8221; clock), and another that is maintained by the operating [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=3&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>ntroduction</h2>
<p>This document explains how to set your computer&#8217;s clock from Linux, how to set your timezone, and other stuff related to Linux and how it does its time-keeping.</p>
<p>Your computer has two timepieces; a battery-backed one that is always running (the &#8220;hardware&#8221;, &#8220;BIOS&#8221;, or &#8220;CMOS&#8221; clock), and another that is maintained by the operating system currently running on your computer (the &#8220;system&#8221; clock).  The hardware clock is generally only used to set the system clock when your operating system boots, and then from that point until you reboot or turn off your system, the system clock is the one used to keep track of time.</p>
<p>On Linux systems, you have a choice of keeping the hardware clock in UTC/GMT time or local time.  The preferred option is to keep it in UTC because then daylight savings can be automatically accounted for. The only disadvantage with keeping the hardware clock in UTC is that if you dual boot with an operating system (such as DOS) that expects the hardware clock to be set to local time, the time will always be wrong in that operating system.<span id="more-3"></span></p>
<h2>Setting your timezone</h2>
<p>The timezone under Linux is set by a symbolic link from <code>/etc/localtime</code><sup><a href="http://www.linuxsa.org.au/tips/time.html#note1">[1]</a></sup> to a file in the <code>/usr/share/zoneinfo</code><sup><a href="http://www.linuxsa.org.au/tips/time.html#note2">[2]</a></sup> directory that corresponds with what timezone you are in.  For example, since I&#8217;m in South Australia, <code>/etc/localtime</code> is a symlink to <code>/usr/share/zoneinfo/Australia/South</code>.  To set this link, type:</p>
<p><code>ln -sf ../usr/share/zoneinfo/<em>your/zone</em> /etc/localtime</code></p>
<p>Replace <code><em>your/zone</em></code> with something like <code>Australia/NSW</code> or <code>Australia/Perth</code>.  Have a look in the directories under <code>/usr/share/zoneinfo</code> to see what timezones are available.</p>
<p><a name="note1">[1]</a> This assumes that <code>/usr/share/zoneinfo</code> is linked to <code>/etc/localtime</code> as it is under Red Hat Linux.</p>
<p><a name="note2">[2]</a> On older systems, you&#8217;ll find that <code>/usr/lib/zoneinfo</code> is used instead of <code>/usr/share/zoneinfo</code>.  See also the later section &#8220;<a href="http://www.linuxsa.org.au/tips/time.html#wrongtime">The time in some applications is wrong</a>&#8221;.</p>
<h2>Setting UTC or local time</h2>
<p>When Linux boots, one of the initialisation scripts will run the <code>/sbin/hwclock</code> program to copy the current hardware clock time to the system clock.  <code>hwclock</code> will assume the hardware clock is set to local time unless it is run with the <code>--utc</code> switch.  Rather than editing the startup script, under Red Hat Linux you should edit the <code>/etc/sysconfig/clock</code> file and change the &#8220;<code>UTC</code>&#8221; line to either &#8220;UTC=true&#8221; or &#8220;UTC=false&#8221; as appropriate.</p>
<h2>Setting the system clock</h2>
<p>To set the system clock under Linux, use the <code>date</code> command.  As an example, to set the current time and date to July 31, 11:16pm, type &#8220;<code>date 07312316</code>&#8221; (note that the time is given in 24 hour notation).  If you wanted to change the year as well, you could type &#8220;<code>date 073123161998</code>&#8221;.  To set the seconds as well, type &#8220;<code>date 07312316.30</code>&#8221; or &#8220;<code>date 073123161998.30</code>&#8221;.  To see what Linux thinks the current local time is, run <code>date</code> with no arguments.</p>
<h2>Setting the hardware clock</h2>
<p>To set the hardware clock, my favourite way is to set the system clock first, and then set the hardware clock to the current system clock by typing &#8220;<code>/sbin/hwclock --systohc</code>&#8221; (or &#8220;<code>/sbin/hwclock --systohc --utc</code>&#8221; if you are keeping the hardware clock in UTC).  To see what the hardware clock is currently set to, run <code>hwclock</code> with no arguments.  If the hardware clock is in UTC and you want to see the local equivalent, type &#8220;<code>/sbin/hwclock --utc</code>&#8221;</p>
<h2><a name="wrongtime">The time in some applications is wrong</a></h2>
<p>If some applications (such as <code>date</code>) display the correct time, but others don&#8217;t, and you are running Red Hat Linux 5.0 or 5.1, you most likely have run into a bug caused by a move of the timezone information from <code>/usr/lib/zoneinfo</code> to <code>/usr/share/zoneinfo</code>.  The fix is to create a symbolic link from <code>/usr/lib/zoneinfo</code> to <code>/usr/share/zoneinfo</code>: &#8220;<code>ln -s ../share/zoneinfo /usr/lib/zoneinfo</code>&#8221;.</p>
<h2>Summary</h2>
<ul>
<li><code>/etc/sysconfig/clock</code> sets whether the hardware clock is stored as UTC or local time.</li>
<li>Symlink <code>/etc/localtime</code> to <code>/usr/share/zoneinfo/...</code> to set your timezone.</li>
<li>Run &#8220;<code>date MMDDhhmm</code>&#8221; to set the current system date/time.</li>
<li>Type &#8220;<code>/sbin/hwclock --systohc [--utc]</code>&#8221; to set the hardware clock.</li>
</ul>
<h2>Other interesting notes</h2>
<p>The Linux kernel always stores and calculates time as the number of seconds since midnight of the 1st of January 1970 UTC regardless of whether your hardware clock is stored as UTC or not.  Conversions to your local time are done at run-time.  One neat thing about this is that if someone is using your computer from a different timezone, they can set the TZ environment variable and all dates and times will appear correct for their timezone.</p>
<p>If the number of seconds since the 1st of January 1970 UTC is stored as an signed 32-bit integer (as it is on your Linux/Intel system), your clock will stop working sometime on the year 2038. Linux has no inherent Y2K problem, but it does have a year 2038 problem.  Hopefully we&#8217;ll all be running Linux on 64-bit systems by then.  64-bit integers will keep our clocks running quite well until aproximately the year 292271-million.</p>
<h2>Other programs worth looking at</h2>
<ul>
<li><code>rdate</code> &#8211; get the current time from a remote machine; can be used to set the system time.</li>
<li><code>xntpd</code> &#8211; like <code>rdate</code>, but it&#8217;s extremely accurate and you need a permanent &#8216;net connection.  <code>xntpd</code> runs continuously and accounts for things like network delay and clock drift, but there&#8217;s also a program (<code>ntpdate</code>) included that just sets the current time like rdate does.</li>
</ul>
<h2>Further information</h2>
<ul>
<li><code>date(1)</code></li>
<li><code>hwclock(8)</code></li>
<li><code>/usr/doc/HOWTO/mini/Clock</code></li>
</ul>
<p>Source  : http://www.linuxsa.org.au</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/indolunix.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/indolunix.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/indolunix.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/indolunix.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/indolunix.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/indolunix.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/indolunix.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/indolunix.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/indolunix.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/indolunix.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=indolunix.wordpress.com&amp;blog=4711869&amp;post=3&amp;subd=indolunix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://indolunix.wordpress.com/2008/09/03/linux-clocks-and-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c3a8a5a921a40a046148a2ced7988c83?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">indolunix</media:title>
		</media:content>
	</item>
	</channel>
</rss>
