shell中date的一些用法

cat data.sh

#!/bin/bash

echo "Today is `date +%Y%m%d`!"


echo "Yesterday is `date --date='1 day ago' +%Y%m%d`!"
echo "Yesterday is `date --date='1 days ago' +%Y%m%d`!"
echo "Yesterday is `date --date='-1day' +%Y%m%d`!"


echo "Tomorrow is `date --date='+1 day' +%Y%m%d`!"


echo "Today is `date +%Y-%m-15`!"


echo "Today is `date --date='-1 month'`"
echo   `date --date='-1 month' +'Last month was %B?'`

echo "`date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!' `"

注意:$(date +%Y-%m-15) -1 month 这个是个变量,所以必须用(“”)括起来,尽管最外面有(“”),但是还是不能够换成(‘’)

==============

注意:

 %B   locale's full month name (e.g., January) 【显示月的名字】

==============

[oracle@SOR_SYS~]sh date.sh

Today is 20110815!
Yesterday is 20110814!
Yesterday is 20110814!
Yesterday is 20110814!
Tomorrow is 20110816!
Today is 2011-08-15!
Today is Fri Jul 15 22:41:29 CST 2011
Last month was July?

Last month was July!

ps:

今天又发现了一个和--date用法相同的参数,也就是-d

eg:date -d '1 month ago'

Sun Sep 11 16:32:46 CST 2011

 date -d yesterday +%Y%m%d
20111010

我们来个实际点的例子,比如说压缩前一天的所有日志:

gzip *$(date -d '1 days ago' +%Y-%m-%d)*

或者

gzip *$(date -d '1 days ago' +%F)*

需要说明的是gzip直接将原来的文件压缩,解压即gunzip

另外再看一个date的参数:

[oracle@Test230 zy]$ date +%c
Fri 06 Apr 2012 03:40:13 PM CST

这个是按照上下午算的~

你可能感兴趣的:(Python)