如果想要快速的学会使用shell命令,那么学会使用man是必不可少的,接下来就来学习一下怎么使用man来解决我们的问题。
我们来通过例子,进行学习:
<1>先将date敲进去试试,它是这样显示的。
$ date
Thu Sep 29 02:38:02 CST 2016
<2>我们想要打印出”2016年9月29日“。那么我们就需要man 我们
分析: 它有 1) 2016年 2)9月 3)29日 三部分组成
$ man date
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
从摘要中我们知道 date的格式是 : date 选项 +格式 ps:格式前面必选加"+"
(ps:[内容] 代表可选内容 <内容> 代表必内容 内容...表示内容个数任意)
我们是要选择适当的格式输出,我们只需要+FORMAT 就可以。
在man date 中FORMAT 部分都是这样写的:
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale’s abbreviated weekday name (e.g., Sun)
%A locale’s full weekday name (e.g., Sunday)
%b locale’s abbreviated month name (e.g., Jan)
可知 date +%% 是这样的格式
$ date +%%
%
先打印出年我们可以这样查找 man date | grep year
$ man date | grep year
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%j day of year (001..366)
%U week number of year, with Sunday as first day of week (00..53)
%W week number of year, with Monday as first day of week (00..53)
%y last two digits of year (00..99)
%Y year
从这几句中我们可以快速找到 %Y 是我没需要的 同样的方式如下:
$ man date | grep month
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
%d day of month (e.g, 01)
%e day of month, space padded; same as %_d
%m month (01..12)
%m是我们需要的
$ man date | grep day
%a locale’s abbreviated weekday name (e.g., Sun)
%A locale’s full weekday name (e.g., Sunday)
%d day of month (e.g, 01)
%e day of month, space padded; same as %_d
%j day of year (001..366)
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
even "next Thursday". A date string may contain items indicating cal-
endar date, time of day, time zone, day of week, relative time, rela-
day. The date string format is more complex than is easily documented
%d是我们需要的
最终 date +%Y年%m月%d日 就是我们需要的命令
<3>在man date的摘要中我们看到这么一行
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
date -u 是用来修改时间的
那么修改的格式是什么,后面有这句话 [MMDDhhmm[[CC]YY][.ss]]
使用这个格式就可以修改时间
我们修改成2970-01-01 00:00:00 ps:硬件上的时间没有改,电脑重启后,时间就会恢复
# date 010100002070.00
Wed Jan 1 00:00:00 CST 2070
# date
Wed Jan 1 00:00:03 CST 2070
如果有问题可以讨论进群讨论 QQ:459996944