看板 Knuckles_note
作者 標題 [Linux] 在 shell script 產生想要的日期格式
時間 2013年01月14日 Mon. PM 01:05:26
使用 date +FORMAT
例如
日期
$ date +"%Y%m%d"
20131401
時間
$ date +"%T"
12:59:33
星期幾
$ date +"%u"
1
將日期格式存成字串
$ NOW=$(date +"%Y%m%d")
$ echo $NOW
20131401
寫成 shell script 檔
#!/bin/bash
NOW=$(date +"%Y%m%d")
FILE="backup.$NOW.tar.gz"
tar -zcvf "$FILE" backup.sql
NOW=$(date +"%Y%m%d")
FILE="backup.$NOW.tar.gz"
tar -zcvf "$FILE" backup.sql
所有的日期格式代號:
%%
顯示%符號
%a
星期幾的縮寫 (e.g. Sun)
%A
星期幾 (e.g. Sunday)
%b
月份的縮寫 (e.g., Jan)
%B
月份 (e.g., January)
%c
日期與時間 (e.g., Thu Mar 3 23:05:25 2005)
%C
世紀,%Y的前兩個數字 (e.g., 20)
%d
每月的幾號 (e.g, 01)
%D
date; same as %m/%d/%y
%e
與%d類似,只是小於10時前面會空一格
%F
full date; same as %Y-%m-%d
%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
%h
same as %b
%H
hour (00..23)
%I
hour (01..12)
%j
day of year (001..366)
%k
hour ( 0..23)
%l
hour ( 1..12)
%m
month (01..12)
%M
minute (00..59)
%n
a newline
%N
nanoseconds (000000000..999999999)
%p
locale's equivalent of either AM or PM; blank if not known
%P
like %p, but lower case
%r
locale's 12-hour clock time (e.g., 11:11:04 PM)
%R
24-hour hour and minute; same as %H:%M
%s
seconds since 1970-01-01 00:00:00 UTC
%S
second (00..60)
%t
a tab
%T
time; same as %H:%M:%S
%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)
%x
locale's date representation (e.g., 12/31/99)
%X
locale's time representation (e.g., 23:13:48)
%y
last two digits of year (00..99)
%Y
year
%z
+hhmm numeric timezone (e.g., -0400)
%:z
+hh:mm numeric timezone (e.g., -04:00)
%::z
+hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z
numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z
alphabetic time zone abbreviation (e.g., EDT)
--
※ 作者: Knuckles 時間: 2013-01-14 13:05:26
※ 編輯: Knuckles 時間: 2015-10-19 06:58:35
※ 看板: KnucklesNote 文章推薦值: 1 目前人氣: 0 累積人氣: 25493
1樓 時間: 2013-03-20 18:59:33 (台灣)
推
03-20 18:59 TW
也可以$ tar cvfz backup`date +%Y%m%d`.tgz mysqldump.sql
回列表(←)
分享