Welcome Buddy!

Get Started
LinuxTips Tricks

Linux Tips: Playing with Time (Part 1)

cnwintech playing with time linux 2

Linux provides many facilities related to time management. This article does not discuss all of these facilities, but only discuss three of the most common activities performed by the Linux users. These three activities include, display and set the time, looking for the data at regular intervals, and make notification (how to create a notification will be discussed later).

1. Displaying time

Time information may be one of the most frequently accessed information by users. Clock applet is the easiest way to see the current time and date. This applet typically located in the lower right corner of your desktop or MATE KDE user, or in the upper right to your desktop user environment Cinnamon / GNOME 3/Unity. You can even choose to display the clock widget on the desktop. The shape resembles a regular wall clock installed in home. At the command line, the command to display the current time and date is:

$ date
cnwintech example of time conversion 2

Display the command output will look like the following:

Sat Jul 6 18:23:54 WIT 2013

Date command without option seen relatively simple. Next, let’s try to function even further. You who often working with colleagues who are abroad would want to know the time in that country. If your colleague are in Hong Kong, you can use the date command to find out the local time (in Hong Kong). The syntax is:

 $ TZ='Hongkong' date
 Sat Jul 6 19:32:14 HKT 2013

Note! We precede the command with reference to a variable ie TZ, followed by a time zone that we want. To be more clear, look again other date command to find the time in New York:

 $ TZ='America/New York' date
 Sat Jul 6 11:34:11 America 2013

The question is, how do we know that the area we mentioned recognizable in Linux? It turns out that Linux has a time zone database commonly stored in the directory /usr/share/zoneinfo on most distributions. This directory has a subdirectory that symbolizes the continent and time zone GMT. Here are excerpts:

 Africa/ Asia/ Canada/ Cuba@ EST@ Factory@ GMT0@
 America/ Atlantic/ CET@ EET@ EST5EDT@ GB@
 Antarctica/ Australia/ Chile/ Egypt@ Etc/ GB-Eire
 Arctic/ Brazil/ CST6CDT Eire@ Europe/ GMT@

Note: the filename behind the @ sign indicates the use of symbolic links (symlinks).

1available ads room - 728x90

The trick is to find an area that we want quickly. The command “find” can be used to find it. For example, if we want to find time data of Thailand:

$ find /usr/share/zoneinfo/ -iname "thailand"

It turns out that there is no output (empty). Therefore, try again with its capital, ie Bangkok:

 $ find /usr/share/zoneinfo/ -iname "bangkok"

Success! Based on this information, it can be concluded that we just only use “Asia/Bangkok” (the name of the directory /usr/share/zoneinfo being ignored):

 $ TZ='Asia/Bangkok' date
 Sat Jul 6 18:47:44 ICT 2013

Now we switched to the date. For example, we want to display the date of 6 days ago:

 $ date -d "6 days ago"
 Sun Jun 30 19:00:16 WIT 2013

Exactly right? Or what if you want to calculate the date three weeks from now?

 $ date -d " now + 3 weeks "
 Sat Jul 27 19:02:13 WIT 2013

Another interesting trick that can be used for example to calculate how many days before Christmas. For this we use the macro %j that will show how many day to day requested:

 $ date +%j
 $ date -d "25 December 2013" +%j

Means Christmas is a day 359th, and today (July 9) is the 90th day. Then with the help of shell we do subtraction:

 $ echo $((359-190))

Means Christmas is 169 days away. It is quite reliable, isn’t it?

2. Finding files created at a specific time

Almost all operating systems keep records when the last time a file is read and the last time the file contents changed. For example, the following command will show all the files in our home directory which modified since 60 minutes ago until now:

find ~ -type f -mmin -60

Note the use of the minus sign (-) which means “a maximum to N minutes”. When using the plus sign (+):

find ~ -type f -mmin +60

Means we are looking for modified files contains more than 60 minutes ago. What if we want to find the files that we modified in a certain clock? The first alternative is we calculate how many minutes have passed. For example, today at 14:00 (2 pm). While we search for files created between the hours 8 and 10 am. Means we are looking for files between 6 hours (360 minutes) and 4 hours (240) hours ago. Mathematically, it means that 240 <= T <= 360. The command is:

find ~ -type f -mmin +240 -mmin -360

Consider the sequence of laying -mmin option in the examples above. The order of option are not the same, for example:

find ~ -type f -mmin +360 -mmin -240

would mean “looking for modified files below 4 hours and more than 6 hours ago”, or t<=240 and t>=360. Surely not even will be found in any file.

cnwintech time calculation with date 2

In this way pretty practical if we could accurately calculate, it’s just become difficult if eg we want to find a very specific hours, ie between 8:45 and 9:45 on this morning. Instead we count how many minutes behind, we take advantage the option of -newerXY from the find. This option will search for files that were last modified more recently than the file specified as an argument-newer. Here is an complete example.

 $ touch -d "08:45" ./start
 $ stat ./start
 File: `./start'
 Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 Device: 808h/2056d Inode: 393539 Links: 1
 Access: (0664/-rw-rw-r--) Uid: ( 1000/ mulyadi) Gid: ( 1000/ mulyadi)
 Access: 2013-07-07 08:45:00.000000000 +0700
 Modify: 2013-07-07 08:45:00.000000000 +0700
 Change: 2013-07-07 21:53:16.610412038 +0700
 Birth: -
 $ touch -d "9:45" ./end
 $ stat ./end
 File: `./end'
 Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 Device: 808h/2056d Inode: 397175 Links: 1
 Access: (0664/-rw-rw-r--) Uid: ( 1000/ azam) Gid: ( 1000/ azam)
 Access: 2013-07-07 21:54:04.246648298 +0700
 Modify: 2013-07-07 09:45:00.000000000 +0700
 Change: 2013-07-07 21:53:59.122628423 +0700
 Birth: -

We’ve made two files named “start” and “end”, each with hour modifications that we need. Of course you can change the name of the file with a different name. Then we “feed” two files over to the find command:

$ find ~ -type f -newer ./start -not -newer ./end

The following is a detailed explanation of the above command:

  • -newer option will check the file modification time “start”, in which case it is 8:45 am today. Then it will search for all files that previously modified afterwards.
  • -not option will reverse the meaning of parameter afterwards, so that -not-newer means previously file modification time.
  • The second -newer option will check the file “end”, which was made at 9:45. Because the option -not, meaning all files will be searched before 9:45.
  • All conditions will be combined, so it will look for files between the hours of 8:45 and 9:45

Note: From our observations, the distributions were released in 2010, such as CentOS 6 or Linux Mint 9 has been included newer find version that allows us to mention the time directly via “-newerXY” option:

find ~ -newermt "8:45"

There are additional letters “mt” after-newer, define each of the last modification time and also mentions hour directly. This trick can we extend, for example, we would like to see any file that we edited on February 9, 2013 for a full day:

$ find ~ -type f -newermt "9 Feb 2013 00:00" -not -newermt "10 Feb 2013 00:00"

We use at 12 am on the next date in order to files created at 23:59 still count. If you want to sort them chronologically, you can combine it with xargs and ls as follows:

 $ find ~ -type f -newermt "9 Feb 2013 00:00" -not -newermt "10 Feb 2013 00:00" | xargs
 -r ls -lt | less

the options -lt in Ls will sort accessible files starting from the most recent to the oldest. If you want to reverse this order, use the command “ls-ltr”. We will shows another interesting linux time tricks in the next chapter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also
Subscribe Now
Back to top button

Adblock Detected!

If you enjoy our content, please support our site by disabling your ad blocker or whitelisting us. We use ads to keep our content happy and free.