Showing posts with label Date Command. Show all posts
Showing posts with label Date Command. Show all posts

Thursday, 14 October 2021

date command in Linux with examples

date command, Linux Tutorial and Material, Linux Certifications, Linux Study Materials, LPI Career, LPI Preparation

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.

Syntax:


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

Options with Examples


1: date (no option) : With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Read More: LPI Certifications

Command: 
$date
Output: 
Tue Oct 10 22:55:01 PDT 2017

Note : Here unix system is configured in pacific daylight time.

2: -u Option: Displays the time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal Time )time zone.

Command: 
$date -u
Output :
Wed Oct 11 06:11:31 UTC 2017

3: –date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string.

Syntax:

$date --date=" string "

Command: 
$date --date="2/02/2010"
$date --date="Feb 2 2010"
Output: 
Tue Feb  2 00:00:00 PST 2010
Tue Feb  2 00:00:00 PST 2010

4: Using –date option for displaying past dates:

◈ Date and time of 2 years ago.

Command: 
$date --date="2 year ago"
Output: 
Sat Oct 10 23:42:15 PDT 2015

◈ Date and time of 5 seconds ago.

Command: 
$date --date="5 sec ago"
Output: 
Tue Oct 10 23:45:02 PDT 2017

◈ Date and time of previous day.

Command:
$date --date="yesterday"
Output: 
Mon Oct  9 23:48:00 PDT 2017

◈ Date and time of 2 months ago.

Command: 
$date --date="2 month ago"
Output: 
Thu Aug 10 23:54:51 PDT 2017

◈ Date and time of 10 days ago.

Command: 
$date --date="10 day ago"
Output: 
Sat Sep 30 23:56:55 PDT 2017

5: Using –date option for displaying future date:

◈ Date and time of upcoming particular week day.

Command: 
$date --date="next tue"
Output: 
Tue Oct 17 00:00:00 PDT 2017

◈ Date and time after two days.

Command: 
$date --date="2 day"
Output: 
Fri Oct 13 00:05:52 PDT 2017

◈ Date and time of next day.

Command: 
$date --date="tomorrow"
Output: 
Thu Oct 12 00:08:47 PDT 2017

◈ Date and time after 1 year on the current day.

Command: 
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2018

6: -s or –set Option: To set the system date and time -s or –set option is used.

Syntax:

$date --set="date to be set"

Command:
$date
Output: 
Wed Oct 11 15:23:26 PDT 2017
Command: 
$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date
Output: 
Tue Nov 13 15:23:34 PDT 2018

7: –file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line.

Syntax:

$date --file=file.txt

$cat >> datefile
Sep 23 2018
Nov 03 2019
Command:
$date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov  3 00:00:00 PDT 2019

8: -r Option: This is used to display the last modified timestamp of a datefile .

Syntax:

$date -r file.txt

We can modify the timestamp of a datefile by using touch command.

date command, Linux Tutorial and Material, Linux Certifications, Linux Study Materials, LPI Career, LPI Preparation

$touch datefile

$date
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile

9: List of Format specifiers used with date command:

%D: Display date as mm/dd/yy.     
%d: Display the day of the month (01 to 31).     
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

Syntax:

$date +%[format-option]

Examples:

Command: 
$date "+%D"
Output: 
10/11/17
Command: 
$date "+%D %T"
Output: 
10/11/17 16:13:27
Command: 
$date "+%Y-%m-%d"
Output: 
2017-10-11
Command:
$date "+%Y/%m/%d"
Output: 
2017/10/11
Command: 
$date "+%A %B %d %T %y"
Output: 
Thursday October 07:54:29 17

Source: geeksforgeeks.org

Tuesday, 19 February 2019

date command in Linux with examples

date command, Linux Tutorial and Material, Linux Certifications, Linux Study Materials

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.

Syntax:


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

Options with Examples


1: date (no option) : With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Command: 
$date
Output: 
Tue Oct 10 22:55:01 PDT 2017
Note : Here unix system is configured in pacific daylight time.

2: -u Option: Displays the time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal Time )time zone.

Command: 
$date -u
Output :
Wed Oct 11 06:11:31 UTC 2017

3: –date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string.

Syntax:

$date --date=" string "

Command: 
$date --date="2/02/2010"
$date --date="Feb 2 2010"
Output: 
Tue Feb  2 00:00:00 PST 2010
Tue Feb  2 00:00:00 PST 2010

