Linux Commands Examples

A great documentation place for Linux commands


print or set the system date and time


date [OPTION]... [+FORMAT]
[-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



Convert seconds since the epoch (1970-01-01 UTC) to a date

$ date --date=’@2147483647’

Show the time on the west coast of the US (use tzselect(1) to find TZ)

$ TZ=’America/Los_Angeles’ date

Show the local time for 9AM next Friday on the west coast of the US

$ date --date=’TZ="America/Los_Angeles" 09:00 next Fri’


##What does it do ?

It displays the system's current date, time, time standard and year


Wed Dec 25 13:42:54 GMT 2013
example added by an anonymous user

How to insert the date into vim

I kept experimenting till I figured out that vim was expanding the "%" character. So just escape "\%" and every thing works as I expected.

:r!date "+\%F"

Now I can put dates into files Like I would like to

:r!date "+\%F" -d "-2 day"


Store the output of date and watch command to a file

watch is meant for output to a display. If you simply want to run a command every X seconds then you should just use a delay loop for that.

while true ; do somecommand ; sleep 2 ; done


How to set current time on Linux?

Check your timezone. hwclock may return GMT time, whileas date returns local time, afaik.


How to convert from day of year and year to a date YYYYMMDD?

Can't be done in just Bash, but if you have Perl:

use POSIX;

my ($jday, $year) = (100, 2011);

# Unix time in seconds since Jan 1st 1970
my $time = mktime(0,0,0, $jday, 0, $year-1900);

# same thing as a list that we can use for date/time formatting
my @tm = localtime $time;

my $yyyymmdd = strftime "%Y%m%d", @tm;


Timezone conversion by command line

This example is from

It gives the local time corresponding to 9AM on the west coast of the US, accounting for differing day light savings transitions.

date --date='TZ="America/Los_Angeles" 09:00 next Fri'

Use tzselect to get the TZ. The PST format is ambiguous. IST = Indian Standard Time and Irish Summer Time for example.


How to make an application detect if system time has changed in Linux

I think this article has an answer to your question: Notify userspace about time changes. But please note that the patch mentioned in the article is quite recent, so you have to check your linux kenel vesrion first.

If your kernel does not support userspace notification mechanism, then you can implement the following algorithm (in pseudocode):

time = gettimeofday()

    sleep 1 second
    new_time = gettimeofday()
    if (time_diff(new_time, time) > 2 seconds) then
       alert System time has changed by an external user/process!

    time = new_time
    goto loop

Hope this helps.


How do I get the current date according to an NTP server without setting it locally?

This Perl script should do what you need (assuming you don't need precision to the 10-6 of a second):

#!/usr/bin/perl -w

use strict;
use Math::Round;

## Get current date (epoch)
my $date=time();

## Get the seconds offset, rounding to the nearest second
my $ntp=nearest(0.1,`ntpdate -q $ARGV[0] | gawk '(\$NF~/sec/){print \$(NF-1)}'`); 

## Get the server's time
my $ntp_date=$date+$ntp;

## Convert to human readable and print
print "The time according to server $ARGV[0] is " . localtime($ntp_date) . "\n";

Save the script as and run it with the server as an argument:

perl ./ my.ntp.server


Set relative time using command line

You can change your time zone by doing # dpkg-reconfigure tzdata. I'm not sure how you would change the time by an arbitrary value, though.


How to set-up date for cron weekly backup?

Here's an idea: Every time a backup is run, the backup script records the current date by touching a file. The next time the backup runs, it uses the previous backup's datestamp file as the argument to tar -N. For example, in a shell script, the logic could look like this:

# Begin the latest backup run now.
touch /backups/new-backup-time

tar -N /backups/prev-backup-time -c ... ...

# Save the latest backup time for the next run.
mv /backups/new-backup-time /backups/prev-backup-time

This depends on the fact that the tar -N option can either be a date, OR it can be the pathname of a file whose data modification time specifies the date.


How to change the date of Linux to UTC/GMT +1?

You need to change the timezone.

  • To do it system-wide, symlink /etc/localtime to the apropriate file in /usr/share/zoneinfo. For example:

    ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime

    In CentOS, you might need to also edit /etc/sysconfig/clock.

  • To change the timezone just for the current user, set $TZ instead:

    export TZ="Europe/Paris"

Note that date -u must always return correct UTC time.


how to change the time format in Linux system

cp /usr/share/zoneinfo/Asia/Calcutta /etc/localtime


cp /usr/share/zoneinfo/Asia/Colombo /etc/localtime

Some distribution provide utilities for that like dpkg-reconfigure tzdata, tzselect, etc.


On the command line, 'find' reports back an illegal time value

The problem is that ` marks are used to denote commands who's output should be substituted in your command... so your command is actually three commands:

  1. "echo " (contained in the first set of 1 marks)
  2. "-1308741881 | bc" (contained in the second set of 1 marks)
  3. find . -mtime -OUTPUT FROM COMMAND #1date +%sOUTPUT FROM COMMAND #2

Command #1 outputs nothing, and command #2 results in the "Command not found" error, because -1308741881 is not a valid command, then outputs nothing either.

Then finally the third command runs, with those replacements and looks like this:

find . -mtime -date

And since "-date" isn't a valid time, it complains about that, too, saying "illegal time value"

The underlying problem is that you're trying to use nested `` marks, which the shell interprets as two separate commands.

A better way to express what you want is this (for bash):

find . -mtime -$(echo $(date +%s-1308741881) | bc)s

And for csh:

set now=`date +%s-1308741881`; set date=`echo $now | bc`s; find . -mtime -$date

And possibly for other shells (untested):

NOW=`date +%s-1308741881`; DATE=`echo $NOW | bc`; find . -mtime -${DATE}s

P.S. I don't think this does what you expect... your date command is returning a number of seconds, but -mtime expects a number of days as input. I'm guessing you'll probably want to adjust your date command accordingly.


How to Change Server Date?

Use the following syntax to set new data and time:

date --set="STRING"


Proper date -d "next monday"

Are you sure there isn't a problem with how you are executing the command within the script, maybe missing quotes or something of that nature? I cannot reproduce your problem.

$ date
Tue Jul 31 19:08:50 EDT 2012
$ date -d "next tuesday"
Tue Aug  7 00:00:00 EDT 2012
$ date -d "this tuesday"
Tue Jul 31 00:00:00 EDT 2012
$ date --version
date (GNU coreutils) 8.5


Display the current time in the given FORMAT, or set the system date.
, --date=STRING

display time described by STRING, not ’now’

-f, --file=DATEFILE

like --date once for each line of DATEFILE

-I[TIMESPEC], --iso-8601[=TIMESPEC]

output date/time in ISO 8601 format. TIMESPEC=’date’ for date only (the default), ’hours’, ’minutes’, ’seconds’, or ’ns’ for date and time to the indicated precision.

-r, --reference=FILE

display the last modification time of FILE

-R, --rfc-2822

output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600


output date and time in RFC 3339 format. TIMESPEC=’date’, ’seconds’, or ’ns’ for date and time to the indicated precision. Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00

-s, --set=STRING

set time described by STRING

-u, --utc, --universal

print or set Coordinated Universal Time


display this help and exit


output version information and exit

FORMAT controls the output. Interpreted sequences are:


a literal %


locale’s abbreviated weekday name (e.g., Sun)


locale’s full weekday name (e.g., Sunday)


locale’s abbreviated month name (e.g., Jan)


locale’s full month name (e.g., January)


locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)


century; like %Y, except omit last two digits (e.g., 20)


day of month (e.g., 01)


date; same as %m/%d/%y


day of month, space padded; same as %_d


full date; same as %Y-%m-%d


last two digits of year of ISO week number (see %G)


year of ISO week number (see %V); normally useful only with %V


same as %b


hour (00..23)


hour (01..12)


day of year (001..366)


hour, space padded ( 0..23); same as %_H


hour, space padded ( 1..12); same as %_I


month (01..12)


minute (00..59)


a newline


nanoseconds (000000000..999999999)


locale’s equivalent of either AM or PM; blank if not known


like %p, but lower case


locale’s 12-hour clock time (e.g., 11:11:04 PM)


24-hour hour and minute; same as %H:%M


seconds since 1970-01-01 00:00:00 UTC


second (00..60)


a tab


time; same as %H:%M:%S


day of week (1..7); 1 is Monday


week number of year, with Sunday as first day of week (00..53)


ISO week number, with Monday as first day of week (01..53)


day of week (0..6); 0 is Sunday


week number of year, with Monday as first day of week (00..53)


locale’s date representation (e.g., 12/31/99)


locale’s time representation (e.g., 23:13:48)


last two digits of year (00..99)




+hhmm numeric time zone (e.g., -0400)


+hh:mm numeric time zone (e.g., -04:00)


+hh:mm:ss numeric time zone (e.g., -04:00:00)


numeric time zone with : to necessary precision (e.g., -04, +05:30)


alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes. The following optional flags may follow ’%’:


(hyphen) do not pad the field


(underscore) pad with spaces


(zero) pad with zeros


use upper case if possible


use opposite case if possible

After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale’s alternate representations if available, or O to use the locale’s alternate numeric symbols if available.


Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

date string

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.

reporting bugs

Report date bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <>
General help using GNU software: <>
Report date translation bugs to <>

see also

The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly installed at your site, the command

info coreutils 'date invocation'

should give you access to the complete manual.


Written by David MacKenzie.

How can this site be more helpful to YOU ?

give  feedback