<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Unix Tutorial &#187; Unix</title>
	<atom:link href="http://www.unixtutorial.org/category/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.unixtutorial.org</link>
	<description>Learn UNIX</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:05:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Mounting NFS shares on Mac OS X</title>
		<link>http://www.unixtutorial.org/2010/03/mounting-nfs-shares-on-mac-os-x/</link>
		<comments>http://www.unixtutorial.org/2010/03/mounting-nfs-shares-on-mac-os-x/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 22:54:50 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Unix]]></category>
		<category><![CDATA[mac os]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[macos]]></category>
		<category><![CDATA[macosx]]></category>
		<category><![CDATA[nfs]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=634</guid>
		<description><![CDATA[I've recently decided to give Mac OS X a try. For the past week or so I've been spending a good few hours a day working in Snow Leopard installed on a MacBook Pro borrowed from a friend. While Mac OS is unlike any Unix-like operating system I've managed so far, there are certainly some [...]]]></description>
			<content:encoded><![CDATA[<p>I've recently decided to give <strong>Mac OS X</strong> a try. For the past week or so I've been spending a good few hours a day working in <strong>Snow Leopard</strong> installed on a MacBook Pro borrowed from a friend.</p>
<p>While <strong>Mac OS</strong> is unlike any Unix-like operating system I've managed so far, there are certainly some of similarities. I can honestly say that I'm enjoying the Mac Book Pro so far, and hope to discover most of the differences compared to my previous Unix-like desktop which is Ubuntu 9.10.</p>
<h3>Mounting NFS on MAC OS X</h3>
<p>One thing which I noticed immediately was that out of the box it was impossible to mount any NFS shares from my Ubuntu NAS server. Any attempt to mount a remote filesystem would give me an error like this:</p>
<blockquote><p>mbp:~ root# <strong>mount nasbox:/try /mnt</strong><br />
mount_nfs: /mnt: Operation not permitted</p></blockquote>
<p>This error was happening for a relatively simple NFS share on the Ubuntu box:</p>
<blockquote><p>nasbox# <strong>cat /etc/exports</strong><br />
/try            (rw)</p></blockquote>
<p>&#8230; so I started looking around and realized that the reason for this strange problem is quite simple.</p>
<h3>Mac OS X uses non-standard port for outgoing NFS connections</h3>
<p>That's right &#8211; apparently, <strong>Mac OS X </strong>uses a non-privileged port (<strong>2049</strong>) for TCP and UDP connections serving the NFS transport. What this means is that most likely attempts to mount remote filesystems will fail, because most NFS servers don't really like connections from insecure ports.</p>
<p>There are two ways to approach this problem:</p>
<ol>
<li>Fix it on the client side (probably makes more sense)</li>
<li>Fix it on the NFS server side (if you manage both systems)</li>
</ol>
<h3>Using reserved NFS port number on Mac OS X</h3>
<p>There's a mount option supported by the <strong>mount_nfs</strong> command, which allows you to force the NFS client connections to originate from a privileged port. This will magically make your attempts to mount remote filesystems successful. The option is called <strong>resvport</strong>.</p>
<p>First we double-check that default mounts still don't work:</p>
<blockquote><p>mbp:~ root# <strong>mount nasbox:/try /mnt</strong><br />
mount_nfs: /mnt: Operation not permitted</p></blockquote>
<p>&#8230; and now let's use the <strong>resvport</strong> option:</p>
<blockquote><p>mbp:~ root# <strong>mount -o resvport nasbox:/try /mnt</strong></p></blockquote>
<p>&#8230; and make sure we're actually looking at a mounted filesystem:</p>
<blockquote><p>mbp:~ root# <strong>df -h /mnt</strong><br />
Filesystem   Size   Used  Avail Capacity  Mounted on<br />
nasbox:/try    61Gi   56Gi  2.6Gi    96%    /mnt</p></blockquote>
<h3>Allowing connections from non-privileged ports on NFS server</h3>
<p>Like I said, if you manage both the Mac OS based client and the NFS server, perhaps it makes more sense to relax the default NFS server security and allow the connections from non-privileged ports.</p>
<p>Just to remind you about the validity of such a decision, the option to allow non-privileged connections is called insecure.</p>
<p>Here's how you use it:</p>
<blockquote><p>nasbox# <strong>cat /etc/exports</strong><br />
/try            (rw,<strong>insecure</strong>)</p></blockquote>
<p>After making this change to the <strong>/etc/exports</strong> file, you'll have to restart your NFS server. On my Ubuntu NAS box, it's done like this:</p>
<blockquote><p>nasbox# <strong>/etc/init.d/nfs-kernel-server restart</strong><br />
* Stopping NFS kernel daemon<br />
&#8230;done.<br />
* Unexporting directories for NFS kernel daemon&#8230;<br />
&#8230;done.<br />
* Exporting directories for NFS kernel daemon&#8230;<br />
&#8230;done.<br />
* Starting NFS kernel daemon<br />
&#8230;done.</p></blockquote>
<p>We know are ready to attempt the default mount of the same filesystem on the <strong>Mac OS X</strong> client:</p>
<blockquote><p>mbp:~ root# <strong>mount nasbox:/try /mnt</strong></p></blockquote>
<p>That's it! I won't promise any <strong>Mac OS </strong>posts just yet, but if there is enough interest &#8211; I'd love to do the research and to post all the discoveries on the <strong><a title="Unix Tutorial" href="http://www.unixtutorial.org">Unix Tutorial</a></strong> pages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2010/03/mounting-nfs-shares-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Unix Tutorial membership &#8211; registration open for a week</title>
		<link>http://www.unixtutorial.org/2009/08/unix-tutorial-membership-registration-open-for-a-week/</link>
		<comments>http://www.unixtutorial.org/2009/08/unix-tutorial-membership-registration-open-for-a-week/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 15:23:00 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=598</guid>
		<description><![CDATA[Hi everyone! I'm back from my holidays, and it's time to open the Unix Tutorial members area for new registrations once again! If you're still looking for a reason to become a member &#8211; please read the Becoming a Unix Tutorial member post. This time around, new users will receive a life-time access to just [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone!</p>
<p>I'm back from my holidays, and it's time to open the <a href="http://www.unixtutorial.org/members"><strong>Unix Tutorial members</strong></a> area for <a href="http://www.unixtutorial.org/ut/signup.php">new registrations</a> once again!</p>
<p>If you're still looking for a reason to become a member &#8211; please read the <a href="http://www.unixtutorial.org/2009/03/become-a-lifetime-unix-tutorial-member/">Becoming a Unix Tutorial member</a> post.</p>
<p>This time around, new users will receive a life-time access to just one course, the <strong>Unix foundations</strong> one. This is going to be a constantly updated and expanded course so I'm sure you'll benefit from revisiting it now and then. New courses added at a later stage will never be available for free: I plan to make them very specialized and will charge for access &#8211; more on this later.</p>
<p>Wait no more &#8211; here's your chance to join: <a href="http://www.unixtutorial.org/ut/signup.php">Unix Tutorial members sign-up</a>. I don't have nearly as much time to further develop the course, but please take time to leave a comment to this post or update a thread on the Unix Tutorial members discussion board about what exactly you're interested in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/08/unix-tutorial-membership-registration-open-for-a-week/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Fixed calculations in Unix scripts</title>
		<link>http://www.unixtutorial.org/2009/06/fixed-point-calculations-in-unix-scripts/</link>
		<comments>http://www.unixtutorial.org/2009/06/fixed-point-calculations-in-unix-scripts/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 12:11:06 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Basic stuff]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[bc command]]></category>
		<category><![CDATA[calculations]]></category>
		<category><![CDATA[unix scripts]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=583</guid>
		<description><![CDATA[Although I've already shown you how to sum numbers up in bash, I only covered the bash way of doing it. I really like scripting with bash, but when it comes to calculations, there's quite a few important features missing from bash, and fixed point (thanks for the correction, Azrael Tod!) calculations is one of [...]]]></description>
			<content:encoded><![CDATA[<p>Although I've already <a title="arithmetic operations in Unix" href="http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/">shown you how to sum numbers up in bash</a>, I only covered the bash way of doing it. I really like scripting with bash, but when it comes to calculations, there's quite a few important features missing from bash, and fixed point (thanks for the correction, <a href="http://g33ky.de/">Azrael Tod</a>!) calculations is one of them. Fortunately, <strong>bc command</strong> comes as a standard in most Unix distros, and can be used for quite complex calculations.</p>
<h3>Basic calculations with bc</h3>
<p><strong>bc </strong>is a very simple command. It takes standard input as an expression and then evaluates this, performing all the necessary calculations and showing you the result. Thus, to quickly sum numbers up or get a result of some other calculation, simply echo the expression and then pipe it out to the <strong>bc command</strong>:</p>
<pre>ubuntu$ <strong>echo "1+2" | bc</strong>
3</pre>
<p>Now, in scripts your calculations with <strong>bc </strong>are done quite similarly to what we did in <strong>bash</strong>. Here's an example:</p>
<pre>ubuntu$ <strong>NUMBER1=1</strong>
ubuntu$ <strong>NUMBER2=2</strong>
ubuntu$ <strong>SUM=$(echo "$NUMBER1+$NUMBER2"| bc)</strong>
ubuntu$ <strong>echo $SUM</strong>
3</pre>
<p>I told you these calculations would be basic, right? Now onto the more interesting stuff!</p>
<h3>Fixed point calculations with bc</h3>
<p>Most people learn about bash math limitations when they attempt to do a simple calculation but can't get the current answer with fixed point values. By default, all the operations happen with integers, and that's what you would get:</p>
<pre>ubuntu$ <strong>echo "1/2" | bc</strong>
0</pre>
<p>Now, if you expect 0.5 to be the result of dividing 1 by 2, you need to explain it to <strong>bc</strong>, because by default it doesn't show you any fractional part of the number.</p>
<p>The way you do this is quite simple: all you have to do is specify the number of digits you'd like to see  after the radix point of your result. For example, if I set this number to 5, I'll get <strong>bc </strong>to output the result of my calculation with 5 digits after the radix point. The special keyword to convey this intention to the <strong>bc</strong> command is called <strong>scale</strong>. Just specify the scale value and separate it from the rest of your expression by the semicolon sign:</p>
<pre>ubuntu$ <strong>echo "scale=5; 1/2" | bc</strong>
.50000</pre>
<p>Here's another example:</p>
<pre>ubuntu$ <strong>echo "scale=5; 0.16*10.79" | bc</strong>
1.7264</pre>
<p>Hope this answers your question! <strong>bc command</strong> is very powerful, so I'll definitely have to revisit it again in the future. For now though, enjoy the fixed point calculations and be sure to ask questions if you think I can help!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="summing numbers up" href="http://www.unixtutorial.org/2009/02/unix-scripts-how-to-sum-numbers-up/">Summing numbers up in Unix scripts</a></strong></li>
<li><strong><a title="basic unix math" href="http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/">Basic math in Unix scripting</a></strong></li>
<li><strong><a title="unix scripts calculations" href="http://www.unixtutorial.org/2008/09/math-expressions-in-unix-scripts/">Another way of doing calculations in scripts</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/06/fixed-point-calculations-in-unix-scripts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Command Aliases in Unix shells</title>
		<link>http://www.unixtutorial.org/2009/06/command-aliases-in-unix-shells/</link>
		<comments>http://www.unixtutorial.org/2009/06/command-aliases-in-unix-shells/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 15:24:46 +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=572</guid>
		<description><![CDATA[One of the really useful features almost every Unix shell has is support for command aliases &#8211; a way to run a command or a series of Unix commands using a shorter name you get associated with such commands. An example of a command alias in Unix shell Here's one of the most useful aliases [...]]]></description>
			<content:encoded><![CDATA[<p>One of the really useful features almost every Unix shell has is support for <strong>command aliases</strong> &#8211; a way to run a command or a series of Unix commands using a shorter name you get associated with such commands.</p>
<h3>An example of a command alias in Unix shell</h3>
<p>Here's one of the most useful aliases I have for Solaris systems:</p>
<pre>solaris$ <strong>alias ls='/usr/local/gnu/bin/ls --color -F'</strong></pre>
<p>What is allows me to do is to simply type "<strong>ls</strong>" instead of the really long command line it refers to: <strong>/usr/local/gnu/bin/ls &#8211;color -F</strong>.</p>
<p>You see, the <strong><a href="http://www.unixtutorial.org/commands/ls">ls command</a></strong> which is shipped with Solaris, doesn't have many options of the more up-to-date GNU ls command, and working with many Linux systems I quite like some of them like color highlighting of different directory objects &#8211; files, directories and executables.</p>
<p>Try typing the longer command a few times in a row, and compare it to the "ls" to get the idea of how much of a productivity gain one command alias can be!</p>
<h3>Why you should use command aliases</h3>
<p>Like many other things in Unix, aliases are a way to become more productive. The general rule of thumb is this: if you have to run some command something more than once every day &#8211; consider creating an alias for it. These are just a few cases where it makes sense to employ them:</p>
<ul>
<li>if you repeatedly check whether some files exist or get updated</li>
<li>if you're monitoring a certain aspect of your OS and you get the values using grep command</li>
<li>if you're connecting to the same hosts using rsh or ssh</li>
</ul>
<p>All of these and many more examples are greatly simplified if you alias them to some shorter commands.</p>
<h3>Creating new aliases in bash</h3>
<p>Setting up a new alias is quite easy, the syntax for <strong>alias command</strong> is very straightforward. Let's say I want to automate the confirmation of swap usage based on a <strong>free command</strong> in Linux:</p>
<pre>redhat$ <strong>free</strong>
total       used       free     shared    buffers     cached
Mem:       2075156     945712    1129444          0     177292     503416
-/+ buffers/cache:     265004    1810152
Swap:      2040244          0    2040244</pre>
<p>The result I'm after is this command:</p>
<pre>redhat$ <strong>free | grep Swap</strong>
Swap:      2040244          0    2040244</pre>
<p>And here's how I can create an alias called "swp" which refers to this series of commands:</p>
<pre>redhat$ <strong>alias swp='free | grep Swap'</strong></pre>
<p>Once you execute this command, you can start using swp as a Unix command:</p>
<pre>redhat$ <strong>swp</strong>
Swap:      2040244          0    2040244</pre>
<p><strong>Important:</strong> such a creation of new aliases is going to be only active for your current Unix shell and sub-shells you may spawn. To make your alias permanent, you'll have to update one of your initialization scripts like. For Linux and bash, you should add the same alias command to your <strong>.bashrc</strong> file.</p>
<h3>Removing aliases in Unix</h3>
<p>In very much the same way, you can use the unalias command to get rid of a certain alias. The nature of this command is such that you'll most likely use it when creating and debugging new aliases. It's unlikely that you'll need to use it in your initialization scripts.</p>
<p>Following the example above, here's how to get rid of the swp alias and verify that it's gone:</p>
<pre>redhat$ <strong>unalias swp</strong>
redhat$ <strong>swp</strong>
bash: swp: command not found</pre>
<h3>How to list your current aliases</h3>
<p>If you run the alias command without any parameters, you'll be shown a full list of aliases currently configured for your user account, here's an example from one of my systems:</p>
<pre>l.      ls -d .* --color=tty
ll      ls -l --color=tty
ls      ls --color=tty
vi      vim</pre>
<p>That's it for today! Stay tuned for a follow-up post which will share some of the examples for command aliases in Unix. If you have some &#8211; please leave a comment so that I can share it with others!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="ls unix command" href="http://www.unixtutorial.org/commands/ls">Unix commands: ls</a></strong></li>
<li><strong><a title="Listing directories" href="http://www.unixtutorial.org/2008/04/how-to-list-directories-in-a-directory-in-unix/">List directories in a given directory</a></strong></li>
<li><strong><a title="what symlink points to" href="http://www.unixtutorial.org/2008/03/how-to-find-what-symlink-points-to/">How to find what symlink points to</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/06/command-aliases-in-unix-shells/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Unix scripts: how to sum numbers up</title>
		<link>http://www.unixtutorial.org/2009/02/unix-scripts-how-to-sum-numbers-up/</link>
		<comments>http://www.unixtutorial.org/2009/02/unix-scripts-how-to-sum-numbers-up/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 15:33:48 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Basic stuff]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=465</guid>
		<description><![CDATA[If you're ever thought of summing up more than two numbers in shell script, perhaps this basic post will be a good start for your Unix scripting experiments. Basic construction for summing up in shell scripts In my Basic arithmetic operations in Unix shell post last year, I've shown you how to sum up two [...]]]></description>
			<content:encoded><![CDATA[<p>If you're ever thought of summing up more than two numbers in shell script, perhaps this basic post will be a good start for your Unix scripting experiments.</p>
<h3>Basic construction for summing up in shell scripts</h3>
<p>In my <a title="arithmetic operatics in Unix" href="http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/">Basic arithmetic operations in Unix shell</a> post last year, I've shown you how to sum up two numbers:<strong></strong></p>
<pre>SUM=$(($NUMBER1 + $NUMBER2)</pre>
<p>using the same approach, it's possible to calculate sums of as many numbers as you like, if you use one of the loops available in your shell.</p>
<p>Before we get started, let's create a simple file with numbers we'll work with:</p>
<pre>ubuntu$ <strong>for i in 1 2 3 4 5 6 7 8 9; do echo $i &gt;&gt; /tmp/nums; done;</strong>
ubuntu$ <strong>cat /tmp/nums</strong>
1
2
3
4
5
6
7
8
9</pre>
<h3>Using a while loop to sum numbers up in Unix</h3>
<p>Here's an example of how you can use a <strong>while loop</strong> in Unix shell for summing numbers up. Save it as a<strong> /tmp/sum.sh</strong> script, and don't forget to do <strong>chmod a+x /tmp/sum.sh</strong> so that you can run it!</p>
<pre>#!/bin/sh
#
SUM=0
#
while read NUM
do
        echo "SUM: $SUM";
        SUM=$(($SUM + $NUM));
        echo "+ $NUM: $SUM";
done &lt; /tmp/nums</pre>
<p>Here's how the output will look when you run it:</p>
<pre>ubuntu$ <strong>/tmp/sum.sh
</strong>SUM: 0
+ 1: 1
SUM: 1
+ 2: 3
SUM: 3
+ 3: 6
SUM: 6
+ 4: 10
SUM: 10
+ 5: 15
SUM: 15
+ 6: 21
SUM: 21
+ 7: 28
SUM: 28
+ 8: 36
SUM: 36
+ 9: 45</pre>
<p>Of course, your data will most probably will be in a less readable form, so you'll have to do a bit of parsing before you get to sum the numbers up, but the loop will be organized in the same way.</p>
<p>That's it for today, enjoy and feel free to ask questions!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="arithmetics in scripts" href="http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/">Basic arithmetics in scripts</a></strong></li>
<li><strong><a title="unix shell variables" href="http://www.unixtutorial.org/2008/05/variables-in-unix-shell/">Variables in Unix shells</a></strong></li>
<li><strong><a title="unix glossary" href="http://www.unixtutorial.org/glossary">Unix glossary</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/02/unix-scripts-how-to-sum-numbers-up/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Converting date and time to Unix epoch in Perl</title>
		<link>http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/</link>
		<comments>http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 23:08:26 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[epoch]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=452</guid>
		<description><![CDATA[This post will show you how to convert standard dates into Unix epoch times in Perl.]]></description>
			<content:encoded><![CDATA[<p>Today I was working on a script, and one of the subroutines needed simple seconds-based arithmetics with time. As you probably remember from  my <a title="time and date in Unix" href="http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/">date and time in Unix scripts </a>article, the easiest way to approach this task is to deal with the raw representation of date and time in Unix &#8211; the Unix epoch times. This post will show you how to convert standard dates into Unix epoch times in Perl.</p>
<h3>Why would you want to convert timestamps to Unix epoch time?</h3>
<p><a title="Unix epoch" href="http://www.unixtutorial.org/glossary/#unix_epoch">Unix epoch time</a> is an ever-growing counter which increments every second and shows the number of seconds elapsed since 00:00:00 UTC on January 1, 1970. Such numbers are pretty useless in their raw form for the tasks of confirming current time and date, but they're perfect for measuring between two points in time.</p>
<h3>Current epoch time in Perl</h3>
<p>First things first. If you want to confirm the current Unix epoch time in Perl, here's how you do it: just use the <strong>time()</strong> subroutine.</p>
<pre>#!/bin/perl
print "Current (epoch) time: " . time() . "\n";</pre>
<h3>Converting regular date and time into epoch time</h3>
<p>It's equally easy to convert time into Unix epoch representation, for this you should use <strong>timegm() </strong>or <strong>localtime()</strong> subroutine from the <strong>Time::Local</strong> module.</p>
<p><strong>timegm()</strong> has the following syntax:</p>
<pre>$current = timegm($sec,$min,$hours,$day,$month,$year);</pre>
<p><strong>Important:</strong> While most of the parameters are self-explanatory, it is probably worth reminding that $year should be specified as a number like 109 and not 2009.<br />
Also, the $month is not the number of a month, but a number of months since January. So, 0 will mean January itself, 1 will mean February, and 11 means December.</p>
<p>Here's a fully working example, showing the countdown in seconds until midnight on the New Year's Eve 2009:</p>
<pre>#!/usr/bin/perl
#
use Time::Local;
#
$sec=59;
$min=59;
$hours=23;
$day=31;
$month=11;
$year=109;
#
$current = time();
print "Current (epoch) time: $current\n";
#
$newyear = timegm($sec,$min,$hours,$day,$month,$year);
print "New Year's Eve 2009: $newyear\n";
#
print "Only " .($newyear-$current) . " seconds to go\n";</pre>
<p>When you save this script as epoch.pl and make the file executable, here's what you'll get if you run it:</p>
<pre># <strong>./epoch.pl</strong>
Current (epoch) time: 1234911622
New Year's Eve 2009: 1262303999
Only 27392377 seconds to go</pre>
<p>That's it for today! Hope this little post will save you time when tacking next scripting challenge with Perl.</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="Time and date" href="http://www.unixtutorial.org/2008/06/unix-scripting-time-and-date/">Time and date in Unix scripts</a></strong></li>
<li><strong><a title="gnu date calculations" href="http://www.unixtutorial.org/2008/09/easy-date-calculations-in-unix-scripts-with-gnu-date/">Date calculations with GNU date command</a></strong></li>
<li><strong><a title="unix glossary" href="http://www.unixtutorial.org/glossary">Glossary of Unix terms</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/02/converting-date-and-time-to-unix-epoch-in-perl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Change Ownership of Files and Directories in Unix</title>
		<link>http://www.unixtutorial.org/2009/02/how-to-change-ownership-of-files-and-directories-in-unix/</link>
		<comments>http://www.unixtutorial.org/2009/02/how-to-change-ownership-of-files-and-directories-in-unix/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 10:21:13 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Basic stuff]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[chown]]></category>
		<category><![CDATA[file ownership]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=439</guid>
		<description><![CDATA[I've just been asked a question about changing the ownership of files from one Unix user to another, and thought it probably makes sense to have a quick post on it. File ownership in Unix Just to give you a quick reminder, I'd like to confirm that every single file in Unix belongs to some [...]]]></description>
			<content:encoded><![CDATA[<p>I've just been asked a question about changing the ownership of files from one Unix user to another, and thought it probably makes sense to have a quick post on it.</p>
<h3>File ownership in Unix</h3>
<p>Just to give you a quick reminder, I'd like to confirm that every single file in Unix belongs to some user and some group. There simply isn't a way to create a file without assigning ownership. I've briefly touched the topic of <a title="find the owner of a file" href="http://www.unixtutorial.org/2008/03/how-to-find-the-owner-of-a-file-in-unix/">confirming file ownership in Unix</a> before, so today I will simply build on that and show you how to change ownership of files.</p>
<p>Here's a setup for today: I have created a temporary directory with a few files and made myself the owner of all the files:</p>
<pre>ubuntu$ <strong>ls -al /home/greys/example/</strong>
total 12
drwxr-xr-x  3 greys admin 4096 Feb  9 03:55 .
drwxr-xr-x 13 greys greys 4096 Feb  9 03:54 ..
drwxr-xr-x  2 greys admin 4096 Feb  9 03:55 dir1
-rw-r--r--  1 greys admin    0 Feb  9 03:54 file1
-rw-r--r--  1 greys admin    0 Feb  9 03:55 file2</pre>
<p>As you can see from this listing, the owner (third field in each line) is my username &#8211; <strong>greys</strong>. The next field is a Unix group of each file's owner &#8211; <strong>admin </strong>in my example.</p>
<h3>Changing owner of a file in Unix</h3>
<p>Changing file ownership means only updating the association between a Unix user and a file, and nothing else. When you're changing the owner of a file, no data contained in a file is changed.</p>
<p>To change the owner of a file, you need to use the <strong>chown command</strong> (easy enough to remember: CHange OWNer &#8211; <strong>chown</strong>), with the following syntax:</p>
<pre>ubuntu$ <strong>chown nobody file1</strong></pre>
<p>In this command, <strong>nobody </strong>is the username of the new owner for a list of files. In my example, the only file we'd like to change ownership for is <strong>file1</strong>.</p>
<p>It is important to realize that you can only change file ownership as a super-user (root). Any regular Unix user cannot change the ownership of any file, and I'd like to explain why.</p>
<p>Indeed, some people are surprised: if I'm the owner of a given file, why can't I change the ownership for it? That's because transferring the ownership will mean some other Unix user will become the owner of the file(s) in question. So changing ownership is like making a decision not only for yourself, but for the new owner of the files.This is only something a super-user &#8211; special administrative account in Unix &#8211; can do.</p>
<p>The same logic applies to other people not being able to become owners of your files, even if they're willing to assume the new responsibilities of owning files. They cannot revoke your ownership, because each Unix user is only allowed to make decisions and take actions on his/her own behalf.</p>
<p>That's why you will probably see an error like this if you attempt to change ownership of a file as your own regular Unix user:</p>
<pre>ubuntu$ <strong>id</strong>
uid=1000(greys) gid=113(admin) groups=33(www-data),113(admin)
ubuntu$ <strong>chown nobody file1</strong>
chown: changing ownership of `file1': Operation not permitted</pre>
<p>But if we become root:</p>
<pre>ubuntu$ <strong>sudo -i</strong>
[sudo] password for greys:
ubuntu#</pre>
<p>&#8230; we'll have no problem changing owners for any files:</p>
<pre>ubuntu# <strong>cd /home/greys/example</strong>
ubuntu# <strong>chown nobody file1</strong>
ubuntu# <strong>ls -l file1</strong>
-rw-r--r-- 1 nobody admin 0 Feb  9 03:54 file1</pre>
<h3>Changing owner for multiple files</h3>
<p>If you're going to change owner of a few files, this can easily be done using either a full list of files or a mask.</p>
<p>First, here's an example of updating ownership for a specified list of files (and as you can see, directories as well):</p>
<pre>ubuntu# <strong>chown nobody file2 dir1</strong>
ubuntu# <strong>ls -al</strong>
total 12
drwxr-xr-x  3 greys  admin 4096 Feb  9 03:55 .
drwxr-xr-x 13 greys  greys 4096 Feb  9 03:54 ..
drwxr-xr-x  2 nobody admin 4096 Feb  9 03:55 dir1
-rw-r--r--  1 nobody admin    0 Feb  9 03:54 file1
-rw-r--r--  1 nobody admin    0 Feb  9 03:55 file2</pre>
<p><strong>IMPORTANT:</strong> here's one thing which is often forgotten: when you're changing an owner of a directory, this DOES NOT automatically change owner of all the files which already exist in this directory. So, if we check the file3 in dir1 after the example above, we can see that even though dir1 now belongs to user nobody, file3 in it still belongs to me:</p>
<pre>ubuntu# <strong>ls -l dir1/file3</strong>
-rw-r--r-- 1 greys admin 0 Feb  9 03:55 dir1/file3</pre>
<p>If your intention is to change ownership of all the files and directories of a certain location in your filesystem, you need to use a <strong>-R</strong> option of the <strong>chown command</strong>, which means recursive ownership change:</p>
<pre>ubuntu# <strong>chown -R nobody dir1</strong>
ubuntu# <strong>ls -l dir1/file3</strong>
-rw-r--r-- 1 nobody admin 0 Feb  9 03:55 dir1/file3</pre>
<p>And just to further demonstrate this, I'm going to change owner of all the files and directories in <strong>/home/greys/example</strong> directory back to my own username, <strong>greys</strong>:</p>
<pre>ubuntu# chown -R greys /home/greys/example/
ubuntu# ls -l /home/greys/example/
total 4
drwxr-xr-x 2 greys admin 4096 Feb  9 03:55 dir1
-rw-r--r-- 1 greys admin    0 Feb  9 03:54 file1
-rw-r--r-- 1 greys admin    0 Feb  9 03:55 file2</pre>
<p>That's it for today, good luck with changing file owners on your Unix system!</p>
<h3>Recommended books:</h3>
<div style="border-style:none; overflow: hidden; clear: both;">
<div style="border-style:none; clear: none; float: left;">
<a target="_blank" href="http://www.amazon.com/gp/product/0470929987/ref=as_li_ss_tl?ie=UTF8&#038;tag=unixtutorial-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0470929987"><img title="Linux Bible 2011 - the latest installment of a great way to get started with Linux!" src="/amazon/linux-bible-2011.jpeg"></a>
</div>
<div style="border-style:none; clear: none; float: right;">
<a target="_blank" href="http://www.amazon.com/gp/product/0131480057/ref=as_li_ss_tl?ie=UTF8&#038;tag=unixtutorial-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0131480057"><img title="A great comprehensive manual on most of the things you'll ever need from your Unix server" src="/amazon/unix-and-linux-system-administraiton-handbook.jpeg"></a></p>
</div>
</div>
<h3>See also:</h3>
<ul>
<li><strong><a title="Owner of a file in Unix" href="http://www.unixtutorial.org/2008/03/how-to-find-the-owner-of-a-file-in-unix/">Finding the owner of a file in Unix</a></strong></li>
<li><strong><a title="find files of a certain user" href="http://www.unixtutorial.org/2008/06/find-files-which-belong-to-a-user-or-unix-group/">Find files which belong to a given Unix user</a></strong></li>
<li><strong><a title="user id instead of username" href="http://www.unixtutorial.org/2009/01/what-to-do-if-numeric-id-is-shown-instead-of-unix-username/">What to do when numeric user id is shown instead of username</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/02/how-to-change-ownership-of-files-and-directories-in-unix/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Perl Scripting: Check If a File Exists</title>
		<link>http://www.unixtutorial.org/2009/01/perl-scripting-check-if-a-file-exists/</link>
		<comments>http://www.unixtutorial.org/2009/01/perl-scripting-check-if-a-file-exists/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 13:22:55 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=428</guid>
		<description><![CDATA[It's not often that I write about Perl Scripting on Unix Tutorial, but that's just because I don't script in Perl this much on a regular basis. Today, however, I'd like to share one of the building blocks &#8211; a really basic piece of functionality which you'll find really useful in almost any Perl script. [...]]]></description>
			<content:encoded><![CDATA[<p>It's not often that I write about <a title="perl scripting" href="http://www.unixtutorial.org/category/unix/perl/">Perl Scripting</a> on <strong><a title="UNIX tutorials" href="http://www.unixtutorial.org">Unix Tutorial</a></strong>, but that's just because I don't script in Perl this much on a regular basis. Today, however, I'd like to share one of the building blocks &#8211; a really basic piece of functionality which you'll find really useful in almost any Perl script.</p>
<h3>How to check if a file exists in Perl</h3>
<p>First of all, we need to create one file for our experiments:</p>
<p>ubuntu$ <strong>touch /tmp/file.try</strong></p>
<p>Now, create a new file with our script: <strong>/tmp/file-try.pl</strong> (don't forget to<strong> chmod a+x /tmp/file-try.p</strong>l afterwards to make it executable):</p>
<pre>#!/usr/bin/perl
#
$FILE1="/tmp/file.try";
$FILE2="/tmp/file2.try";
#
if (-e $FILE1) {
        print "File $FILE1 exists!\n";} else {
        print "File $FILE1 is not found!\n";
}
if (-e $FILE2) {
        print "File $FILE2 exists!\n";
} else {
        print "File $FILE2 is not found!\n";
}</pre>
<p>Now, as you can see from the example, the actual condition checking for the existence of a file is this:</p>
<pre>if (-e $FILE)</pre>
<p>where <strong>$FILE</strong> is the string containing the filename.</p>
<p>In my example, there's two filenames (<strong>$FILE1</strong> and <strong>$FILE2</strong>) and two conditions, so that you can see for yourself how one of them confirms the existence of a file and another one proves that there's no such file found.</p>
<p>To see the script work, simply run it without any parameters:</p>
<pre>ubuntu$ <strong>/tmp/file-try.pl</strong>
File /tmp/file.try exists!
File /tmp/file2.try is not found!</pre>
<p>That's all for today! Enjoy your Perl scripting!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="file permissions unix perl" href="http://www.unixtutorial.org/2008/02/file-type-and-permissions-in-perl/">How to find out file permissions in Perl</a></strong></li>
<li><strong><a title="walking directory in perl" href="http://www.unixtutorial.org/2007/11/perl-searching-through-directory-trees/">Walking a directory tree in Perl</a></strong></li>
<li><a title="unix glossary" href="http://www.unixtutorial.org/glossary"><strong></strong></a><strong><a href="http://www.unixtutorial.org/glossary">Unix glossary</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/01/perl-scripting-check-if-a-file-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What to do if numeric id is shown instead of Unix username</title>
		<link>http://www.unixtutorial.org/2009/01/what-to-do-if-numeric-id-is-shown-instead-of-unix-username/</link>
		<comments>http://www.unixtutorial.org/2009/01/what-to-do-if-numeric-id-is-shown-instead-of-unix-username/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 10:50:15 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Advanced topics]]></category>
		<category><![CDATA[Questions & Answers]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=336</guid>
		<description><![CDATA[As you know, every file in your Unix OS belongs to some user and some group. It is very easy to confirm the ownership of any file because user id and group id which own the file are always linked to the file. However, sometimes you can't tell which user owns the file, and today [...]]]></description>
			<content:encoded><![CDATA[<p>As you know, every file in your Unix OS belongs to some user and some group. It is very easy to confirm the ownership of any file because user id and group id which own the file are always linked to the file. However, sometimes you can't tell which user owns the file, and today I'm going to explain why. It's a rather lengthy post and a complicated matter, so please leave questions or comments to help me polish this article off.</p>
<h3>Files and directories ownership in Unix</h3>
<p>If you look at any file using ls command, you will see an output like the one shown below &#8211; it reveals file access permissions, user and group id of the owner, the modification timestamp and the file name itself:</p>
<pre>ubuntu$ <strong>ls -l /tmp/myfile</strong>
-rw-r--r-- 1 greys admin 0 Jan  6 03:51 /tmp/myfile</pre>
<p>In this example, the /tmp/myfile file belongs to me, hence the username is greys. It also belongs to my default (primary) Unix group &#8211; admin.</p>
<p>Similarly, ownership of any file or any directory can be confirmed for every object in available filesystems. Here's just a few more examples, these are the standard system files belonging to root:</p>
<pre>ubuntu$ <strong>ls -ald /etc /etc/passwd</strong>
drwxr-xr-x 91 root root 4096 Jan  6 03:51 /etc
-rw-r--r--  1 root root 1481 Jan  6 03:51 /etc/passwd</pre>
<h3>Why numeric IDs are sometimes shown instead of username or groupname</h3>
<p>Sometimes though, you will look at a file and instead of the username you will see a numeric ID:</p>
<pre>ubuntu# <strong>ls -al /tmp/file</strong>
-rw-r--r-- 1 1006 root 0 Jan  6 03:51 /tmp/file</pre>
<p>The reason numberic ID (<strong>1006 </strong>in the example above) is shown instead of a username is because your system doesn't recognize this ID &#8211; it can't be associated to any username known to your Unix OS.</p>
<p>There are a few possible scenarios for this to happen, but most likely the user has been removed since the file was created. Naturally, deleting any User doesn't automatically mean removing every single file belonging to such a user, that's why the files stay but can no longer be associated with the existing user. All they have to show is the user ID which once owned the file.</p>
<h3>How to find the missing username using user id</h3>
<p>Unfortunately, there are no easy ways to recover the username (or any other user-specific information) based on a misterious user ID some of your files might have. There are a few things can try though.</p>
<ol>
<li><strong> Try other Unix systems in your environment </strong>
<p>It can be the case that Unix account was a local one automatically created by your system administrators. There's still a chance the same uid exists on other systems. Log into a few of them and verify if they have a user with the same user id (read this post for more information: <a title="ways to find user id" href="http://www.unixtutorial.org/2008/01/how-to-find-uid-in-unix/">How to Find Out user id</a>):</p>
<pre>ubuntu$ <strong>getent passwd 1006</strong>
newowner:x:1006:1006::/home/newowner:/bin/sh</pre>
<p>The same tip applies in case of more mature environments where Unix systems don't have local users, but instead rely on NIS or LDAP for accessing user accounts information.</p>
<p>If your system for whatever reason can't access the centralized storage for users, you will experience the same symptoms &#8211; most of files belonging to users will appear to have numeric IDs instead of usernames. Most likely though, you'll have more important problems in this case &#8211; like not being able to log in as anything else but root (which is an administrative account always created locally on each system).</li>
<li><strong>Look at home directories and their owners
<p></strong>When <a title="creating user accounts in unix" href="http://www.unixtutorial.org/2008/12/how-to-create-user-accounts-in-unix/">a new user is added to Unix system</a>, it usually gets a home directory assigned to it. Creating a home directory is not a default behaviour at times, but it's a good practice and so there's a very high probability that the user you're looking for had a home directory. Removing home directories isn't usually done at the same time when a user is removed, so there's also a good chance that even though the user isn't found anymore, the home directory is still there.</p>
<p>What you should be looking for is a home directory which belongs to the same user id which some of the unidentified files of yours belong to.</p>
<p>Simply do an <a href="http://www.unixtutorial.org/commands/ls/">ls command</a> under /home directory and see if any of the directories there appear to have numeric IDs instead of usernames:</p>
<pre>ubuntu# <strong>ls -ald /home/*</strong>
drwxr-xr-x  2 ftp   nogroup  4096 May 22  2007 /home/ftp
drwxr-xr-x 11 greys greys    4096 Dec 13 19:56 /home/greys
drwxr-xr-x  2 1006  admin    4096 Jan  6 04:23 /home/mike</pre>
<p>As you can see, sometimes you might get lucky &#8211; the directory is there, and since most home directories usually have the same name as the username which owns them, you can deduct that the username of the user id <strong>1006 </strong>was "<strong>mike</strong>". You can now recreate Mike's account and it will be immediately reflected for all the files owned by user id <strong>1006</strong>:</p>
<pre>ubuntu# <strong>ls -ald /home/mike</strong>
drwxr-xr-x 2 1006 admin 4096 Jan  6 04:23 /home/mike
root@simplyunix:~# useradd -u 1006 mike
root@simplyunix:~# ls -ald /home/mike /tmp/file
drwxr-xr-x 2 mike admin 4096 Jan  6 04:23 /home/mike
-rw-r--r-- 1 mike root 0 Jan  6 03:51 /tmp/file</pre>
</li>
<li><strong>Look at other users known to your system</strong>Sometimes users are created in batches, and you can guess who the user was by looking which users were created before and after. All you have to do is to use the same <strong>getent passwd </strong>approach for user ids which are smaller or larger than the one you want to identify.
<p>Another way to user other users' information to your advantage is to verify which groups they belong to and to then query the groups to see if they have any members not currently known to your system. This will only work for NIS/LDAP groups, not local ones. What could happen is that even though a user was removed, the username is still listed in a few NIS groups.</li>
</ol>
<p>That's it for today. Hope this post helps you in your investigations, and stay tuned for more!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="new users in Unix" href="http://www.unixtutorial.org/2008/12/how-to-create-user-accounts-in-unix/">Adding users in Unix</a></strong></li>
<li><strong><a title="list unix groups a user belongs to" href="http://www.unixtutorial.org/2008/07/list-groups-user-belongs-to-in-unix/">How to list groups a Unix user belongs to</a><br />
</strong></li>
<li><strong><a title="find user id" href="http://www.unixtutorial.org/2008/01/how-to-find-uid-in-unix/">How to find out user ID</a></strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2009/01/what-to-do-if-numeric-id-is-shown-instead-of-unix-username/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Tracking the Progress of  Rsync Transfers</title>
		<link>http://www.unixtutorial.org/2008/10/tracking-the-progress-of-rsync-transfers/</link>
		<comments>http://www.unixtutorial.org/2008/10/tracking-the-progress-of-rsync-transfers/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 17:09:57 +0000</pubDate>
		<dc:creator>Gleb Reys</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[rsync]]></category>

		<guid isPermaLink="false">http://www.unixtutorial.org/?p=121</guid>
		<description><![CDATA[In my first introductory rsync post, How To Synchronize Directories with Rsync, I've shown you the most basic approach to syncing two directories up. Today, I'd like to show you another useful thing you can do with rsync. Just to remind you all, rsync is a remote synchronization tool. This means that its primary use [...]]]></description>
			<content:encoded><![CDATA[<p>In my first introductory <strong>rsync </strong>post, <strong><a title="synchronize directories with rsync" href="http://www.unixtutorial.org/2008/09/how-to-synchronize-directories-with-rsync/">How To Synchronize Directories with Rsync</a></strong>, I've shown you the most basic approach to syncing two directories up. Today, I'd like to show you another useful thing you can do with <strong>rsync</strong>.</p>
<p>Just to remind you all,<strong> rsync </strong>is a <em><strong>r</strong>emote <strong>sync</strong>hronization</em> tool. This means that its primary use lies in the filed of synchronizing files and directories between remote Unix systems, but I feel that it's really important for you to understand the basics before moving on to more advanced stuff.</p>
<p>The beauty of <strong>rsync </strong>is that its syntax and command line options are exactly the same for local and remote directories synchronizations, so it's not like you'll have to learn it all from scratch when I show you the real world usage of <strong>rsync </strong>in my next post.</p>
<h3><strong>rsync experiments setup</strong></h3>
<p>Just use the following commands to re-create your rsync playground for this post:</p>
<pre><strong>ubuntu$</strong> rm -rf /tmp/dir1 /tmp/dir2
<strong>ubuntu$</strong> mkdir /tmp/dir1 /tmp/dir2
<strong>ubuntu$</strong> cd /tmp
<strong>ubuntu$</strong> echo "original file 1" &gt; dir1/file1
<strong>ubuntu$</strong> echo "original file 2" &gt; dir1/file2
<strong>ubuntu$</strong> echo "original file 3" &gt; dir1/file3
<strong>ubuntu$ </strong>cp dir1/file1 dir2</pre>
<p>It will probably be more convenient for you to just download the same commands in form of a script though, so here's the link: <a title="rsync setup script" href="http://www.unixtutorial.org/examples/rsync-setup.sh">rsync-setup.sh</a></p>
<h3>Tracking rsync progress</h3>
<p>The larger the directories you're synchronizing with rsync, the longer it will take for the command to finish. Depending on the scale of your task, it can be minutes, long hours or even days before you get the synchronization complete. With this in mind, the importance of progress tracking with rsync should be obvious to you.</p>
<p>Here's how you make <strong>rsync </strong>report its progress: just use the <strong>&#8211;progress</strong> command line option (in addition to the command line I introduced you to last time):</p>
<pre>ubuntu$ <strong>rsync -avz --stats --progress /tmp/dir1/ /tmp/dir2</strong>
building file list ...
4 files to consider
file2
          16 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/4)
file3
          16 100%   15.62kB/s    0:00:00 (xfer#2, to-check=0/4)</pre>
<pre>Number of files: 4
Number of files transferred: 2
Total file size: 48 bytes
Total transferred file size: 32 bytes
Literal data: 32 bytes
Matched data: 0 bytes
File list size: 59
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 193
Total bytes received: 64</pre>
<pre>sent 193 bytes  received 64 bytes  514.00 bytes/sec
total size is 48  speedup is 0.19</pre>
<h3><strong>Why you should track the progress of rsyncs</strong></h3>
<p>The key differences are that you first get a regularly updated message about building a file list. When you run into hundreds of thousands of files, this becomes quite useful:</p>
<pre>building file list ...
4 files to consider</pre>
<p>Another way to track progress is to see the transfer progress for each of the involved files. For thousands tiny files it won't be all that impressive a feature, but if you're transferring huge files between remote systems, a dynamic transfer progress for each file will be exactly what you need:</p>
<pre>file2
          16 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/4)
file3
          16 100%   15.62kB/s    0:00:00 (xfer#2, to-check=0/4)</pre>
<p>That's it, hope you like this new option. I promise to tell you more some other time!</p>
<h3>See also:</h3>
<ul>
<li><strong><a title="rsync synchronization" href="http://www.unixtutorial.org/2008/09/how-to-synchronize-directories-with-rsync/">Synchronize Directories in Unix with rsync command</a></strong></li>
<li><strong><a title="How to compare Unix directories" href="http://www.unixtutorial.org/2008/06/how-to-compare-directories-in-unix/">Comparing directories in Unix</a></strong></li>
<li><strong><a title="How to find large files and directories" href="http://www.unixtutorial.org/2008/03/find-large-files-and-directories/">Finding large files and directories</a><br />
</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.unixtutorial.org/2008/10/tracking-the-progress-of-rsync-transfers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/43 queries in 0.012 seconds using disk: basic
Object Caching 625/724 objects using disk: basic
Content Delivery Network via Amazon Web Services: S3: unixtutorial.s3.amazonaws.com

Served from: www.unixtutorial.org @ 2012-02-04 19:27:17 -->
