3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コマンドプロンプトに日時(タイムスタンプ)をつける

Posted at

Linux&bashの場合PS1環境変数を変更することでコマンドプロンプトを変更できます。以下のようにすればコマンドプロンプトに日時(タイムスタンプ)をつけることができます。

$ PS1='\D{%c} \u@\h:\w\$ '

実行後はこんな感じです。

Fri Mar 24 05:42:49 2017 it@it:~$ PS1='\D{%c} \u@\h:\w\$ '
Fri Mar 24 05:42:51 2017 it@it:~$ 
Fri Mar 24 05:42:52 2017 it@it:~$ 
Fri Mar 24 05:42:52 2017 it@it:~$ 
Fri Mar 24 05:42:52 2017 it@it:~$ 
Fri Mar 24 05:42:52 2017 it@it:~$ 

もうちょい短くするならPS1='\D{%F %T} \u@\h:\w\$ 'とすれば、以下のような感じにちょい短くなります。

$ PS1='\D{%F %T} \u@\h:\w\$ '
2017-03-24 05:43:39 it@it:~$

さらに短くするならPS1='\D{%y%m%d%H%M%S} \u@\h:\w\$ 'とすればシリアル番号みたいに短くなります。

$ PS1='\D{%y%m%d%H%M%S} \u@\h:\w\$ '
170324054411 it@it:~$ 
170324054412 it@it:~$ 

それはさすがに…ということならPS1='[\D{%c}]\n\u@\h:\w\$ 'のように改行コード(\n)を挟んで2行にしちゃうという手もあります。冗談のような話ですが実際に使ってみると思ったほどは気にならないので、私は案外これが好きだったりします。

$ PS1='[\D{%c}]\n\u@\h:\w\$ '
[Fri Mar 24 05:45:52 2017]
it@it:~$ 
[Fri Mar 24 05:45:52 2017]
it@it:~$ 

こうゆう設定をすると場所をとるといえばとるのですが、ターミナル&コピー&ペーストで、いつ何をしてどうゆう結果になったのか、といった記録を残す時とかは便利かなと思います。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?