{"id":1436,"date":"2021-12-16T00:00:13","date_gmt":"2021-12-15T18:30:13","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1436"},"modified":"2022-11-12T02:12:32","modified_gmt":"2022-11-11T20:42:32","slug":"date-command-in-linux-unix-with-practical-examples","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/date-command-in-linux-unix-with-practical-examples\/","title":{"rendered":"Date Command in Linux\/Unix with Practical Examples"},"content":{"rendered":"\n
The date command in Linux\/Unix, although seemingly simple, has many applications – creating unique names to use with logfiles and screenshots, energy-efficient alarm clock, finding times in different time zones, finding date 1 year before today, converting Unix Timestamps to DateTime format or any other formats, finding the last modified time of a file, and many more. In this article, I will talk about all features of the date command using such applications. <\/p>\n\n\n\n
One problem I very often face while booting back and forth between Windows and Linux in my dual-boot PC is that the current time gets changed. While fixing this up, I use the date<\/strong> command very often.<\/p>\n\n\n\n Syntax<\/strong>:<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Combining the above command with the watch<\/strong> command you can even create a digital clock<\/strong> that will print the date and time at every second \ud83d\ude01:<\/p>\n\n\n\n You can also get the Coordinated Universal Time (UTC) using -u or –utc or –universal flag:<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n In the above examples, as you are seeing, the outputs are in a special order – first weekday (Mon), then the year (Dec), then the day of the month (13), and so on. We can change this order of output and that will be described in the next heading.<\/p>\n\n\n\n To change the output format, we use special sequences. The following sequences are sufficient to fulfill most of your needs:<\/p>\n\n\n\n There are more such sequences. To learn about them, see the man page.<\/p>\n\n\n\n Syntax:<\/strong><\/p>\n\n\n\n Output<\/strong>:<\/p>\n\n\n\n My screenshot command is based on the above example:<\/p>\n\n\n\n Here import<\/strong> The beauty of the above date based command:<\/strong><\/p>\n\n\n\n To verify the beauty, you can just execute the given screenshot command couple of times in your terminal. Here is how the output files will look like:<\/p>\n\n\n\n Similarly, Linux system administrators can also create log files with unique names.<\/p>\n\n\n\n Another example<\/strong> is what I use as my clock in my i3-tiling window manager:<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Notice how in the above example special characters like colon ( All of the above date commands were related to ‘current’ time. What if we want to manipulate times other than the current one?<\/p>\n\n\n\n~$ date<\/code><\/pre>\n\n\n\n
Tue Dec 14 02:54:05 AM IST 2021<\/code><\/pre>\n\n\n\n
~$ watch --interval=1 date<\/code><\/pre>\n\n\n\n
~$ date --utc<\/code><\/pre>\n\n\n\n
Mon Dec 13 09:25:50 PM UTC 2021<\/code><\/pre>\n\n\n\n
2. How to Get Current Time and Date in Given Formats Using Date Command in Linux<\/h2>\n\n\n\n
Format Sequences<\/th> Interpretation<\/th><\/tr><\/thead> %a<\/td> abbreviated weekday name (e.g., Sun)<\/td><\/tr> %A<\/td> full weekday name (e.g., Sunday)<\/td><\/tr> %b<\/td> abbreviated month name (e.g., Jan)<\/td><\/tr> %B<\/td> full month name (e.g., January)<\/td><\/tr> %d<\/td> day of the month (e.g., 01)<\/td><\/tr> %H<\/td> hour (e.g., 00..23)<\/td><\/tr> %I<\/td> hour (e.g., 01..12)<\/td><\/tr> %m<\/td> month (01..12)<\/td><\/tr> %M<\/td> minute (00..59)<\/td><\/tr> %Y<\/td> year<\/td><\/tr> %N<\/strong><\/td> nanoseconds (000000000..999999999)<\/strong><\/td><\/tr> %p<\/td> AM or PM<\/td><\/tr> %P<\/td> am or pm<\/td><\/tr> %s<\/strong><\/td> seconds since the Epoch (1970-01-01 00:00 UTC)<\/strong><\/td><\/tr> %S<\/td> second (00..60)<\/td><\/tr> %u<\/td> day of the week (1..7); 1 is Monday<\/td><\/tr><\/tbody><\/table> man date<\/code><\/figcaption><\/figure>\n\n\n\n
~$ date +FORMAT<\/code><\/pre>\n\n\n\n
2.1. Example 1. Creating Unique Filename<\/h3>\n\n\n\n
~$ date '+%Y-%m-%d_%T.%N'<\/code><\/pre>\n\n\n\n
2021-12-20_23:11:42.983703647<\/code><\/pre>\n\n\n\n
~$ import \"$(date '+%Y-%m-%d_%T.%N').png\"<\/code><\/pre>\n\n\n\n
<\/strong><\/code>is a screenshot tool included in the package imagemagick<\/strong> and
\"$(date '+%Y-%m-%d_%T.%N').png\"<\/code> is the name of the image where the screenshot will be saved.<\/p>\n\n\n\n
2021-12-20_23:13:10.639718047.png\n2021-12-20_23:13:13.580715441.png\n2021-12-20_23:13:18.507580139.png\n2021-12-20_23:13:20.517476383.png\n2021-12-20_23:13:23.277434727.png\n2021-12-20_23:13:36.722378523.png\n2021-12-20_23:13:38.646510476.png\n2021-12-20_23:13:41.255368267.png<\/code><\/pre>\n\n\n\n
2.2. Example 2: Creating Clock for Tiling Window Managers<\/h3>\n\n\n\n
~$ date '+%d\/%m %I:%M %u'<\/code><\/pre>\n\n\n\n
14\/12 03:27 2<\/code><\/pre>\n\n\n\n
:<\/code>), slash (
\/<\/code>), and spaces are printed without any changes.<\/p>\n\n\n\n
3. How to Print a Given Time Instead of the Current Time Using Date Command in Linux<\/h2>\n\n\n\n