Linux date and touch

来源:互联网 发布:js click target 编辑:程序博客网 时间:2024/04/30 10:54

功能说明:显示或设置系统时间与日期。
语  法:date [-d <字符串>][-u][+%H%I%K%l%M%P%r%s%S%T%X%Z%a%A%b%B%c%d%D%j%m%U%w%x%y%Y%n%t]
              或 date [-s <字符串>][-u] [MMDDhhmmCCYYss] 或 date [--help][--version]
补充说明:第一种语法可用来显示系统日期或时间,以%为开头的参数为格式参数,可指定日期或时间的显示格式。

第二种语法可用来设置系统日期与时间。只有管理员才有设置日期与时间的权限。若不加任何参数,data会显示目前的日期与时间。

参  数:
%H  小时(以00-23来表示)。
%I  小时(以01-12来表示)。
%K  小时(以0-23来表示)。
%l  小时(以0-12来表示)。
%M  分钟(以00-59来表示)。
%P  AM或PM。
%r  时间(含时分秒,小时以12小时AM/PM来表示)。
%s  总秒数。起算时间为1970-01-01 00:00:00 UTC。
%S  秒(以本地的惯用法来表示)。
%T  时间(含时分秒,小时以24小时制来表示)。
%X  时间(以本地的惯用法来表示)。
%Z  市区。
%a  星期的缩写。
%A  星期的完整名称。
%b  月份英文名的缩写。
%B  月份的完整英文名称。
%c  日期与时间。只输入date指令也会显示同样的结果。
%d  日期(以01-31来表示)。
%D  日期(含年月日)。
%j  该年中的第几天。
%m  月份(以01-12来表示)。
%U  该年中的周数。
%w  该周的天数,0代表周日,1代表周一,异词类推。
%x  日期(以本地的惯用法来表示)。
%y  年份(以00-99来表示)。
%Y  年份(以四位数来表示)。
%n  在显示时,插入新的一行。
%t  在显示时,插入tab。
MM  月份(必要)。
DD  日期(必要)。
hh  小时(必要)。
mm  分钟(必要)。
CC  年份的前两位数(选择性)。
YY  年份的後两位数(选择性)。
ss  秒(选择性)。
-d<字符串>  显示字符串所指的日期与时间。字符串前後必须加上双引号。
-s<字符串>  根据字符串来设置日期与时间。字符串前後必须加上双引号。
-u  显示GMT。
--help  在线帮助。
--version  显示版本信息。

 

 

ABOUT DATE

Tells you the date and time in UNIX.

SYNTAX

date [-a] [-u] [-s datestr]

-a Slowly adjust the time by sss.fff seconds (fff represents fractions of a second). This adjustment can be positive or negative. The system's clock will be sped up or slowed down until it has drifted by the number of seconds specified. Only the super-user may adjust the time. -u Display (or set) the date in Greenwich Mean Time (GMT-universal time), bypassing the normal conversion to (or from) local time. -s datestr Sets the time and date to the value specfied in the datestr. The datestr may contain the month names, timezones, 'am', 'pm', etc. See examples for an example of how the date and time can be set.


EXAMPLES

date - Would list the date and time of the server. Below is an example of the output.

Thu Feb 8 16:47:32 MST 2001

date -s "11/20/2003 12:48:00" - Set the date to the date and time shown.

date '+DATE: %m/%d/%y%nTIME:%H:%M:%S' - Would list the time and date in the below format:

DATE: 02/08/01 TIME:16:44:55

ADDITIONAL INFORMATION

If you are looking for additional information about the release date of UNIX or a UNIX variant please see our main UNIX page or our history section.

 

 

名称:date
date //显示当前日期
date -s //设置当前时间,据说只有root权限才能设置,其他只能查看。
date -s 20061010 //设置成20061010,这样会把具体时间设置成空00:00:00
date -s 12:23:23 //设置具体时间,不会对日期做更改
date -s “12:12:23 2006-10-10″ //这样可以设置全部时间
date -s “12:12:23 20061010″ //这样可以设置全部时间
或者
date -s “2006-10-10 12:12:23″ //这样可以设置全部时间
date -s “20061010 12:12:23″ //这样可以设置全部时间
date +"%Y%m%d_%H%M"
这个格式里面的CST是中国标准时间(China Standard Time)的意思。
关于时区,在/etc/sysconfig/clock里有记录诸如 ZONE=”Asia/Shanghai”
此类的时区信息。时区很多,我们也不太会去非洲美洲大洋洲,
一般没必要记住。知道以下几个可能就差不多了:

CST:中国标准时间(China Standard Time),这个解释可能是针对RedHat Linux。

UTC:协调世界时,又称世界标准时间,简称UTC,
从英文国际时间/法文协调时间”Universal Time/Temps Cordonné”而来。
中国大陆、香港、澳门、台湾、蒙古国、新加坡、马来西亚、菲律宾、澳洲西部的时间与
UTC的时差均为+8,也就是UTC+8。

GMT:格林尼治标准时间(旧译格林威治平均时间或格林威治标准时间;英语:Greenwich Mean Time,GMT)
是指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。
 
名称:touch
使用权限:所有使用者
使用方式:
touch [-acfm]
[-r reference-file] [--file=reference-file]
[-t MMDDhhmm[[CC]YY][.ss]]
[-d time] [--date=time] [--time={atime,access,use,mtime,modify}]
[--no-create] [--help] [--version]
file1 [file2 ...]
说明:
touch 指令改变档案的时间记录。 ls -l 可以显示档案的时间记录。
参数:
a 改变档案的读取时间记录。
m 改变档案的修改时间记录。
c 假如目的档案不存在,不会建立新的档案。与 --no-create 的效果一样。
f 不使用,是为了与其他 unix 系统的相容性而保留。
r 使用参考档的时间记录,与 --file 的效果一样。
d 设定时间与日期,可以使用各种不同的格式。
t 设定档案的时间记录,格式与 date 指令相同。
--no-create 不会建立新档案。
--help 列出指令格式。
--version 列出版本讯息。
 
范例:
最简单的使用方式,将档案的时候记录改为现在的时间。若档案不存在,系统会建立一个新的档案。
touch file
touch file1 file2
 
将 file 的时间记录改为 5 月 6 日 18 点 3 分,公元两千年。
时间的格式可以参考 date 指令,至少需输入 MMDDHHmm ,就是月日时与分。
touch -c -t 05061803 file
touch -c -t 050618032000 file
将 file 的时间记录改变成与 referencefile 一样。
touch -r referencefile file
将 file 的时间记录改成 5 月 6 日 18 点 3 分,公元两千年。
时间可以使用 am, pm 或是 24 小时的格式,日期可以使用其他格式如 6 May 2000。
====================================================================================
touch -d 和 date -s 的用法相同。
如果没有指定日期,默认为系统日期
touch -d 18:03 file
touch -d "18:03" file
touch -d "6:03pm" file
 
如果没有指定时间,默认为 00:00:00
touch -d 20000506 file
touch -d "05/06/2000" file
touch -d "20000506" file
touch -d "6:03pm 05/06/2000" file
touch -d "20000506 18:03" file
touch -d "20000506 18:03:00" file