historyコマンドの履歴を削除する方法

2021-07-13

historyコマンドについて

コマンドの履歴を表示する「history」コマンドがあります。コマンドで引数にパスワードを付きのコマンドを利用したり、間違った内容をペーストしたりして「history」の履歴したい場合あるかと思います。個別に削除する方法とhistoryの履歴を全て削除する方法を紹介します

historyコマンドの全ての履歴を削除する

すべての履歴を削除するには「-c」のオプションを利用します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ history -c
$ history -c
$ history -c

historyコマンドの履歴を個別に削除する

historyコマンドで個別に削除するには「history -d 番号」で削除したいコマンドの番号を入力します。
但しhistoryの履歴に「history -d 番号」が残るのであまりスマートなやり方ではありません

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ history
320 touch test.txt
321 ls -la
322 ls /var/www
323 exit
324 vim /etc/httpd/conf/httpd.conf
325 vim sql.txt
326 ls
327 top
328 free -m
329 sh test.sh admin dl4d94das
# 「329 sh test.sh admin dl4d94das」 の履歴を削除します
$ history -d 329
$ history 320 touch test.txt 321 ls -la 322 ls /var/www 323 exit 324 vim /etc/httpd/conf/httpd.conf 325 vim sql.txt 326 ls 327 top 328 free -m 329 sh test.sh admin dl4d94das # 「329 sh test.sh admin dl4d94das」 の履歴を削除します $ history -d 329
$ history 
  320  touch test.txt
  321  ls -la
  322  ls /var/www
  323  exit
  324  vim /etc/httpd/conf/httpd.conf
  325  vim sql.txt
  326  ls
  327  top 
  328  free -m
  329  sh test.sh admin dl4d94das

# 「329  sh test.sh admin dl4d94das」 の履歴を削除します
$ history -d 329

historyの履歴は「~/.bash_history」のファイルにあるので、このファイルを直接編集してもhistoryの履歴を削除できます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ vim ~/.bash_history
$ vim ~/.bash_history
$ vim ~/.bash_history

 

スポンサーリンク

0
0