<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8904977416785454796</id><updated>2024-09-16T07:54:14.252+08:00</updated><category term="linux"/><category term="shell"/><category term="tips"/><category term="tricks"/><category term="distribution"/><category term="io redirect"/><category term="redirect"/><category term="version"/><title type='text'>Linux Tips</title><subtitle type='html'>Collections of useful Linux tips that will help you to increase your efficiency in daily life.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://linuxeasygo.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://linuxeasygo.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jackie Yeh</name><uri>http://www.blogger.com/profile/11918635100118582131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8904977416785454796.post-8416190398301675723</id><published>2011-12-02T19:09:00.001+08:00</published><updated>2011-12-02T19:13:43.964+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="shell"/><category scheme="http://www.blogger.com/atom/ns#" term="tips"/><category scheme="http://www.blogger.com/atom/ns#" term="tricks"/><title type='text'>3 Advanced usages of Linux ‘date’ command</title><content type='html'>In Linux shell, ‘date’ is a basic command to show current date, like:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;&lt;tt&gt; $ date&lt;br /&gt;Sat Jan&amp;nbsp; 8 15:34:16 CST 2011&lt;br /&gt;$&lt;/tt&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
From the result, it shows more than just date, and also time and time zone. It’s different from what DOS does, which use two command to get/set date and time. However the ‘date’ command can do much more than this, let’s first check the usage.&lt;br /&gt;
Standard version:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;&lt;tt&gt; $ date --help&lt;br /&gt;Usage: date [OPTION]... [+FORMAT]&lt;br /&gt;&amp;nbsp; or:&amp;nbsp; date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]&lt;br /&gt;Display the current time in the given FORMAT, or set the system date.&lt;br /&gt;...[skipped]&lt;/tt&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;busybox version:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;&lt;tt&gt;&lt;/tt&gt; &lt;br /&gt;
&lt;tt&gt;# date --help&lt;br /&gt;BusyBox v1.11.1 (2010-12-23 19:34:38 CST) multi-call binary &lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;Usage: date [OPTION]... [+FMT] [TIME] &lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;Display time (using +FMT), or set time&lt;br /&gt;...[skipped]&lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;&lt;/tt&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The busybox version has less functions than standard version (of course!), however the following scenarios are supported on both versions.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;img align=&quot;middle&quot; src=&quot;http://www.dotnetscraps.com/samples/bullets/005.gif&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1. Show current date/time in a specific format&lt;/h3&gt;
&lt;blockquote&gt;
If the ‘date’ command is followed by a plus sign (+) and immediately the specific format string, it can be sued to determine the date/time output format. Here is all the supported format for ‘date’ command.&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;&lt;tt&gt;&lt;/tt&gt; &lt;br /&gt;
&lt;tt&gt;FORMAT controls the output.&amp;nbsp; The only valid option for the second form&lt;br /&gt;specifies Coordinated Universal Time.&amp;nbsp; Interpreted sequences are: &lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;&amp;nbsp; %%&amp;nbsp;&amp;nbsp; a literal %&lt;br /&gt;&amp;nbsp; %a&amp;nbsp;&amp;nbsp; locale&#39;s abbreviated weekday name (e.g., Sun)&lt;br /&gt;&amp;nbsp; %A&amp;nbsp;&amp;nbsp; locale&#39;s full weekday name (e.g., Sunday)&lt;br /&gt;&amp;nbsp; %b&amp;nbsp;&amp;nbsp; locale&#39;s abbreviated month name (e.g., Jan)&lt;br /&gt;&amp;nbsp; %B&amp;nbsp;&amp;nbsp; locale&#39;s full month name (e.g., January)&lt;br /&gt;&amp;nbsp; %c&amp;nbsp;&amp;nbsp; locale&#39;s date and time (e.g., Thu Mar&amp;nbsp; 3 23:05:25 2005)&lt;br /&gt;&amp;nbsp; %C&amp;nbsp;&amp;nbsp; century; like %Y, except omit last two digits (e.g., 21)&lt;br /&gt;&amp;nbsp; %d&amp;nbsp;&amp;nbsp; day of month (e.g, 01)&lt;br /&gt;&amp;nbsp; %D&amp;nbsp;&amp;nbsp; date; same as %m/%d/%y&lt;br /&gt;&amp;nbsp; %e&amp;nbsp;&amp;nbsp; day of month, space padded; same as %_d&lt;br /&gt;&amp;nbsp; %F&amp;nbsp;&amp;nbsp; full date; same as %Y-%m-%d&lt;br /&gt;&amp;nbsp; %g&amp;nbsp;&amp;nbsp; last two digits of year of ISO week number (see %G)&lt;br /&gt;&amp;nbsp; %G&amp;nbsp;&amp;nbsp; year of ISO week number (see %V); normally useful only with %V&lt;br /&gt;&amp;nbsp; %h&amp;nbsp;&amp;nbsp; same as %b&lt;br /&gt;&amp;nbsp; %H&amp;nbsp;&amp;nbsp; hour (00..23)&lt;br /&gt;&amp;nbsp; %I&amp;nbsp;&amp;nbsp; hour (01..12)&lt;br /&gt;&amp;nbsp; %j&amp;nbsp;&amp;nbsp; day of year (001..366)&lt;br /&gt;&amp;nbsp; %k&amp;nbsp;&amp;nbsp; hour ( 0..23)&lt;br /&gt;&amp;nbsp; %l&amp;nbsp;&amp;nbsp; hour ( 1..12)&lt;br /&gt;&amp;nbsp; %m&amp;nbsp;&amp;nbsp; month (01..12)&lt;br /&gt;&amp;nbsp; %M&amp;nbsp;&amp;nbsp; minute (00..59)&lt;br /&gt;&amp;nbsp; %n&amp;nbsp;&amp;nbsp; a newline&lt;br /&gt;&lt;span style=&quot;color: red;&quot;&gt;&amp;nbsp; %N&amp;nbsp;&amp;nbsp; nanoseconds (000000000..999999999)&lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&amp;nbsp; %p&amp;nbsp;&amp;nbsp; locale&#39;s equivalent of either AM or PM; blank if not known&lt;br /&gt;&amp;nbsp; %P&amp;nbsp;&amp;nbsp; like %p, but lower case&lt;br /&gt;&amp;nbsp; %r&amp;nbsp;&amp;nbsp; locale&#39;s 12-hour clock time (e.g., 11:11:04 PM)&lt;br /&gt;&amp;nbsp; %R&amp;nbsp;&amp;nbsp; 24-hour hour and minute; same as %H:%M&lt;br /&gt;&amp;nbsp; %s&amp;nbsp;&amp;nbsp; seconds since 1970-01-01 00:00:00 UTC&lt;br /&gt;&amp;nbsp; %S&amp;nbsp;&amp;nbsp; second (00..60)&lt;br /&gt;&amp;nbsp; %t&amp;nbsp;&amp;nbsp; a tab&lt;br /&gt;&amp;nbsp; %T&amp;nbsp;&amp;nbsp; time; same as %H:%M:%S&lt;br /&gt;&amp;nbsp; %u&amp;nbsp;&amp;nbsp; day of week (1..7); 1 is Monday&lt;br /&gt;&amp;nbsp; %U&amp;nbsp;&amp;nbsp; week number of year, with Sunday as first day of week (00..53)&lt;br /&gt;&amp;nbsp; %V&amp;nbsp;&amp;nbsp; ISO week number, with Monday as first day of week (01..53)&lt;br /&gt;&amp;nbsp; %w&amp;nbsp;&amp;nbsp; day of week (0..6); 0 is Sunday&lt;br /&gt;&amp;nbsp; %W&amp;nbsp;&amp;nbsp; week number of year, with Monday as first day of week (00..53)&lt;br /&gt;&amp;nbsp; %x&amp;nbsp;&amp;nbsp; locale&#39;s date representation (e.g., 12/31/99)&lt;br /&gt;&amp;nbsp; %X&amp;nbsp;&amp;nbsp; locale&#39;s time representation (e.g., 23:13:48)&lt;br /&gt;&amp;nbsp; %y&amp;nbsp;&amp;nbsp; last two digits of year (00..99)&lt;br /&gt;&amp;nbsp; %Y&amp;nbsp;&amp;nbsp; year&lt;br /&gt;&amp;nbsp; %z&amp;nbsp;&amp;nbsp; +hhmm numeric timezone (e.g., -0400)&lt;br /&gt;&lt;span style=&quot;color: red;&quot;&gt;&amp;nbsp; %:z&amp;nbsp; +hh:mm numeric timezone (e.g., -04:00)&lt;br /&gt;&amp;nbsp; %::z&amp;nbsp; +hh:mm:ss numeric time zone (e.g., -04:00:00)&lt;br /&gt;&amp;nbsp; %:::z&amp;nbsp; numeric time zone with : to necessary precision (e.g., -04, +05:30)&lt;/span&gt;&lt;br /&gt;&amp;nbsp; %Z&amp;nbsp;&amp;nbsp; alphabetic time zone abbreviation (e.g., EDT) &lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;&lt;span style=&quot;color: red;&quot;&gt;By default, date pads numeric fields with zeroes.&lt;br /&gt;The following optional flags may follow `%&#39;: &lt;/span&gt;&lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;&lt;span style=&quot;color: red;&quot;&gt;&amp;nbsp; -&amp;nbsp; (hyphen) do not pad the field&lt;br /&gt;&amp;nbsp; _&amp;nbsp; (underscore) pad with spaces&lt;br /&gt;&amp;nbsp; 0&amp;nbsp; (zero) pad with zeros&lt;br /&gt;&amp;nbsp; ^&amp;nbsp; use upper case if possible&lt;br /&gt;&amp;nbsp; #&amp;nbsp; use opposite case if possible&lt;/span&gt;&lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;&lt;/tt&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
It looks a very long table, it’s mandatory to meet any requests. Among these usage, the red color part are not supported by busybox version.&lt;br /&gt;
Let’s discover the format by presenting some examples:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ date&lt;br /&gt;Sat Jan&amp;nbsp; 8 16:32:05 CST 2011&lt;br /&gt;$ date +%x&lt;br /&gt;01/08/2011&lt;br /&gt;$ date %X&lt;br /&gt;date: invalid date `%X&#39; &lt;span style=&quot;color: #408080;&quot;&gt;#you’ll get such prompt when missing ‘+’&lt;/span&gt;$ date +%X&lt;br /&gt;04:32:22 PM&lt;br /&gt;$ date +%a&lt;br /&gt;Sat&lt;br /&gt;$ date +%A&lt;br /&gt;Saturday&lt;br /&gt;$ date +%D&lt;br /&gt;01/08/11&lt;br /&gt;$ date +%F&lt;br /&gt;2011-01-08&lt;br /&gt;$ &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
If you want a more complicated output (like including a single quote (‘) in your output, you’ll need the double quote (“) to enclose your format string. Here is the example:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ date +&quot;Hello world. It&#39;s: %Y/%m/%d %H:%M:%S&quot;&lt;br /&gt;Hello world. It&#39;s: 2011/01/08 16:37:34&lt;br /&gt;$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;Yet there is a very special format, normally used when writing a shell script. It’s “%s”, it will show the elapsed time in seconds since January first 1970 (1970/1/1). If you holds two different time with above seconds, then substrate these two number will get the elapsed time (We’ll talk about this later in details). Here is the output of “%s”:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ date +%s&lt;br /&gt;1294476056&lt;br /&gt;$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;img align=&quot;middle&quot; src=&quot;http://www.dotnetscraps.com/samples/bullets/005.gif&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2. Set system time&lt;/h3&gt;
First of all, one thing you should know: Only root has the permission to set system time. Any other users trying to do this will get a error message “Operation not permitted”, and system time keep unchanged:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ date 01020101&lt;br /&gt;date: cannot set date: Operation not permitted&lt;br /&gt;Sun Jan&amp;nbsp; 2 01:01:00 CST 2011&lt;br /&gt;$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Normally we don’t do this in a PC Linux, maybe you’ll use NTP client to synchronize with NTP server to get a precision time. Sometimes we’ll set system time in a embedded device, so let’s check the command syntax on busybox:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;# date --help&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...[skipped]&lt;br /&gt;Recognized formats for TIME:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hh:mm[:ss]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [YYYY.]MM.DD-hh:mm[:ss]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; YYYY-MM-DD hh:mm[:ss]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MMDDhhmm[[YY]YY][.ss] &lt;br /&gt;
#&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
busybox provide 4 main formats to set system time, for each format you can yet skipped some parts. Frankly speaking, it’s kind of lousy. So the practical exercise is knowing how to get the help and follow it.Here are some examples:&lt;br /&gt;
1. Set system time to 5:35&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;# date&lt;br /&gt;Mon Jan 10 07:03:27 UTC 2011&lt;br /&gt;# date 5:35&lt;br /&gt;Mon Jan 10 05:35:00 UTC 2011&lt;br /&gt;#&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
2. Set system date/time to May 8th 0:00&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;# date&lt;br /&gt;Mon Jan 10 05:36:32 UTC 2011&lt;br /&gt;
# date 05080000&lt;br /&gt;Sun May&amp;nbsp; 8 00:00:00 UTC 2011&lt;br /&gt;#&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
3. Set date/time to 2011 Jan 11st 8:00 PM&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;# date&lt;br /&gt;Mon Jan 10 05:36:32 UTC 2011&lt;br /&gt;# date 2011.01.11-20:00&lt;br /&gt;Tue Jan 11 20:00:00 UTC 2011&lt;br /&gt;#&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
As the 3 examples show, you can set just time, or date without year but with time, or a complete setting on year, month, day and time.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;img align=&quot;middle&quot; src=&quot;http://www.dotnetscraps.com/samples/bullets/005.gif&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3. Calculate elapsed time&lt;/h3&gt;
Sometimes we’ll use shell script to do a test or monitor something on the system. It’s good to know how much time has been elapsed.&lt;br /&gt;
Utilize “date +%s” mentioned above, and a little calculation, we can generate a shell function to calculate elapsed time. Here is an example:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ cat elap.bash&lt;br /&gt;#!/bin/bash&lt;br /&gt;function elapsed_time()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; s_time=$1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e_time=$2 &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; elap_s=$((e_time-s_time))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ss=$((elap_s%60))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mm=$(((elap_s/60)%60))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hh=$((elaps/3600))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf &quot;%i:%02i:%02i&quot; $hh $mm $ss&lt;br /&gt;} &lt;br /&gt;
begin_time=`date &quot;+%s&quot;` &lt;br /&gt;
for i in {1..3};&lt;br /&gt;do&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; now_time=`date &quot;+%s&quot;`&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elap_time=`elapsed_time $begin_time $now_time`&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;elapsed time=$elap_time&quot;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sleep 1&lt;br /&gt;done &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;The function “elapsed_time” reads two arguments generated by “date +%s”, they present two different time point. After a simple calculation, hh, mm and ss, represent elapsed hours, minutes and seconds respectively. Finally the formatted output function “printf” is used to generate the output. One thing to know: this is a simple example, so it doesn’t cover the “day” concept, modify it yourself if you need it.&lt;br /&gt;
This simple script will call “elapsed_time” function every second, and print elapsed time.&lt;br /&gt;
Here is the execution result:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot; cellspacing=&quot;15&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ source elap.bash&lt;br /&gt;elapsed time=0:00:00&lt;br /&gt;elapsed time=0:00:01&lt;br /&gt;elapsed time=0:00:02&lt;br /&gt;$&lt;br /&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
&lt;img align=&quot;middle&quot; src=&quot;http://www.dotnetscraps.com/samples/bullets/005.gif&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Summary&lt;/h3&gt;
&lt;blockquote&gt;
Although ‘date’ is a very common command that we usually used, it may also reach a useful function.&lt;/blockquote&gt;</content><link rel='replies' type='application/atom+xml' href='http://linuxeasygo.blogspot.com/feeds/8416190398301675723/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://linuxeasygo.blogspot.com/2011/12/3-advanced-usages-of-linux-date-command.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/8416190398301675723'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/8416190398301675723'/><link rel='alternate' type='text/html' href='http://linuxeasygo.blogspot.com/2011/12/3-advanced-usages-of-linux-date-command.html' title='3 Advanced usages of Linux ‘date’ command'/><author><name>Jackie Yeh</name><uri>http://www.blogger.com/profile/11918635100118582131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8904977416785454796.post-6781113144938778877</id><published>2011-11-24T19:50:00.001+08:00</published><updated>2011-11-24T20:02:39.245+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="shell"/><category scheme="http://www.blogger.com/atom/ns#" term="tips"/><category scheme="http://www.blogger.com/atom/ns#" term="tricks"/><title type='text'>2 ways to generate random numbers in Bash shell</title><content type='html'>&lt;h3&gt;




&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Have you ever tried to write Bash shell script and need a random number generator? Here provides 2 ways to achieve this.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3&gt;




1. Use the Built-in variable $RANDOM of Bash&lt;/h3&gt;
$RANDOM is a built-in variable of Bash, it can be used to generate a random number. It&#39;s very easy to use, we can use &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;echo &lt;/span&gt;to get its value. Here is the example:&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ echo $RANDOM&lt;br /&gt;
4371&lt;br /&gt;
$ echo $RANDOM&lt;br /&gt;
20269&lt;br /&gt;
$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Below is another example, which will generate 3 random numbers automatically:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ for i in {1..3}; do echo $RANDOM; done&lt;br /&gt;
11434&lt;br /&gt;
9696&lt;br /&gt;
32237&lt;br /&gt;
$ &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The number generated by&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt; $RANDOM&lt;/span&gt; is between&amp;nbsp; &lt;b&gt;0 and 32767&lt;/b&gt;, &lt;a href=&quot;http://www.faqs.org/docs/bashman/bashref_60.html#IDX227&quot;&gt;here &lt;/a&gt;is the explanation in detail.&lt;br /&gt;
&lt;br /&gt;
If you want another range, you can use the following method to generate it. Here we create a shell function &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;random_range&lt;/span&gt;,&amp;nbsp; which can accept up to 2 arguments. If there is only one argument, it will generate random number between 0 and first argument. Yet if there is 2 arguments, these two numbers will be the lower bound and upper bound of generated numbers. (To keep it simple in the example, there is no strict checking mechanism, please make sure the second argument is larger than the first one).&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;function random_range()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if [ &quot;$#&quot; -lt &quot;2&quot; ]; then &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &quot;Usage: random_range &amp;lt;low&amp;gt; &amp;lt;high&amp;gt;&quot;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fi&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; low=$1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; range=$(($2 - $1))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; echo $(($low+$RANDOM % $range))&lt;br /&gt;
} &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Here is the execution results:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ random_range&amp;nbsp; 10&lt;br /&gt;
Usage: random_range &amp;lt;low&amp;gt; &amp;lt;high&amp;gt;&lt;br /&gt;
$ random_range&amp;nbsp; 10 30&lt;br /&gt;
18&lt;br /&gt;
$ random_range&amp;nbsp; 10 30&lt;br /&gt;
13&lt;br /&gt;
$ random_range&amp;nbsp; 10 30&lt;br /&gt;
10&lt;br /&gt;
$ random_range&amp;nbsp; 10 300&lt;br /&gt;
85&lt;br /&gt;
$ random_range&amp;nbsp; 10 300&lt;br /&gt;
221&lt;br /&gt;
$ random_range&amp;nbsp; 10 300&lt;br /&gt;
103&lt;br /&gt;
$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3&gt;



&amp;nbsp;&lt;/h3&gt;
&lt;h3&gt;




2. Use the special file /dev/urandom&lt;/h3&gt;
There are 2 special files in Linux-like system: &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;dev/random&lt;/span&gt; and &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;dev/urandom&lt;/span&gt;. They are used to &lt;a href=&quot;http://en.wikipedia.org/wiki//dev/urandom&quot;&gt;provide the random number for Kernel&lt;/a&gt;.&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;urandom&lt;/span&gt; is the one which is proper to act as a normal use in user space.&lt;br /&gt;
&lt;br /&gt;
The following command utilizes the &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;od &lt;/span&gt;command to generate a random number between 0 and 32767, which is exactly the same as &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;above &lt;/span&gt;$echo $RANDOM &lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;command&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;where the random number is 16-bits wide.&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ od -vAn -N2 -i /dev/urandom&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6484&lt;br /&gt;
$ od -vAn -N2 -i /dev/urandom&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25989&lt;br /&gt;
$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;od &lt;/span&gt;command alone with&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt; /dev/urandom&lt;/span&gt;&amp;nbsp; can yet generate a much larger random number. Below is an example with 32-bits wide random number which is located in the range of 0&amp;nbsp; ~ 2147483645.&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ od -vAn -N4 -tu4&amp;nbsp; /dev/urandom&lt;br /&gt;
2981514769&lt;br /&gt;
$ od -vAn -N4 -tu4&amp;nbsp; /dev/urandom&lt;br /&gt;
1055285400&lt;br /&gt;
$&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;h3&gt;



Summary&lt;/h3&gt;
&lt;h3&gt;



&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;Toda&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;y 2 ways are provided to generate random numbers in Bash. Apparently the first way with &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;$RANDOM&lt;/span&gt; is a easier way. However if you need a very large number, the second way with &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;/dev/urandom&lt;/span&gt; could be your better choice.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3&gt;



&amp;nbsp;&lt;/h3&gt;</content><link rel='replies' type='application/atom+xml' href='http://linuxeasygo.blogspot.com/feeds/6781113144938778877/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://linuxeasygo.blogspot.com/2011/11/2-ways-to-generate-random-numbers-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/6781113144938778877'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/6781113144938778877'/><link rel='alternate' type='text/html' href='http://linuxeasygo.blogspot.com/2011/11/2-ways-to-generate-random-numbers-in.html' title='2 ways to generate random numbers in Bash shell'/><author><name>Jackie Yeh</name><uri>http://www.blogger.com/profile/11918635100118582131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8904977416785454796.post-5809025704539913181</id><published>2011-11-17T12:26:00.001+08:00</published><updated>2011-11-24T20:02:48.722+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="io redirect"/><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="redirect"/><category scheme="http://www.blogger.com/atom/ns#" term="shell"/><category scheme="http://www.blogger.com/atom/ns#" term="tips"/><category scheme="http://www.blogger.com/atom/ns#" term="tricks"/><title type='text'>Linux shell IO redirect</title><content type='html'>IO redirect is fundamental in Linux shell -- one basic thing you should&amp;nbsp; pay your attention when learning/using Linux.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: medium;&quot;&gt; &lt;h3&gt;


3 Basic File descriptors:&lt;/h3&gt;
&lt;/span&gt; &lt;br /&gt;
&lt;table bgcolor=&quot;white&quot; border=&quot;2&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;#0080ff&quot; bordercolorlight=&quot;#0080c0&quot; style=&quot;width: 589px;&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td width=&quot;152&quot;&gt;&lt;b&gt;File Descriptor&lt;/b&gt;&lt;/td&gt; &lt;td width=&quot;154&quot;&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/td&gt; &lt;td width=&quot;279&quot;&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt; &lt;td width=&quot;152&quot;&gt;0&lt;/td&gt; &lt;td width=&quot;154&quot;&gt;stdin&lt;/td&gt; &lt;td width=&quot;279&quot;&gt;standard input &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt; &lt;td width=&quot;152&quot;&gt;1&lt;/td&gt; &lt;td width=&quot;154&quot;&gt;stdout&lt;/td&gt; &lt;td width=&quot;279&quot;&gt;standard output&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt; &lt;td width=&quot;152&quot;&gt;2&lt;/td&gt; &lt;td width=&quot;154&quot;&gt;stderr&lt;/td&gt; &lt;td width=&quot;279&quot;&gt;standard error&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3&gt;


&lt;/h3&gt;
&lt;h3&gt;


&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Below I&#39;ll introduce 3 advanced usage, well they are also very common scenarios.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3&gt;


1. Redirect stderr to stdout&lt;/h3&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ ls non-exist 2&amp;gt;&amp;amp;1&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3&gt;


&lt;/h3&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;h3&gt;


&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Note the &quot;2&amp;gt;&amp;amp;1&quot; sequence, please keep them there&lt;/span&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;. This command&lt;/span&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt; will redirect stderr to stdout.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3&gt;


&amp;nbsp;&lt;/h3&gt;
&lt;h3&gt;


2. Output stdout and stderr to a file simultaneously&lt;/h3&gt;
Think about this: you are compiling a huge project which uses Makefile, the compilation failed and a lot of error message shown on the scree. It could be easier for you to analyze these error messages in your favorite editor.&lt;br /&gt;
&lt;br /&gt;
Here is the solution. Either of below ways will achieve this. And of course the show-up sequence is exactly the same as on the screen.&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ find /var -name run &amp;amp;&amp;gt;file&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ find /var -name run &amp;gt;file 2&amp;gt;&amp;amp;1&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
Please note:(1) The sequence of &quot;&amp;amp;&quot; and &quot;&amp;gt;&quot; on two ways is different, don&#39;t mix them up!&lt;br /&gt;
(2) For the second one, you must assign stdout first, then stderr. &lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;


 &lt;/h3&gt;
&lt;h3&gt;


 &lt;/h3&gt;
&lt;h3&gt;


3. Ignore stdout or stderr&lt;/h3&gt;
Sometimes you don&#39;t want to see some message just because they are trifling or you want to keep a clean screen. You can put everything into the bottomless pit: /dev/null:&lt;br /&gt;
&lt;br /&gt;
This example ignores stderr:&lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ find /var -name run 2&amp;gt;/dev/null&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
This example ignores both stdout and stderr: &lt;br /&gt;
&lt;table bgcolor=&quot;#ebebeb&quot; border=&quot;1&quot; bordercolor=&quot;black&quot; bordercolordark=&quot;silver&quot; bordercolorlight=&quot;silver&quot;&gt; &lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;$ secret-app 1&amp;gt;/dev/null 2&amp;gt;/dev/null &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;pix-related-post pix-related-post-showed&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
&lt;!--
google_ad_client = &quot;ca-pub-8236209678463697&quot;;
/* 336x280-大矩形圖像 */
google_ad_slot = &quot;3486904185&quot;;
google_ad_width = 336;
google_ad_height = 280;
//--&gt;
&lt;/script&gt;
&lt;script src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;ins style=&quot;border: medium none; display: inline-table; height: 280px; margin: 0pt; padding: 0pt; position: relative; visibility: visible; width: 336px;&quot;&gt;&lt;ins id=&quot;aswift_1_anchor&quot; style=&quot;border: medium none; display: block; height: 280px; margin: 0pt; padding: 0pt; position: relative; visibility: visible; width: 336px;&quot;&gt;&lt;iframe allowtransparency=&quot;true&quot; frameborder=&quot;0&quot; height=&quot;280&quot; hspace=&quot;0&quot; id=&quot;aswift_1&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; name=&quot;aswift_1&quot; onload=&quot;var i=this.id,s=window.google_iframe_oncopy,H=s&amp;amp;&amp;amp;s.handlers,h=H&amp;amp;&amp;amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;amp;&amp;amp;d&amp;amp;&amp;amp;(!d.body||!d.body.firstChild)){if(h.call){i+=&#39;.call&#39;;setTimeout(h,0)}else if(h.match){i+=&#39;.nav&#39;;w.location.replace(h)}s.log&amp;amp;&amp;amp;s.log.push(i)}&quot; scrolling=&quot;no&quot; style=&quot;left: 0pt; position: absolute; top: 0pt;&quot; vspace=&quot;0&quot; width=&quot;336&quot;&gt;&lt;/iframe&gt;&lt;/ins&gt;&lt;/ins&gt;&lt;script type=&quot;text/javascript&quot;&gt;
&lt;!--
google_ad_client = &quot;ca-pub-8236209678463697&quot;;
/* 336x280-大矩形圖像 */
google_ad_slot = &quot;3486904185&quot;;
google_ad_width = 336;
google_ad_height = 280;
//--&gt;
&lt;/script&gt;
&lt;script src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;ins style=&quot;border: medium none; display: inline-table; height: 280px; margin: 0pt; padding: 0pt; position: relative; visibility: visible; width: 336px;&quot;&gt;&lt;ins id=&quot;aswift_1_anchor&quot; style=&quot;border: medium none; display: block; height: 280px; margin: 0pt; padding: 0pt; position: relative; visibility: visible; width: 336px;&quot;&gt;&lt;iframe allowtransparency=&quot;true&quot; frameborder=&quot;0&quot; height=&quot;280&quot; hspace=&quot;0&quot; id=&quot;aswift_1&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; name=&quot;aswift_1&quot; onload=&quot;var i=this.id,s=window.google_iframe_oncopy,H=s&amp;amp;&amp;amp;s.handlers,h=H&amp;amp;&amp;amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;amp;&amp;amp;d&amp;amp;&amp;amp;(!d.body||!d.body.firstChild)){if(h.call){i+=&#39;.call&#39;;setTimeout(h,0)}else if(h.match){i+=&#39;.nav&#39;;w.location.replace(h)}s.log&amp;amp;&amp;amp;s.log.push(i)}&quot; scrolling=&quot;no&quot; style=&quot;left: 0pt; position: absolute; top: 0pt;&quot; vspace=&quot;0&quot; width=&quot;336&quot;&gt;&lt;/iframe&gt;&lt;/ins&gt;&lt;/ins&gt;</content><link rel='replies' type='application/atom+xml' href='http://linuxeasygo.blogspot.com/feeds/5809025704539913181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://linuxeasygo.blogspot.com/2011/11/linux-shell-io-redirect.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/5809025704539913181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/5809025704539913181'/><link rel='alternate' type='text/html' href='http://linuxeasygo.blogspot.com/2011/11/linux-shell-io-redirect.html' title='Linux shell IO redirect'/><author><name>Jackie Yeh</name><uri>http://www.blogger.com/profile/11918635100118582131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8904977416785454796.post-9185778497412591620</id><published>2011-11-16T18:08:00.001+08:00</published><updated>2011-11-24T20:02:59.973+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="distribution"/><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="shell"/><category scheme="http://www.blogger.com/atom/ns#" term="tips"/><category scheme="http://www.blogger.com/atom/ns#" term="tricks"/><category scheme="http://www.blogger.com/atom/ns#" term="version"/><title type='text'>How to check Linux distribution version</title><content type='html'>Do you know how to check Linux distribution version by a shell command?&lt;br /&gt;
&lt;br /&gt;
In Linux, the most common command to show current version is &quot;&lt;span id=&quot;gtbmisp_0&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;uname&lt;/span&gt;-r&quot; to check the kernel version:&lt;br /&gt;
&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;596&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;[jackieyeh@dhcppc15 ~]$ &lt;span id=&quot;gtbmisp_1&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;uname&lt;/span&gt; -r&lt;br /&gt;2.6.18-6-686&lt;br /&gt;[jackieyeh@dhcppc15 ~]$&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
What if you&#39;d like to know the distribution version in stead of kernel version?&lt;br /&gt;
&lt;br /&gt;
Since there are some many &quot;flavors&quot; in Linux, the Linux version is distinguished not only by kernel version, but also distribution versions, like Fedora, Debian, Ubuntu, etc...&lt;br /&gt;
&lt;br /&gt;
Here is the summary of the ways to get the distribution version. Although different distributions may have slight difference, luckily you can get the answers with couple trials.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;

&lt;b&gt;1. cat /proc/version&lt;/b&gt;&lt;/h3&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
By typing &quot;cat /proc/version&quot;, you&#39;ll get similar results as &quot;&lt;span id=&quot;gtbmisp_5&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;uname&lt;/span&gt;&quot;, but it&#39;s more complete. This is a generic command for every distribution, you may or may not get the distribution information, but anyway you can get some &quot;clues&quot;. Below are 2 examples of Fedora and Debian:&lt;/blockquote&gt;
&lt;blockquote&gt;
Fedora:&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;596&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;[jackieyeh@vega ~]$ cat /proc/version&lt;br /&gt;Linux version 2.6.23.17-88.&lt;span style=&quot;color: red;&quot;&gt;fc7&lt;/span&gt; (mockbuild@xenbuilder2.fedora.redhat.com) (&lt;span id=&quot;gtbmisp_7&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;gcc&lt;/span&gt; version 4.1.2 20070925 (&lt;span style=&quot;color: red;&quot;&gt;Red Hat 4.1.2-27&lt;/span&gt;)) #1 SMP Thu May 15 00:35:10 EDT 2008&lt;br /&gt;[jackieyeh@vega ~]$&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
Debian:&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;600&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;&lt;span id=&quot;gtbmisp_8&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;packetship&lt;/span&gt;:~# cat /proc/version&lt;br /&gt;Linux version 2.6.18-6-686 (Debian 2.6.18.dfsg.1-24etch2) (dannf@debian.org) (&lt;span id=&quot;gtbmisp_10&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;gcc&lt;/span&gt; version 4.1.2 20061115 (&lt;span id=&quot;gtbmisp_11&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;prerelease&lt;/span&gt;) (&lt;span style=&quot;color: red;&quot;&gt;Debian 4.1.1-21&lt;/span&gt;)) #1 SMP Tue May 5 00:40:20 UTC 2009&lt;br /&gt;&lt;span id=&quot;gtbmisp_12&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;packetship&lt;/span&gt;:~#&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;

2. cat /etc/*-release&lt;/h3&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
By typing &quot;cat /etc/*-release&quot;, you may get the distribution version. The &quot;*&quot; is a wild-card character as we don&#39;t know the exact versions (that&#39;s what we try to get, remember?). This command is effective on Fedora and Ubuntu. Below is an example of Fedora.&lt;/blockquote&gt;
&lt;blockquote&gt;
Fedora:&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;596&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;[jackieyeh@vega &lt;span id=&quot;gtbmisp_14&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;src&lt;/span&gt;]$ cat /etc/*-release&lt;br /&gt;Fedora release 7 (Moonshine)&lt;br /&gt;Fedora release 7 (Moonshine)&lt;br /&gt;[jackieyeh@vega &lt;span id=&quot;gtbmisp_15&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;src&lt;/span&gt;]$&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;

3. cat /etc/issue&lt;/h3&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
By tying &quot;cat /etc/issue&quot;, you can always get the result -- This is effective for every distribution since it&#39;s a standard file for Linux system and always there. Normally this file will show distribution version by default. However this file is usually edited by the system administrator to show welcome message when a user log in the system -- therefore the distribution version may be removed. &lt;/blockquote&gt;
&lt;blockquote&gt;
Below are 2 examples of Fedora and Debian:&lt;/blockquote&gt;
&lt;blockquote&gt;
Fedora:&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;596&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;[jackieyeh@vega ~]$ cat /etc/issue&lt;br /&gt;Fedora release 7 (Moonshine)&lt;br /&gt;Kernel \r on an \m &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: yellow;&quot;&gt;[jackieyeh@vega ~]$&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Debian:&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;600&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;&lt;span id=&quot;gtbmisp_16&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;packetship&lt;/span&gt;:~# cat /etc/issue&lt;br /&gt;Debian GNU/Linux 4.0 \n \l&lt;/span&gt; &lt;br /&gt;
&lt;span style=&quot;color: yellow;&quot;&gt;&lt;span id=&quot;gtbmisp_17&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;packetship&lt;/span&gt;:~#&lt;/span&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;

4. cat /etc/debian_version &lt;/h3&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
Obviously, typing &quot;cat /etc/debian_version&quot; is exclusively for Debian. It will show Debian distribution version without any problem.&lt;/blockquote&gt;
&lt;blockquote&gt;
Debian:&lt;br /&gt;
&lt;table bgcolor=&quot;black&quot; border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; style=&quot;width: 600px;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;600&quot;&gt;&lt;span style=&quot;color: yellow;&quot;&gt;&lt;span id=&quot;gtbmisp_20&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;packetship&lt;/span&gt;:~# cat /etc/debian_version&lt;br /&gt;4.0&lt;br /&gt;&lt;span id=&quot;gtbmisp_22&quot; style=&quot;background: none repeat scroll 0% 0% transparent; border: 0pt none; color: red; cursor: pointer; font: bold 100% serif; margin: 0pt; padding: 0pt; position: static; text-align: left; text-decoration: underline; text-indent: 0pt; text-transform: none;&quot;&gt;packetship&lt;/span&gt;:~#&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;
&lt;b&gt;Summary: &lt;/b&gt;&lt;br /&gt;
Generally speaking, by typing &quot;cat /etc/issue&quot; normally you can get your answers. And it&#39;s effective cross every distribution, so it should be your first choice. Once it&#39;s modified by the system administrator and prevent you from getting the answer, then you can try other ways as suggested above.</content><link rel='replies' type='application/atom+xml' href='http://linuxeasygo.blogspot.com/feeds/9185778497412591620/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://linuxeasygo.blogspot.com/2011/11/how-to-check-linux-distribution-version.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/9185778497412591620'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8904977416785454796/posts/default/9185778497412591620'/><link rel='alternate' type='text/html' href='http://linuxeasygo.blogspot.com/2011/11/how-to-check-linux-distribution-version.html' title='How to check Linux distribution version'/><author><name>Jackie Yeh</name><uri>http://www.blogger.com/profile/11918635100118582131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>