shell 中date 的用法

2021-07-05 20:39:54 字數 3702 閱讀 9448

1. 獲取離今天的上週  上月  下週 下月的日期

[html]view plain

copy

date -d "-1 week" "+%y%m%d %a"  

date -d "1 day" "+%y%m%d %a"  

date -d "2 day" "+%y%m%d %a"  

date -d "-1 month" "+%y%m%d %a"  

date -d "1 month" "+%y%m%d %a"  

2. date '+%y-%m-%d %h:%m' -d '-10 minute' 

之前的第10分鐘

3. date的用法:

2. atime  mtime ctime  區別

3./usr/bin/stat -c %y  filename

[zengtai.csm@tweb1 conf]$ /usr/bin/stat -c %y /home/a/search/nfs/tanx/adzoneinfo.tdbm.done

1342054262

4. shell 中的date  常用方法

(2). date 基本用法

[html]view plain

copy

$ a=`date +"%y-%m-%d %h:%m:%s"`  

echo $a  

2012-07-30 09:31:41  

5. date  unix   時間戳  和 字串的轉換

6. 昨天的元月日表示:

[html]view plain

copy

#!/bin/bash  

src_err_log

=/home/a/share/nginx/logs/error.log  

src_acc_log

=/home/a/share/nginx/logs/proxy-reload.log  

dst_log_root

="/home/a/share/nginx/logs/cronolog"

year_mon

=$(date -d "yesterday" +"%y")/$(date -d "yesterday" +"%m")  

year_mon_day

=$(date -d "yesterday" +"%y-%m-%d")  

dst_err_log

=$/$/$-error_log  

dst_acc_log

=$/$/$-proxy-reload_log  

mkdir -p $/$  

chown ads $ -r  

mv $ $  

mv $ $  

kill -usr1 `cat /home/a/share/nginx/logs/nginx-proxy.pid`  

gzip -c $ >

$.gz  

rm $  

gzip -c $ >

$.gz  

rm $  

chown ads $.gz  

chown ads $.gz  

[html]view plain

copy

echo $(date -d "1 minute ago" +"%y-%m-%d %h:%m:%s")   

2012-11-02 21:03:44  

echo $(date -d "1 hours ago" +"%y-%m-%d %h:%m:%s")         

2012-11-02 20:05:16  

echo $(date -d "1 days ago" +"%y-%m-%d %h:%m:%s")       

2012-11-01 21:05:22  

echo $(date -d "1 week ago" +"%y-%m-%d %h:%m:%s")      

2012-10-26 21:05:27  

echo $(date -d "1 months ago" +"%y-%m-%d %h:%m:%s")        

2012-10-02 21:05:39  

1分鐘  1小時  1天  1周  1月 1年

[html]view plain

copy

echo $(date -d "1 minute " +"%y-%m-%d %h:%m")  

2012-11-02 21:09  

echo $(date -d "1 hours " +"%y-%m-%d %h:%m")           

2012-11-02 22:08  

echo $(date -d "1 days " +"%y-%m-%d %h:%m")       

2012-11-03 21:08  

echo $(date -d "1 month " +"%y-%m-%d %h:%m")       

2012-12-02 21:08  

echo $(date -d "1 year " +"%y-%m-%d %h:%m")       

2013-11-02 21:08  

重用的簡單格式:

[html]view plain

copy

echo $(date +%s)  

1351861770  

echo $(date +%f)  

2012-11-02  

echo $(date +%d)  

11/02/12  

8. date 各種時間之間的轉化:

[html]view plain

copy

date -d "@1354021141 " +"%y-%m-%d %h:%m"  

2012-11-27 20:59  

date -d "2012-11-27 20:59:00" +%s  

<

prename

="code"

class

="html"

>

1354021140

pre>

<

br>

date -d "2012-11-27 20:59:00" +%f<

br>

2012-11-27<

br>

<

br>

<

pre>

pre>

<

br>

<

br>

<

p>

<

br>

p>

<

br>

<

p>

p>

<

p>

<

br>

p>

<

p>

p>

<

p>

p>

<

divstyle

="padding-top:20px"

>

<

pstyle

="font-size:12px;"

>

p>

div>

Shell中date的用法

man date可以看到date的help檔案 date 獲取當前時間 date d 1week y m d 獲取上週日期 day,month,year,hour date date 24 hour y m d 同上 date now date s shell指令碼裡面賦給變數值 輸出 符號 a 當...

shell 中 date的用法

1.獲取離今天的上週 上月 下週 下月的日期 html view plain copy date d 1 week y m d a date d 1 day y m d a date d 2 day y m d a date d 1 month y m d a date d 1 month y m ...

SHELL下date命令的用法

date命令如何獲得上星期的日期?date d 1 week y m d a 舉一反三 date命令獲得前天,昨天,明天,後天,上個月,下個月的日期 date d 2 day y m d a 或 date date 2 days ago y m d a date d 1 day y m d a 或 ...