<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>UNIX Tutorial: Learn UNIX</title>
	
	<link>http://www.unixtutorial.org</link>
	<description>Learn UNIX</description>
	<pubDate>Mon, 30 Jun 2008 15:19:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/UnixTutorial" type="application/rss+xml" /><item>
		<title>How To Determine Physical Memory Size in Linux</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/314901600/</link>
		<comments>http://www.unixtutorial.org/2008/06/physical-memory-size-in-linux/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 21:12:04 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Basic stuff]]></category>

		<category><![CDATA[Linux]]></category>

		<category><![CDATA[meminfo]]></category>

		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=57</guid>
		<description><![CDATA[If you're logged in at some remote Linux system and need to quickly confirm the amount of available memory, there's a few commands you will find quite useful.
free - free and used memory stats
free command is the most obvious choice for a first command when it comes to your RAM.
Simply run it without any parameters, [...]]]></description>
			<content:encoded><![CDATA[<p>If you're logged in at some remote Linux system and need to quickly confirm the amount of available memory, there's a few commands you will find quite useful.</p>
<h3>free - free and used memory stats</h3>
<p>free command is the most obvious choice for a first command when it comes to your RAM.</p>
<p>Simply run it without any parameters, and it will show you something like this:</p>
<pre>ubuntu# <strong>free</strong>
             total       used       free     shared    buffers     cached
Mem:       4051792    4024960      26832          0      63768    3131532
-/+ buffers/cache:     829660    3222132
Swap:      4096492      43212    4053280</pre>
<p>For this exercise, you're only interested in the "total" column of the first line. 4051792 confirms that my home PC seems to have around 4Gb of memory available for Ubuntu to use.</p>
<h3>Using dmesg to check memory size as recognized by Linux kernel</h3>
<p><strong>dmesg </strong>command shows you the last status messages reported by your OS kernel, and since every boot procedure includes scanning the hardware and confirming the devices and resources recognized by the kernel, you can see some basic information by using <strong>dmesg</strong>.</p>
<p>For our purpose, we need to filter out the memory stats:</p>
<pre>ubuntu# <strong>dmesg | grep Memory</strong>
[   18.617904] Memory: 4043492k/5242880k available (2489k kernel code, 150360k reserved, 1318k data, 320k init)</pre>
<p>Once again, the overall amount of memory confirms that 4Gb of RAM were still found during the last time my PC booted up.</p>
<h3>Using /proc/meminfo to confirm the RAM size</h3>
<p>/proc/meminfo is one of the special files managed by Linux kernel. It's a clear text presentation of the most vital memory stats of your system (this means you can do something like <strong>cat /proc/meminfo</strong> to see all the parameters)</p>
<p>This is what you need to do to get the total size of your physical memory:</p>
<pre>ubuntu# <strong>grep MemTotal /proc/meminfo</strong>
MemTotal:      4051792 kB</pre>
<p>That's it for today, enjoy!</p>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/314901600" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/06/physical-memory-size-in-linux/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/06/physical-memory-size-in-linux/</feedburner:origLink></item>
		<item>
		<title>Unix scripts: basic arithmetic operations</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/312963063/</link>
		<comments>http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 11:30:01 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[arithmetic]]></category>

		<category><![CDATA[assignment]]></category>

		<category><![CDATA[Scripts]]></category>

		<category><![CDATA[Unix]]></category>

		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=56</guid>
		<description><![CDATA[As I was writing the Time and date in Unix scripts post last week, I've realized that there's one really useful feature of Unix shell scripting which I haven't covered on my pages here: it's the double bracket parenthesis which allows you to evaluate arithmetic expressions.
Basic arithmetic calculations in Unix scripts
In most scenarios, your script [...]]]></description>
			<content:encoded><![CDATA[<p>As I was writing the <a title="time and date in unix" href="http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/">Time and date in Unix scripts</a> post last week, I've realized that there's one really useful feature of Unix shell scripting which I haven't covered on my pages here: it's the double bracket parenthesis which allows you to evaluate arithmetic expressions.</p>
<h3>Basic arithmetic calculations in Unix scripts</h3>
<p>In most scenarios, your script will need only basic arithmetic operations: summing or subtracting numbers, multiplying or dividing them. Using only these operations, you'll be able to implement all sorts of counters and calculate percentages or any other progress indicators you may need.</p>
<p>So far, I've only shown you this way of evaluating arithmetic expressions in Unix shell (yes, it's not a script - you can just type commands one after another right in your shell prompt):</p>
<pre>ubuntu$ <strong>START=1</strong>
ubuntu$ <strong>FINISH=5</strong>
ubuntu$ <strong>ELAPSED=`expr $FINISH - $START`</strong>
ubuntu$ <strong>echo $ELAPSED</strong>
4</pre>
<p>But an even better way to evaluate arithmetic expressions is to use the built-in functionality of double brackets. Just continue typing in your shell:</p>
<pre>ubuntu$ <strong>ELAPSED2=$(($FINISH - $START))</strong>
ubuntu$ <strong>echo $ELAPSED2</strong>
4</pre>
<p>You see, everything that is inside double brackets gets evaluated and then this calculated value is assigned to the variable. I'm using a different variable - ELAPSED2 - to clearly show that this calculation has nothing to do with the original ELAPSED variable.</p>
<h3>Assignment operator</h3>
<p>If you wonder about the theory behind such a way of evaluating expressions, I should talk a bit about <a title="variables in Unix scripts" href="http://www.unixtutorial.org/2008/05/variables-in-unix-shell/">using variables in Unix shells</a>.</p>
<p>As you probably remember, when you're assigning your variable a new value, you're simply specifying the name of the variable to the left of the expression, then use the <strong>=</strong> sign (it's not just a symbol but an assignment operator by the way), and to the right of this operator goes the expression which represents the new value:</p>
<pre>ubuntu$ <strong>MYVAR=123</strong></pre>
<h3>Variable substitution operator</h3>
<p>As you also remember, when you want to access the value of the variable, for example to print it out, you use the variable name with a leading <strong>$</strong> sign. That's because <strong>$</strong>, once again, isn't just a fancy symbol, but in fact a special character in Bash - it's a variable substitution operator, which means your Unix script substitutes the variable name you're specifying in the script with the value of the variable:</p>
<pre>ubuntu$ <strong>echo $MYVAR</strong>
3</pre>
<p>Using the substitution operator (<strong>$</strong>) before the name of a variable is crucial. If you omit the <strong>$</strong> sign, your shell will think you have simply passed it a text string, and will not recognize it to be a name of the variable:</p>
<pre>ubuntu$ <strong>echo MYVAR</strong>
MYVAR</pre>
<h3>Expression evaluation explained</h3>
<p>Coming back to arithmetic evaluations, you can probably see now that a line similar to this:</p>
<pre>ubuntu$ <strong>ELAPSED=$(($FINISH - $START))</strong></pre>
<p>uses the variable substitution operator (<strong>$</strong>) and it's therefore only logical that the further specified expression gets evaluated and the ELAPSED variable gets assigned with the resulting value.</p>
<p>Once again, using <strong>$</strong> sign is crucial as without it your shell will not understand you:</p>
<pre>ubuntu$ <strong>ELAPSED=(($FINISH - $START))</strong>
sh: syntax error near unexpected token `('</pre>
<p>So, what do you think? I hope this makes sense to you, and will be sure to use this technique from now on - once you get used to it, it's really easy to write and read shell scripts based on this functionality.</p>
<p>Let me know if there's anything else you'd like to hear an explanation of! I'll do my best! <img src='http://www.unixtutorial.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>See also:</h3>
<ul>
<li><strong><a href="http://tldp.org/LDP/abs/html/part2.html">Basics of BASH scripting</a></strong></li>
<li><strong><a title="variables in scripts" href="http://www.unixtutorial.org/2008/05/variables-in-unix-shell/">Using variables in Unix scripts</a></strong></li>
</ul>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/312963063" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/</feedburner:origLink></item>
		<item>
		<title>Find files which belong to a user or Unix group</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/311091674/</link>
		<comments>http://www.unixtutorial.org/2008/06/find-files-which-belong-to-a-user-or-unix-group/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 11:30:03 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Basic stuff]]></category>

		<category><![CDATA[find]]></category>

		<category><![CDATA[gid]]></category>

		<category><![CDATA[uid]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=54</guid>
		<description><![CDATA[If you need to find all the files owned by a certain Unix user or group in a given directory, it's actually very easy to do using find command.
How to find files owned by a user
If you know the username, this is the command you might use to locate all the files which belong to [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to find all the files owned by a certain Unix user or group in a given directory, it's actually very easy to do using <a title="find command" href="http://www.unixcommand.org/find/">find command</a>.</p>
<h3>How to find files owned by a user</h3>
<p>If you know the username, this is the command you might use to locate all the files which belong to it:</p>
<pre>ubuntu$ <strong>find /var -user www-data</strong>
/var/cache/apache2/mod_disk_cache
/var/lib/awstats
&#8230;</pre>
<p>In this case, we're looking for all the files and directories owned by a webserver used <em>www-data</em> under <em>/var.</em></p>
<h3>Find files owned by a Unix group</h3>
<p>If you're doing a broader search, you may be interested in identifying all the files owned by a Unix group. Here's how you can do it:</p>
<pre>ubuntu$ <strong>find /usr -group staff</strong>
/usr/local/lib/site_ruby
/usr/local/lib/site_ruby/1.8
/usr/local/lib/site_ruby/1.8/x86_64-linux
/usr/local/lib/python2.5
/usr/local/lib/python2.5/site-packages
/usr/local/lib/python2.4
/usr/local/lib/python2.4/site-packages
/usr/local/share/fonts</pre>
<p>In this example, we're using <strong>find </strong>to locate all the files in <em>/usr</em> owned by the <em>staff</em> group.</p>
<h3>Locate files by UID or GID</h3>
<p>If you're more comfortable dealing with Unix user IDs (UIDs) and group IDs (GIDs), you can use them with <strong>find </strong>command as well. In this example, I'm looking for the temporary files created by myself (my UID on that system is 1000):</p>
<pre>ubuntu$ <strong>find /var/tmp -uid 1000</strong>
/var/tmp/photos.rar
/var/tmp/mysql.tar.gz
&#8230;</pre>
<p>I presume this post answers your questions on the topic, but do let me know if there's anything else you'd like me to explain!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="unix find" href="http://www.unixcommand.org/find/">Unix commands: find</a></strong></li>
<li><strong><a title="unix file ownership" href="http://www.unixtutorial.org/2008/03/how-to-find-the-owner-of-a-file-in-unix/">How to find an owner of a Unix file</a></strong></li>
<li><strong><a title="directory location in Unix" href="http://www.unixtutorial.org/2008/04/find-location-of-directory-in-unix/">Finding a location of a directory</a><br />
</strong></li>
</ul>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/311091674" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/06/find-files-which-belong-to-a-user-or-unix-group/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/06/find-files-which-belong-to-a-user-or-unix-group/</feedburner:origLink></item>
		<item>
		<title>Unix Scripting: Time and Date</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/309568936/</link>
		<comments>http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 11:30:01 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Scripts]]></category>

		<category><![CDATA[Unix]]></category>

		<category><![CDATA[date]]></category>

		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=55</guid>
		<description><![CDATA[If you have followed this blog for a while, you should remember how to use variables in Unix shell scripts.
Going further, I'd like to show you some basics of working with time and date in your scripts - generating all sorts of timestamps and timing some parts of your script for reporting purposes.
The history of [...]]]></description>
			<content:encoded><![CDATA[<p>If you have followed this blog for a while, you should remember how to use <a title="variables in Unix scripts" href="http://www.unixtutorial.org/2008/05/variables-in-unix-shell/">variables in Unix shell scripts</a>.</p>
<p>Going further, I'd like to show you some basics of working with time and date in your scripts - generating all sorts of timestamps and timing some parts of your script for reporting purposes.</p>
<h3>The history of (Unix) time</h3>
<p>In case you didn't know, time in Unix starts with a <strong>Unix epoch</strong>, sometimes also referred to as POSIX epoch.  <em>The Unix epoch</em> is the time <em>00:00:00 UTC on January 1<a>, 1970</a></em><a>.</a></p>
<p>Most of Unix-like system today store and calculate this time according to Unix epoch, which means they all have an internal counter which counts the number of seconds elapsed since midnight of January 1, 1970.</p>
<p>As you can imagine, this is a pretty big number (it passed  1,000,000,000 seconds back in 2001, just so that you know), and reporting in directly would not be very useful to real humans. Because of this, most of Unix time and date reporting commands and functions do the conversion from Unix time into something more meaningful to the end user.</p>
<p>While for the vast majority of time and date related tasks this conversion is good, a task of timing certain events can sometimes benefit from using raw second counters and simply subtracting them when needed.</p>
<h3>Getting Unix time and date</h3>
<p>In Unix shells, the easiest way to get a current time and date is to use the <a title="date command" href="http://www.unixcommand.org/date/"><strong>date </strong>command</a>:</p>
<pre>ubuntu$ <strong>date</strong>
Tue Jun 10 10:46:07 IST 2008</pre>
<p><a title="date" href="http://www.unixcommand.org/date/"><strong>date</strong> </a>is a very smart command, and apart from this default behavior it supports template system for printing the current time and date - so you can use it to report only specific part of the time and date like the current hour or the day of a week or the year.</p>
<p>Read the man page for <strong>date </strong>(type <strong>man date</strong> in your shell prompt) to learn all the details, but this is how you use date output templates:</p>
<pre>ubuntu$ <strong>date "+%b %d, %Y"</strong>
Jun 10, 2008</pre>
<p>In this example, the %b parameter in this template represents the short name of the current month, %d is the day of the month, and %Y is the four-digit representation of the current year.</p>
<h3>Timing parts of your Unix script</h3>
<p>If you're after timing some parts of your script, you'll need to use two variables: one for saving the time before the start of an observed part of the script, and another one for saving the time after the same piece of code.</p>
<p>These two variables will be used to subtract the time and report the elapsed time in seconds, so in order to do this we'll need the date command to report time in seconds. That's why I've given you an introduction to Unix time earlier: date reports the number of seconds since the Unix epoch:</p>
<pre>ubuntu$ <strong>date +%s</strong>
1213091896</pre>
<p>If you run it just a few seconds later, you'll see a different number:</p>
<pre>ubuntu$ <strong>date +%s</strong>
1213091922</pre>
<p>That's why, if such values are saved and then subtracted, we'll get the elapsed time in seconds.</p>
<p>Here's a simple script showing how this is done:</p>
<pre>#!/bin/bash
#
START=`date +%s`
echo "Script start time (Unix epoch): $START"
#
echo "- sleeping for 3 seconds..."
sleep 3
echo "- sleeping for 2 seconds more..."
sleep 2
#
FINISH=`date +%s`
echo "Script finish time (Unix epoch): $START"
#
ELAPSED=`expr $FINISH - $START`
echo "Elapsed time: $ELAPSED"</pre>
<p>And if you run it, you will see this output:</p>
<pre>ubuntu$ <strong>/tmp/time-example.sh</strong>
Script start time (Unix epoch): 1213092131
- sleeping for 3 seconds&#8230;
- sleeping for 2 seconds more&#8230;
Script finish time (Unix epoch): 1213092131
Elapsed time: 5</pre>
<p>That's all I wanted to show you today. In future posts, I'll show you a few more things you can do regarding timing and timestamps in Unix. Until then - good luck with your scripting, and feel free to ask if you need any more help!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="Unix time" href="http://en.wikipedia.org/wiki/Unix_time">Unix time - a great article on Wikipedia</a></strong></li>
<li><strong><a title="date command" href="http://www.unixcommand.org/date/">date command in Unix</a><br />
</strong></li>
<li><strong><a title="Unix shell variables" href="http://www.unixtutorial.org/2008/05/variables-in-unix-shell/">Working with variables in Unix shell</a></strong></li>
</ul>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/309568936" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/</feedburner:origLink></item>
		<item>
		<title>How To Compare Directories in Unix</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/307991766/</link>
		<comments>http://www.unixtutorial.org/2008/06/how-to-compare-directories-in-unix/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 12:36:36 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Basic stuff]]></category>

		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=53</guid>
		<description><![CDATA[Certain situations require you to quickly confirm which files between two directories are different, and while your particular requirements may suggest writing a script for this task, I want to make sure you're familiar with the basics first - majority of directory comparisons can be done using diff command (yes, that's right - the same [...]]]></description>
			<content:encoded><![CDATA[<p>Certain situations require you to quickly confirm which files between two directories are different, and while your particular requirements may suggest writing a script for this task, I want to make sure you're familiar with the basics first - majority of directory comparisons can be done using <strong>diff </strong>command (yes, that's right - the same one used for <a title="Comparing text files" href="http://www.unixtutorial.org/2008/02/compare-text-files-using-diff/">comparing files</a>).</p>
<h3>Why compare directories?</h3>
<p>First of all, let's agree on why you may need to compare directories. There's a few possible reasons:</p>
<ul>
<li><strong>identifying if some files are missing from one of the directories</strong> - can be useful when you want to make sure two directories with configuration files for a certain package are identical - files can be different, but the same files are present in the same locations for both directories</li>
<li><strong>confirming if files in two directories are the same</strong> - a typical task when comparing your actual data against a backup copy. When something goes wrong, this is one of the first things you do to make sure all the important files are not only present, but are actually the same as they have been when you took the last backup copy</li>
<li><strong>highlighting textual differences between files in directories</strong> - this is a useful exercise when you're looking at two similar directories and expect only minor changes between the files - version numbers, different file or directory names hardcoded in various scripts, etc.</li>
</ul>
<h3>Test setup for comparison exercises</h3>
<p>For today's post, I've created a set of directories and files to show how you can compare them. Here is the setup:</p>
<pre>ubuntu$ <strong>find /tmp/dir1 /tmp/dir2</strong>
/tmp/dir1
/tmp/dir1/file1
/tmp/dir1/file2
/tmp/dir1/dir11
/tmp/dir1/dir11/file11
/tmp/dir1/dir11/file12
/tmp/dir2
/tmp/dir2/file1
/tmp/dir2/dir11
/tmp/dir2/dir11/file11
/tmp/dir2/dir11/file12
/tmp/dir2/file3</pre>
<p>As you can see, I've got two directories: <em>/tmp/dir1</em> and <em>/tmp/dir2</em>, with a dir11 subdirectory in each of them. There's also a few files here and there, some of them missing from one of the directories specifically to be highlighted by our comparison exercises.</p>
<h3>Basic diff usage for comparing directories</h3>
<p>The easiest way to get started is to simply invoke diff command and specify two directories as command line parameters. Here's what you will probably see:</p>
<pre>ubuntu$ <strong>diff /tmp/dir1 /tmp/dir2</strong>
Common subdirectories: /tmp/dir1/dir11 and /tmp/dir2/dir11
diff /tmp/dir1/file1 /tmp/dir2/file1
1d0
&lt; New line
Only in /tmp/dir1: file2
Only in /tmp/dir2: file3</pre>
<p>This output confirms that <em>/tmp/dir1</em> and <em>/tmp/dir2 </em>both contain a dir11 directory, and also shows that <em>/tm/dir1/file1</em> and <em>/tmp/dir2/file1</em> are actually different files even though they have the same name. By default, diff compares such files and you can see the result of each comparison in the output. Also included are pointers to the files which are present only in one of the compared directories: you can see that <em>file2 </em>can only be found in <em>/tmp/dir1</em> and <em>file3 </em>was present only in <em>/tmp/dir2</em>.</p>
<h3>Find which files are missing in one of the directories</h3>
<p>From the example below, it is easy to deduct that the command line for identifying files missing in one of the directories will be this one:</p>
<pre>ubuntu$ <strong>diff /tmp/dir1 /tmp/dir2 | grep Only</strong>
Only in /tmp/dir1: file2
Only in /tmp/dir2: file3</pre>
<h3>Highlight the different files, not the differences</h3>
<p>If you're only interested in files which exist in both directory structures, but are different - you can use a special command line option. It will simply point the files out, without getting into any further details. You'll probably notice how this output is very similar to the default one:</p>
<pre>ubuntu$ <strong>diff &#8211;brief /tmp/dir1 /tmp/dir2</strong>
Common subdirectories: /tmp/dir1/dir11 and /tmp/dir2/dir11
Files /tmp/dir1/file1 and /tmp/dir2/file1 differ
Only in /tmp/dir1: file2
Only in /tmp/dir2: file3</pre>
<p>Note how instead of showing the difference between <em>file1 </em>in <em>/tmp/dir1</em> and <em>/tmp/dir2</em>, this time you only get told that these two files are different.</p>
<h3>How to recursively compare directories</h3>
<p>If you're dealing with a complex directory structure, you'll be glad to know that &#8211;recursive parameter for the <strong>diff </strong>command compares not only the immediate directories pointed to from the command line, but also walks through the full tree of subdirectories:</p>
<pre>ubuntu$ <strong>diff &#8211;recursive &#8211;brief /tmp/dir1 /tmp/dir2</strong>
Files /tmp/dir1/dir11/file12 and /tmp/dir2/dir11/file12 differ
Files /tmp/dir1/file1 and /tmp/dir2/file1 differ
Only in /tmp/dir1: file2
Only in /tmp/dir2: file3</pre>
<p>Feel better now? Many directory comparison tasks can be accomplished using the diff command, but if you're stuck with a particular problem which can't be solved using my examples - please leave a commend and I'll come up with a solution.</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="Comparing text files" href="http://www.unixtutorial.org/2008/02/compare-text-files-using-diff/">Comparing text files in Unix</a></strong></li>
<li><strong><a href="http://www.unixtutorial.org/2008/04/how-to-list-directories-in-a-directory-in-unix/">Listing directories in a directory</a></strong></li>
</ul>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/307991766" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/06/how-to-compare-directories-in-unix/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/06/how-to-compare-directories-in-unix/</feedburner:origLink></item>
		<item>
		<title>Get Username From UID in Unix</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/300496995/</link>
		<comments>http://www.unixtutorial.org/2008/05/get-username-from-uid-in-unix/#comments</comments>
		<pubDate>Thu, 29 May 2008 11:30:11 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=51</guid>
		<description><![CDATA[Finding out the username by user id (uid) in Unix is not as common a task as determining the uid by a username, but if you need to do it - I'll show you how.
How to find the username using user id (uid)
Simply use the getent command. Most common use for it is to query [...]]]></description>
			<content:encoded><![CDATA[<p>Finding out the username by user id (uid) in Unix is not as common a task as <a href="http://www.unixtutorial.org/2008/01/how-to-find-uid-in-unix/">determining the uid by a username</a>, but if you need to do it - I'll show you how.</p>
<h3>How to find the username using user id (uid)</h3>
<p>Simply use the <a title="getent unix command" href="http://www.unixcommand.org/getent">getent command</a>. Most common use for it is to query the passwd source for a username, like this:</p>
<pre>ubuntu$ <strong>getent passwd greys</strong>
greys:x:1000:113:Gleb Reys,,,:/home/greys:/bin/bash</pre>
<p>however, if you query for a user id instead (1000 in this case), <a title="getent" href="http://www.unixcommand.org/getent">getent</a> will work just as good:</p>
<pre>ubuntu$ <strong>getent passwd 1000</strong>
greys:x:1000:113:Gleb Reys,,,:/home/greys:/bin/bash</pre>
<p>That's all there is to it! Enjoy!</p>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/300496995" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/05/get-username-from-uid-in-unix/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/05/get-username-from-uid-in-unix/</feedburner:origLink></item>
		<item>
		<title>What UUIDs Are and How To Use Them in Ubuntu</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/288661382/</link>
		<comments>http://www.unixtutorial.org/2008/05/ubuntu-uuid-how-to/#comments</comments>
		<pubDate>Mon, 12 May 2008 12:30:09 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Ubuntu]]></category>

		<category><![CDATA[uuid]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=49</guid>
		<description><![CDATA[If you tried installing or upgrading Ubuntu recently, you probably noticed that all the storage devices are now using UUID - Universally Unique IDentifiers. I'm not claiming to know everything there is to know about UUIDs, but have become quite comfortable managing them lately, so hopefully this post will help you achieve the same.
What is [...]]]></description>
			<content:encoded><![CDATA[<p>If you tried installing or upgrading Ubuntu recently, you probably noticed that all the storage devices are now using UUID - Universally Unique IDentifiers. I'm not claiming to know everything there is to know about UUIDs, but have become quite comfortable managing them lately, so hopefully this post will help you achieve the same.</p>
<h3>What is a UUID exactly?</h3>
<p><strong>UUID</strong> is a <em>Universally Unique IDentifier</em>. It's a identification code given to each storage device you have on your system, aimed to help you uniquely identify each device no matter what.</p>
<p>UUIDs can be used to identify DVD drives, removable media (USB flashsticks) and each partition on any of your hard drives.</p>
<p>This is how a typical UUID looks:</p>
<p>c73a37c8-ef7f-40e4-b9de-8b2f81038441</p>
<h3>Why use UUID?</h3>
<h4>Reason 1: Truly unique identification</h4>
<p>UUID is the only way to guarantee you recognize the same drive or partition no matter what. For example, if you introduce to your system another hard drive, this might upset quite a few things, starting with the way your system boots up (or stops booting up upon the new drive introduction). Using UUID helps remedy most of such things.</p>
<h4>Reason 2: Device names are not always persistent</h4>
<p>Automatically assigned device names in your system are not consistent, they are according to the order of loading the kernel modules up during (most usually) the startup time, and thus the names will look different if you boot with one of your USB flashsticks attached and then reboot after you plug it out.</p>
<p>Generally, I've also found UUIDs really useful for mounting my removable media - I have a USB reader, one of the 24-in-1 kinds - it supports various types of cards and I use UUID to always mount the same card at the same location.</p>
<h4>Reason 3: Most critical functionality of your Ubuntu system already depends on UUIDs</h4>
<p>GRUB - your boot loader - relies on UUIDs as well. If you look into <strong>/boot/grub/menu.lst</strong> file, you'll find something similar to this:</p>
<p>title           Ubuntu hardy (development branch), kernel 2.6.24-16-generic<br />
root            (hd2,0)<br />
kernel          /boot/vmlinuz-2.6.24-16-generic root=UUID=c73a37c8-ef7f-40e4-b9de-8b2f81038441 ro quiet splash<br />
initrd          /boot/initrd.img-2.6.24-16-generic<br />
quiet</p>
<h3>List UUIDs for all your devices</h3>
<p>If you are using one of the recent releases of Ubuntu (UUIDs have been there since Edgy), you can use the blkid command to get a list of all the drives and partitions along with their UUIDs:</p>
<pre>ubuntu# <strong>blkid</strong>
/dev/sda1: UUID="2220CF8220CF5B83&#8243; TYPE="ntfs"
/dev/sda2: UUID="48E81F29E81F14B2&#8243; LABEL="DRIVE-D" TYPE="ntfs"
/dev/sdb1: UUID="c73a37c8-ef7f-40e4-b9de-8b2f81038441&#8243; SEC_TYPE="ext2&#8243; TYPE="ext3&#8243;
/dev/sdb5: TYPE="swap" UUID="abe7529e-dcd5-4afc-b714-05569dbcd30b"
/dev/sdb6: UUID="f34c8c7c-a020-4a14-8c97-257180240250&#8243; SEC_TYPE="ext2&#8243; TYPE="ext3&#8243;
/dev/sdb7: UUID="8fa274ca-5b22-411f-b5da-7469c1f276da" SEC_TYPE="ext2&#8243; TYPE="ext3&#8243;
/dev/sdc1: UUID="1e36f323-c4e5-4f55-ba0a-838643550bf9&#8243; TYPE="ext3&#8243; SEC_TYPE="ext2&#8243;
/dev/sdc2: UUID="83aa92e4-4df4-4aab-80f3-9bbb447e0459&#8243; TYPE="ext3&#8243; SEC_TYPE="ext2&#8243;</pre>
<p>As you can see, I've still got a few NTFS partitions as I'm slowly migrating my data from Windows after my switch to Ubuntu desktop a couple months ago.</p>
<h3>Get UUID for a particular device</h3>
<p>If you know a device name and just want to confirm the UUID for it to later use it in <strong>/etc/fstab</strong>, here's how you can do it using <strong>vol_id</strong> command:</p>
<pre>ubuntu# <strong>vol_id -u /dev/sdb1</strong>
c73a37c8-ef7f-40e4-b9de-8b2f81038441</pre>
<p>That's all I can think of so far. I know a few more things about UUID which I'll share in a separate post, but it's a start.</p>
<p>Have you got any more great ideas and tips for UUID? Let me know and I'll be sure to share them with others in the future posts.</p>
<h3>See also:</h3>
<ul>
<li><a title="Mounting ISO images in Linux" href="http://www.unixtutorial.org/2007/12/how-to-mount-an-iso-image/">How to mount an ISO image</a></li>
<li><a title="Find largest files" href="http://www.unixtutorial.org/2008/03/find-large-files-and-directories/">Find large files and directories in Unix</a></li>
<li><a href="https://help.ubuntu.com/community/UsingUUID">Using UUIDs - Ubuntu Help wiki</a></li>
<li><a title="Default block size" href="http://www.unixtutorial.org/2008/04/default-block-size-in-unix/">How to find the default block size in Unix</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/288661382" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/05/ubuntu-uuid-how-to/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/05/ubuntu-uuid-how-to/</feedburner:origLink></item>
		<item>
		<title>Using variables in Unix shell scripts</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/285336955/</link>
		<comments>http://www.unixtutorial.org/2008/05/variables-in-unix-shell/#comments</comments>
		<pubDate>Wed, 07 May 2008 12:30:58 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Basic stuff]]></category>

		<category><![CDATA[Scripts]]></category>

		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=48</guid>
		<description><![CDATA[Any Unix shell script longer than a line will most likely involve using variables. Variables are used to store temporary values to simply using them in various Unix commands of your script. The beauty of using variables is that they can be evaluated and set ones, but then reused as many times as you like [...]]]></description>
			<content:encoded><![CDATA[<p>Any Unix shell script longer than a line will most likely involve using variables. Variables are used to store temporary values to simply using them in various Unix commands of your script. The beauty of using variables is that they can be evaluated and set ones, but then reused as many times as you like without your shell interpreter having to re-evaluate them again.</p>
<h3>Defining a variable in Unix shell</h3>
<p>To specify a value for a variable, you need to decide on the variable name - can be any word or combination of English alphabet symbols and digits, and specify the value.</p>
<p>In Bourne shell (<strong>sh</strong>), Bourne Again Shell (<strong>bash</strong>) and Korn Shell (<strong>ksh</strong>), here's how you would define a new variable and assign it a value:</p>
<pre>CONFIG_FILE="/etc/myfile"</pre>
<p>In C-Shell (<strong>csh</strong>), it's done like this:</p>
<pre>setenv CONFIG_FILE "/etc/myfile"</pre>
<h3>Basic variables usage</h3>
<p>To access the value of a variable, you need to use the same variable name, but with a dollar sign in front of it, like this:</p>
<pre>$CONFIG_FILE</pre>
<p><strong>Important:</strong> to set a new value to the variable, you use just the variable name like CONFIG_FILE, but to access this value later you need to use the $CONFIG_FILE form.</p>
<p>The most basic way to use variables is to assign them constant values at the beginning of your script. This is a good way to define locations of standard files or directories your script will work with, etc:</p>
<pre>#!/bin/sh
#
CONFIG_FILE=/etc/myfile
MY_DIR=/etc
echo $CONFIG_FILE</pre>
<h3>Using output of Unix commands to set variables</h3>
<p>One of the best things about shell scripting is that it's very easy to use any Unix command to generate the output and use it to set the variable.</p>
<p>In this example, I'm running a date command and saving its output as values for my variables:</p>
<pre>ubuntu$ cat /tmp/1.sh</pre>
<pre>#!/bin/sh
#
STARTED=`date`
sleep 5
FINISHED=`date`
#
echo "Script start time: $STARTED"
echo "Script finish time: $FINISHED"</pre>
<p>If I run this simple script, I see the following:</p>
<p>ubuntu$ <strong>/tmp/1.sh</strong><br />
Script start time: Wed May  7 04:56:51 CDT 2008<br />
Script finish time: Wed May  7 04:56:56 CDT 2008<br />
The same approach can be used for practically any scenario.</p>
<p>Here's an example of using uname command to extract some useful information about our system:</p>
<pre>#!/bin/sh
#
STARTED=`date`
NODE=`uname -n`
OS=`uname -o`
CPU=`uname -p`
FINISHED=`date`
#
echo "Nodename: $NODE"
echo "OS type: $OS"
echo "Processor: $CPU"
echo "Script start time: $STARTED"
echo "Script finish time: $FINISHED"</pre>
<p>And this is how it works:</p>
<pre>ubuntu$ <strong>/tmp/1.sh</strong>
Nodename: ubuntu
OS type: GNU/Linux
Processor: unknown
Script start time: Wed May  7 05:05:31 CDT 2008
Script finish time: Wed May  7 05:05:31 CDT 2008</pre>
<p>That's it for today! Let me know what next topic about Unix shell scripting you'd like to see covered, and I'll do my best to explain it in the coming posts.</p>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/285336955" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/05/variables-in-unix-shell/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/05/variables-in-unix-shell/</feedburner:origLink></item>
		<item>
		<title>How To Show a Processes Tree in Unix</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/281426423/</link>
		<comments>http://www.unixtutorial.org/2008/05/how-to-show-a-processes-tree-in-unix/#comments</comments>
		<pubDate>Thu, 01 May 2008 12:30:37 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[RedHat]]></category>

		<category><![CDATA[Solaris]]></category>

		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=47</guid>
		<description><![CDATA[Showing your processes in a hierarchical list is very useful for confirming the relationship between every process running on your system. Today I'd like to show you how you can get tree-like processes lists using various commands.
Showing processes tree with ptree
In Solaris, there's quite a few commands which make the life of any system administrator [...]]]></description>
			<content:encoded><![CDATA[<p>Showing your processes in a hierarchical list is very useful for confirming the relationship between every process running on your system. Today I'd like to show you how you can get tree-like processes lists using various commands.</p>
<h3>Showing processes tree with ptree</h3>
<p>In Solaris, there's quite a few commands which make the life of any system administrator much easier, they're the process commands (p-commands). One of them which I particularly like is the ptree command which shows you a list of processes.</p>
<p>As you run the command, you get a hierarchical list of all the processes running on your Solaris system, along with process IDs (PIDs). To me, this is a very useful command, because it shows you how exactly each process relates to others in your system.</p>
<p>Here's a fragment of the ptree output:</p>
<pre>bash-3.00$ <strong>ptree</strong>
7     /lib/svc/bin/svc.startd
  250   /usr/lib/saf/sac -t 300
    268   /usr/lib/saf/ttymon
  260   -sh
    5026  -csh
9     /lib/svc/bin/svc.configd
107   /usr/lib/sysevent/syseventd
136   /usr/lib/picl/picld
140   /usr/lib/crypto/kcfd
159   /usr/sbin/nscd
227   /usr/sbin/rpcbind
234   /usr/lib/nfs/statd
235   /usr/sbin/keyserv
236   /usr/lib/netsvc/yp/ypserv -d
  237   rpc.nisd_resolv -F -C 8 -p 1073741824 -t udp
241   /usr/lib/nfs/lockd
247   /usr/lib/netsvc/yp/ypbind
263   /usr/lib/utmpd
286   /usr/sadm/lib/smc/bin/smcboot
  287   /usr/sadm/lib/smc/bin/smcboot
  288   /usr/sadm/lib/smc/bin/smcboot</pre>
<h3>Processes tree with pstree</h3>
<p>In most Linux distributions, you can find a pstree command, very similar to ptree.</p>
<p>That's how you may use it (-p is an option to show PIDs and -l uses long output format):</p>
<pre>ubuntu$ <strong>pstree -pl</strong>
init(1)─┬─NetworkManager(5427)
        ├─NetworkManagerD(5441)
        ├─acpid(5210)
        ├─apache2(6966)─┬─apache2(2890)
        │               ├─apache2(2893)
        │               ├─apache2(7163)
        │               ├─apache2(7165)
        │               ├─apache2(7166)
        │               ├─apache2(7167)
        │               └─apache2(7168)
        ├─atd(6369)
        ├─avahi-daemon(5658)───avahi-daemon(5659)
        ├─bonobo-activati(7816)───{bonobo-activati}(7817)
&#8230;</pre>
<h3>Showing processes tree with ps &#8211;forest</h3>
<p>ps command found in Linux has a &#8211;forest option, which shows you a tree structure of processes.</p>
<p>The best in my experience is to use it like this:</p>
<pre>ubuntu$ <strong>ps -aef &#8211;forest</strong>
UID        PID  PPID  C STIME TTY          TIME CMD
&#8230;
107       5473     1  0 10037  4600   0 Apr28 ?        00:00:02 /usr/sbin/hald
root      5538  5473  0  5511  1288   0 Apr28 ?        00:00:00  \_ hald-runner
root      5551  5538  0  6038  1284   0 Apr28 ?        00:00:01      \_ hald-addon-input: Listening on /dev/input
107       5566  5538  0  4167   992   1 Apr28 ?        00:00:00      \_ hald-addon-acpi: listening on acpid socke
root      5600  5538  0  6038  1272   1 Apr28 ?        00:00:15      \_ hald-addon-storage: polling /dev/scd0 (ev
root      5476     1  0 10272  2532   0 Apr28 ?        00:00:00 /usr/sbin/console-kit-daemon
root      5627     1  0 12728  1176   1 Apr28 ?        00:00:00 /usr/sbin/sshd
root      9151  5627  0 17536  3032   0 10:53 ?        00:00:00  \_ sshd: greys [priv]
greys     9162  9151  0 17538  1892   1 10:54 ?        00:00:00      \_ sshd: greys@pts/3
greys     9168  9162  0  5231  3820   1 10:54 pts/3    00:00:00          \_ -bash
greys     9584  9168  0  3802  1124   0 11:27 pts/3    00:00:00              \_ ps -aeF &#8211;forest</pre>
<p>This output is for demonstration purpose only, and so I've taken the first lines of the output out because they weren't serving the purpose of this example very well.</p>
<p>For thins fragment of the output you can see how you get all the vital information about each process. I really like this way of running the ps command.</p>
<p>That's it for today! Do you know any other neat way of looking at processes tree? Let me know!</p>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/281426423" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/05/how-to-show-a-processes-tree-in-unix/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/05/how-to-show-a-processes-tree-in-unix/</feedburner:origLink></item>
		<item>
		<title>Ubuntu 8.04 (Hardy Heron) released!</title>
		<link>http://feeds.feedburner.com/~r/UnixTutorial/~3/277074382/</link>
		<comments>http://www.unixtutorial.org/2008/04/ubuntu-804-hardy-heron/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 18:11:10 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[hardy heron]]></category>

		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=46</guid>
		<description><![CDATA[
For all of you who were waiting for the next release of Ubuntu, it's finally here! I've been using the beta of Ubuntu 8.04 (Hardy Heron) for the past month or so, and found it an excellent release to work with.
Ubuntu 8.04 links:

Ubuntu 8.04 TLS download
What's new in Ubuntu 8.04
Upgrading to Ubuntu 8.04

]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" src="http://www.unixtutorial.org/images/ubuntu-804-tls.jpg" alt="Ubuntu 8.04 TLS" /></p>
<p>For all of you who were waiting for the next release of <a title="Ubuntu" href="http://www.ubuntu.com/">Ubuntu</a>, it's finally here! I've been using the beta of Ubuntu 8.04 (Hardy Heron) for the past month or so, and found it an excellent release to work with.</p>
<h3>Ubuntu 8.04 links:</h3>
<ul>
<li><a title="Ubuntu download" href="http://www.ubuntu.com/getubuntu/download">Ubuntu 8.04 TLS download</a></li>
<li><a href="http://www.ubuntu.com/getubuntu/releasenotes/804overview">What's new in Ubuntu 8.04</a></li>
<li><a href="http://www.ubuntu.com/getubuntu/upgrading">Upgrading to Ubuntu 8.04</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/UnixTutorial/~4/277074382" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/04/ubuntu-804-hardy-heron/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.unixtutorial.org/2008/04/ubuntu-804-hardy-heron/</feedburner:origLink></item>
	</channel>
</rss>