4: Using –date option for displaying past dates:

◈ Date and time of 2 years ago.

Command: 
$date --date="2 year ago"
Output: 
Sat Oct 10 23:42:15 PDT 2015

◈ Date and time of 5 seconds ago.

Command: 
$date --date="5 sec ago"
Output: 
Tue Oct 10 23:45:02 PDT 2017

◈ Date and time of previous day.

Command:
$date --date="yesterday"
Output: 
Mon Oct  9 23:48:00 PDT 2017

◈ Date and time of 2 months ago.

Command: 
$date --date="2 month ago"
Output: 
Thu Aug 10 23:54:51 PDT 2017

◈ Date and time of 10 days ago.

Command: 
$date --date="10 day ago"
Output: 
Sat Sep 30 23:56:55 PDT 2017

5: Using –date option for displaying future date:

◈ Date and time of upcoming particular week day.

Command: 
$date --date="next tue"
Output: 
Tue Oct 17 00:00:00 PDT 2017

◈ Date and time after two days.

Command: 
$date --date="2 day"
Output: 
Fri Oct 13 00:05:52 PDT 2017

◈ Date and time of next day.

Command: 
$date --date="tomorrow"
Output: 
Thu Oct 12 00:08:47 PDT 2017

◈ Date and time after 1 year on the current day.

Command: 
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2018

6: -s or –set Option: To set the system date and time -s or –set option is used.

Syntax:

$date --set="date to be set"

Command:
$date
Output: 
Wed Oct 11 15:23:26 PDT 2017
Command: 
$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date
Output: 
Tue Nov 13 15:23:34 PDT 2018

7: –file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line.

Syntax:

$date --file=file.txt

$cat >> datefile
 Sep 23 2018
 Nov 03 2019
Command:
$date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov  3 00:00:00 PDT 2019

8: -r Option: This is used to display the last modified timestamp of a datefile .

Syntax:

$date -r file.txt

We can modify the timestamp of a datefile by using touch command.

$touch datefile

$date
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile

9: List of Format specifiers used with date command:

%D: Display date as mm/dd/yy.     
%d: Display the day of the month (01 to 31).     
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

Syntax:

$date +%[format-option]

Examples:

Command: 
$date "+%D"
Output: 
10/11/17
Command: 
$date "+%D %T"
Output: 
10/11/17 16:13:27
Command: 
$date "+%Y-%m-%d"
Output: 
2017-10-11
Command:
$date "+%Y/%m/%d"
Output: 
2017/10/11
Command: 
$date "+%A %B %d %T %y"
Output: 
Thursday October 07:54:29 17

Wednesday, 27 June 2018

Date Command in Unix and Linux Examples

Date Command, Unix Command, Linux Command, LPI Study Materials, LPI Learning

Date command is used to print the date and time in unix. By default the date command displays the date in the time zone that the unix operating system is configured.

Now let see the date command usage in unix

Date Command Examples:


1. Write a unix/linux date command to print the date on the terminal?


>date
Mon Jan 23 01:37:51 PST 2012

This is the default format in which the date command print the date and time. Here the unix server is configured in pacific standard time.

2. Write a unix/linux date command to print the date in GMT/UTC time zone?


>date -u
Mon Jan 23 09:40:21 UTC 2012

The -u option to the date command tells it to display the time in Greenwich Mean Time.

3. Write a unix/linux date command to sett the date in unix?


You can change the date and time by using the -s option to the date command.

>date -s "01/01/2000 12:12:12"

4. Write a unix/linux date command to display only the date part and ignore the time part?


>date '+%m-%d-%Y'
01-23-2012

You can format the output of date command by using the %. Here %m for month, %d for day and %Y for year.

5. Write a unix/linux date command to display only the time part and ignore the date part?


>date '+%H-%M-%S'
01-48-45

Here %H is for hours in 24 hour format, %M is for minutes and %S for seconds

6. Write a unix/linux date command to format both the date and time part.


>date '+%m-%d-%Y %H-%M-%S'
01-23-2012 01-49-59

7. Write a unix/linux date command to find the number of seconds from unix epoch.


>date '+%s'
1327312228

Unix epoch is the date on January 1st, 1970. The %s option is used to find the number of seconds between the current date and unix epoch.