2
4

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.

historyコマンド

Posted at

お題

家ではLinux、会社ではMacを使っていて、historyコマンドにご厄介になることが多い。
fishとかpecoとか入れてる人はそんなことないのかな・・・)
使うのは、たいてい、過去に自分が叩いた(長めの)コマンドが何だったか知りたい時。
こんな感じで。「history | grep docker
とした時に、似たようなのが複数出て、「これ、いつ叩いたやつだっけ?」と、ふと、ヒントが欲しくなる。
コマンド叩いた日時が出たらいいなぁと思って調べたら設定ひとつでできたので、ついでに便利そうな設定や使い方が他にもないか調べてみた。

環境

Linux(Ubuntu)だけど、historyいじるので本体を汚すのは避けたくdocker(イメージはubuntu:latest)を使用。

実践

■コンテナ準備

> sudo docker pull ubuntu:latest
latest: Pulling from library/ubuntu
 〜省略〜
Status: Downloaded newer image for ubuntu:latest

> sudo docker run -itd ubuntu

> sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
79a9cefd4368        ubuntu              "/bin/bash"         7 seconds ago       Up 5 seconds                            fervent_kare

> sudo docker exec -it 79a9cefd4368 bash
root@79a9cefd4368:/# 

■historyコマンド叩いたら各コマンド実行日時も出るようにする

root@79a9cefd4368:/# history
    1  history
root@79a9cefd4368:/# export HISTTIMEFORMAT='[%Y/%m/%d %H:%M:%S] '
root@79a9cefd4368:/# history
    1  [2018/10/03 16:15:14] history
    2  [2018/10/03 16:15:26] export HISTTIMEFORMAT='[%Y/%m/%d %H:%M:%S] '
    3  [2018/10/03 16:15:29] history
root@79a9cefd4368:/# 

これで、パイプで grep する時、日付で絞れる。
(ログインのつど export 叩くの嫌だから、 .bashrc あたりに書いておく。)

■(「history」みたいに)いちいち履歴に出なくていいやつを除外する

root@79a9cefd4368:/# export HISTIGNORE=history*:man*
root@79a9cefd4368:/# pwd
/
root@79a9cefd4368:/# history
    1  [2018/10/03 16:31:31] export HISTIGNORE=history*:man*
    2  [2018/10/03 16:31:39] pwd
root@79a9cefd4368:/# man ls
bash: man: command not found
root@79a9cefd4368:/# history
    1  [2018/10/03 16:31:31] export HISTIGNORE=history*:man*
    2  [2018/10/03 16:31:39] pwd
root@79a9cefd4368:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@79a9cefd4368:/# history
    1  [2018/10/03 16:31:31] export HISTIGNORE=history*:man*
    2  [2018/10/03 16:31:39] pwd
    3  [2018/10/03 16:32:18] ls

「history」、「man」コマンドの履歴は出てこなくなった。

■履歴を全消し

root@03c12f5ae24d:/# history -c
root@03c12f5ae24d:/# history

ただ、コンテナにログインしなおしたら、クリアしたはずの履歴が復活してた。
~/.bash_history」に情報が残っているためみたい。「-c」はカレントコンソールの履歴はクリアするけど、
~/.bash_history」の情報は消してくれず、再ログイン時に履歴が元に戻る挙動のよう。
こうすると良いみたい。↓

root@03c12f5ae24d:/# history -c && history -w
root@03c12f5ae24d:/# history
root@03c12f5ae24d:/# exit
exit
koge@koge-W65-67SF:~$ sudo docker exec -it 03c12f5ae24d bash
root@03c12f5ae24d:/# history
    1  [2018/10/03 17:10:25] exit

■履歴のサイズを限界まで上げる

root@03c12f5ae24d:/# echo $HISTSIZE
1000
root@03c12f5ae24d:/# export HISTSIZE=10000
root@03c12f5ae24d:/# echo $HISTSIZE
10000

まとめ

実験は終わったので、種々の export をコンテナでない本体の「~/.bashrc」に追記して終了。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?