スポンサーリンク
突然ですが、Linuxで一番親切なコマンドは何だと思いますか?私は断然 dateコマンド だと思います。
dateコマンドは様々な「かゆいところに手が届く」オプションが用意された、とても素敵なコマンドなのです。
今回は、そんなdateコマンドの魅力を余すこと無く伝えていきたいと思います。
dateコマンドは時刻を表示するコマンドです。
日本語環境(LANG=ja_JP.UTF-8 など)であれば、以下のような出力結果になると思います。
$ date
2018年 4月 21日 土曜日 11:02:58 JST
はい。見事に現在時刻を表示してくれました。非常に無駄がないですね。
ということで、次から色々なオプションを見ていきましょう。
上記の通り、デフォルトでは出力結果が日本語表記になってしまいます。これはこれで嬉しいときもありますが、ツールの連携等を考えると全角文字やスペースは不要だったりもします。
ということで、まずはスペースなしの表記です。
コマンド
date "+%Y%m%d%H%M"
実行結果
201804211102
シンプルな表示結果になりました。
このように、date +"フォーマット"
と言うコマンドでフォーマットを指定して表示させることができます。
フォーマットの内容でよく使うのは以下のようなものです。
フォーマット | 意味 | 表示例 |
---|---|---|
%Y | 年を4桁で表示 | 2018 |
%m | 月を2桁で表示 | 04 |
%d | 日を2桁で表示 | 21 |
%H | 時刻を2桁・24H形式で表示 | 11 |
%M | 分を2桁で表示 | 02 |
%S | 秒を2桁で表示 | 58 |
先程の例はシンプルですが、少し読みづらいかもしれません。ということで、次は半角文字で区切りを入れます。
date "+%Y/%m/%d %H:%M:%S"
2018/04/21 11:02:58
どうでしょうか。少し見やすくなりました。
これでも見やすくなったと思いますが、人間の読みやすさで言うと最初の表示(オプションなし)の方が読みやすいかと思います。
しかしオプション無しの場合、言語設定により表示が異なってしまうため、シェルスクリプト等で使う場合には工夫が必要です。
英語で表示する場合は
LANG="C" date
Mon Apr 21 11:02:58 JST 2018
日本語で表示する場合は
LANG="ja_JP.UTF-8" date
表示は
2018年 4月 21日 土曜日 11:02:58 JST
このようにコマンドの前にLANG
で環境変数を指定すれば、このコマンドにだけ有効な環境変数を設定できます。
次はもう少し細かい使い方です。例えば月の表示などは1桁の場合と2桁の場合がありますが、この表示形式も変更できます。
デフォルトでは1桁の場合04
のように0で埋めて(パディングして)2桁に合わせて表示してくれますが、以下のようにすればそれをやめられます。
コマンド
date "+%Y/%-m/%d"
結果
2018/4/21
上記の通り、%-m
のように%と表示内容の間に-
を入れれば0埋めをやめてくれます。
先の例では0埋めをやめる方法を書きましたが、この場合だと文字の位置がずれる場合があります。(例えば5文字目が月表示の場合と5,6文字目が月表示の場合など)
基本的にあまり問題はないですが、シェルスクリプトにするときに問題になる場合があります。
でもdateコマンドは親切なのでちゃんとそういう場合にも対応できます。_
(アンダーバー)を付けることで0の代わりにスペースで間を埋めてくれます。
コマンド
date "+%Y/%_m/%d"
結果
2018/ 4/21
このように表示形式を指定することができます。
これまで書いてきた例は基本的に現在時刻を表示していました。
しかしスクリプト等で使う場合には現在時刻以外の時刻を指定したい場合があります。そんな時、dateコマンドはその親切さを十分に発揮してきます。
例えば2000年1月1日を指定したい場合は
コマンド
date -d "2000-1-1"
結果
2000年 1月 1日 土曜日 00:00:00 JST
また、時刻まで指定したい場合は
date -d "2000-1-1 9:30"
結果
2000年 1月 1日 土曜日 09:30:00 JST
となります。
上記のように、-d オプション
を使うことで日付や時刻を指定することができます。
ちなみに-dオプションで指定する時刻の形式はある程度ゆらぎを許容してくれます(区切り文字を-
にしたり/
にしても大丈夫)。
親切ですね~
先程の例では○年×月△日
のように絶対時刻で指定しました。しかしdateコマンドの便利さはここからです。以下の例を見てください。
コマンド
date -d "1 day ago"
結果
2018年 4月 20日 金曜日 11:02:58 JST
上記のように1 day ago
と指定すると、1日前の日付・時刻を表示してくれます。
この表記ではいくつかの指定が可能です。
週単位
# date -d "1 week ago"
2018年 4月 14日 土曜日 11:02:58 JST
月単位
# date -d "2 month ago"
2018年 2月 21日 水曜日 08:53:08 JST
非常に親切です。これの何が親切かというと、今月が何日まであるか?とかうるう年か?などを考慮しなくてもよいこと です。
シェルスクリプトを書くときにこれはとても助かります。
また、ここまでは過去の日付を表示していましたが、未来の日付も可能です。
コマンド
date -d "1 day"
結果
2018年 4月 21日 土曜日 11:02:58 JST
このように、ago を付けなければ未来の日付になります。
先程の例と似てはいるんですが、数値で(1ヶ月や2週間など)指定しなくても、next
やlast
を指定すれば相対時刻は使えます。
コマンド
date -d "next week"
結果
2018年 4月 27日 金曜日 08:46:20 JST
また、この指定であれば曜日も使えます。
date -d "next sunday"
結果
2018年 4月 22日 日曜日 00:00:00 JST
なお、先週の日曜日であれば
date -d "last sunday"
結果
2018年 4月 15日 日曜日 00:00:00 JST
とっても親切です。
また、あまり使わないかもしれませんが
date -d "tomorrow"
2018年 4月 21日 土曜日 08:46:20 JST
といった指定方法も可能です。(1日前であればyesterday
も可能)
その3は少し毛色が変わりますが、unixtimeというものをdateで扱う時のノウハウを説明します。
unix(linuxの大元のOSみたいなやつ)で使われる時刻の概念です。○年×月△日
みたいな人間がわかる表記ではなく、ただの数値で表されます。ちなみに、1970年1月1日 0時0分 GMT を起点に計算されます。
具体例で言うと
unixtime | 日付・時刻 |
---|---|
0 | 1970/1/1 0:00:00 |
1 | 1970/1/1 0:00:01 |
60 | 1970/1/1 0:01:00 |
3600 | 1970/1/1 1:00:00 |
86400 | 1970/1/2 0:00:00 |
946684800 | 2000/1/1 0:00:00 |
1524182400 | 2018/4/20 0:00:00 |
こんな感じです。0 = 1970/1/1 0:00:00 GMT
を起点にし、1秒毎に1が追加されていきます。
これを使うと何が便利かと言うと、先程と近いのですが 日付の計算を数値で行えるようになる ということが言えます。
つまり、本来は3600秒=60分=1時間のような計算
や、2月は28日まで、3月は31日まで、、
のように単純な規則性がない場合の考慮が不要になることがメリットです。
まず、基本の使い方から。時刻をunixtimeで表示する場合はフォーマット指定で+%s
と指定すればOKです。
コマンド
date "+%s"
結果
1524182400
# 2018-04-20 9:00
このようになります。
次は、unixtimeから時刻に変換する場合の方法です。
こちらは、-d @"変換したいunixtime"
というオプション指定でOKです。
コマンド
date -d @1524182400
結果
2018年 4月 20日 金曜日 09:00:00 JST
とこのようになります。
ということで、linuxで一番親切なコマンドであるdateコマンドの魅力を お伝えしました。
スポンサーリンク
2022/05/21 02:01:01
cialis and levitra levitra dosage 20mg https://glevitrargu.com/
2022/05/21 03:38:54
dissertation writing services
help with phd dissertation
doctorate degree online no dissertation
2022/04/07 22:10:28
combitic global tadalafil avanafil vs tadalafil
2022/04/08 06:28:58
п»їcialis tadalafil reviews
2022/04/08 15:27:00
online sildenafil liquid sildenafil citrate dosage
2022/04/09 03:26:53
long term use of tadalafil 100mg cialis tadalafil
2022/04/10 06:45:52
what does viagra do buy cheap viagra
2022/04/10 16:14:02
buying viagra does generic viagra work viagra substitute buy generic viagra
2022/04/11 18:44:03
viagra brand name generic drug order sildenafil from canada
2022/04/12 03:23:31
canada drugs on line walgreens pharmacy
2022/04/12 12:02:45
Glucophage is it safe to buy prescription drugs from canada
2022/04/13 10:45:45
indian pharmacy paypal arthritis
2022/04/13 19:01:23
cialis shipping cialis brand no prescription needed
2022/04/14 03:37:54
va prescription drugs pharmacy online 365 discount code
2022/04/14 13:26:33
xanax canadian pharmacy pharmacy tech training online
2022/04/15 17:47:07
over the counter viagra for women buying generic viagra online
2022/04/16 02:29:11
levitra efectos secundarios levitra for bph
2022/04/16 12:11:56
cialis without perscriptions cialis with daxopretine
2022/04/16 20:48:36
ivermectin for guinea pig lice ivermectin dosage dogs
2022/04/17 06:13:18
ivermectin tablet ivermectin moa
2022/04/17 15:01:09
cheap viagra pills india buy sildenafil citrate online
2022/04/18 18:38:11
tadalafil eli lilly tadalafil dose
2022/04/19 03:45:59
what to consider as budget when running pharmacy store the canadian pharmacy complaints
2022/04/19 20:35:50
where can i buy viagra tablets sildenafil 100mg tablets canada
2022/04/20 05:10:33
buy cheap levitra how to make levitra more effective
2022/04/20 21:58:28
cvs pharmacy store manager salary store locator lloyds pharmacy
2022/04/22 01:29:27
cialischeap buy cialis generic online
2022/04/22 10:02:21
levitra vs viagra cost why is levitra so expensive
2022/04/23 01:00:00
georgia board of pharmacy canadian pharmacy viagra cialis
2022/04/23 09:38:55
Yasmin online canada pharmacy
2022/04/23 18:32:15
pharmacies in canada prescription drugs app
2022/04/24 02:33:50
average cost of prescription drugs order drugs from canada legal
2022/04/25 06:09:19
first canadian pharmacy addictive prescription drugs
2022/04/25 14:50:44
sav on pharmacy store locator walgreen pharmacy online
2022/04/29 12:48:30
canadian overnight pharmacy review drug store pharmacy technician https://dtupharmyjn.com/
2022/04/29 21:17:40
online canadian pharmacy 24 safe canadian online pharmacy https://jzpharmacyukj.com/
2022/04/30 05:58:43
purchase female viagra online can i buy viagra https://sgiviagraikn.com/
2022/04/30 13:52:33
ivermectin goat dosage ivermectin for swine oral https://stromectolujlo.com/
2022/05/01 16:35:35
sildenafil brand names sildenafil tablets https://sildenafilkiol.com/
2022/05/02 01:14:28
levitra prescribing info viagara cialis levitra comparison https://glevitrargu.com/
2022/05/03 15:24:26
cialis information cialis mit dapoxetine https://dccialisxvf.com/
2022/05/03 23:28:24
levitra price costco levitra over the counter https://xclevitradb.com/
2022/05/04 07:59:16
real online pharmacy best canadian pharmacy
2022/05/04 16:07:06
buy female viagra in india where can i buy viagra over the counter https://aviagrasbt.com/
2022/05/08 16:04:20
hydrocloquin https://keys-chloroquinehydro.com/
2022/05/09 01:54:38
hcqs 400 https://keys-chloroquinehydro.com/
2022/05/09 09:31:20
cialis, money order cialis vs viagra side effects https://dccialisxvf.com/
2022/05/09 17:30:12
does tricare cover levitra levitra expiration dates https://xclevitradb.com/
2022/05/10 01:44:31
best canadian pharmacy online pharmacy tech
2022/05/10 09:47:34
sildenafil 100mg online canada sildenafil online purchase india https://aviagrasbt.com/
2022/05/11 11:08:12
sildenafil manufacturer viagra sildenafil https://sildenafilkiol.com/
2022/05/11 19:23:07
cost viagra cialis levitra levitra with alcohol https://glevitrargu.com/
2022/05/12 22:28:54
2022/05/13 13:27:11
2022/05/14 20:07:55
does tricare cover levitra sildenafil levitra https://xclevitradb.com/
2022/05/15 02:23:45
uk dissertation writing help quotes
dissertation proposal writing service
dissertation help nz
2022/05/15 04:37:05
cvs pharmacy store hours sunday viagra 100mg canadian pharmacy
2022/05/16 01:21:37
business dissertation help
online dissertation writer
dissertation writing service new york
2022/05/16 14:41:57
sildenafil at walgreens sildenafil uses https://sildenafilkiol.com/
2022/05/18 02:25:42
order modafinil 200mg online cheap buy modafinil online cheap
2022/05/18 14:57:34
whats better cialis viagra or levitra viagra vs cialis vs levitra https://dccialisxvf.com/
2022/05/18 22:29:31
provigil order modafinil medication buy provigil online
2022/05/18 23:19:53
prices levitra buy viagra levitra https://xclevitradb.com/
2022/05/19 07:44:43
Ditropan phentermine online pharmacy
2022/05/19 14:01:31
dissertation writing services
doctoral dissertation help reference
writing a dissertation abstract
2022/05/19 15:36:28
sildenafil 48 tabs viagra for sale paypal https://aviagrasbt.com/
2022/05/19 18:14:22
modafinil cost modafinil medication purchase modafinil without prescription
2022/05/20 00:54:45
2022/05/20 03:04:31
buy provigil online provigil medication
2022/05/20 11:50:42
https://erythromycinn.com/# low dose erythromycin
2022/05/20 17:48:34
how sildenafil works sildenafil medicine https://sildenafilkiol.com/