古い不要ファイル・ディレクトリを削除する『tmpwatch』コマンド

作成した一時ファイルや古いログファイルの削除や、使われなくなった古いファイルを自動的に削除する際に使えるのが、今回紹介する『tmpwatch』コマンドだ。
tmpwatchコマンドは「/tmp」フォルダ配下へ配置したファイルのように、指定したPATH内にある、一定期間(指定する)アクセスされていないファイル・ディレクトリを削除してくれるコマンドだ…というか、「/tmp」フォルダ配下の削除を行っているのはこのコマンドだ。
スクリプト等で古いログを削除させる場合、rmコマンドとfindコマンドを組み合わせて削除させている事が多いけれども、これを使えば同じことを一行で実現できる。

基本的にはtmpwatchはRHEL系のコマンドだが、Debian系にも「tmpreaper」というコマンドがある。「tmpreaper」コマンドについては、また後日記述する。
今回は、CentOS 7にこのtmpwatchコマンドをインストールし利用してみることとする。

1.インストール

デフォルトで入っている事が多いのだが、インストール方法によっては入っていない場合もある。
もし入っていない場合は、以下のようにインストールを行う。

bash
yum install tmpwatch
shell
[root@dctest01 work]# yum install tmpwatch 読み込んだプラグイン:fastestmirror base | 3.6 kB 00:00 epel/x86_64/metalink | 5.2 kB 00:00 epel | 4.4 kB 00:00 epel-debuginfo/x86_64/metalink | 5.4 kB 00:00 epel-debuginfo | 3.0 kB 00:00 epel-source/x86_64/metalink | 5.2 kB 00:00 epel-source | 3.7 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 (1/4): epel/x86_64/primary_db | 4.1 MB 00:00 (2/4): epel-debuginfo/x86_64/primary_db | 566 kB 00:00 (3/4): epel-source/x86_64/primary_db | 1.4 MB 00:00 (4/4): updates/7/x86_64/primary_db | 957 kB 00:00 (1/4): epel/x86_64/updateinfo | 364 kB 00:00 (2/4): epel-source/x86_64/updateinfo | 364 kB 00:00 (3/4): epel/x86_64/pkgtags | 1.4 MB 00:00 (4/4): epel-source/x86_64/pkgtags | 1.4 MB 00:00 Loading mirror speeds from cached hostfile * base: ftp.iij.ad.jp * epel: ftp.kddilabs.jp * epel-debuginfo: ftp.kddilabs.jp * epel-source: ftp.kddilabs.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp 依存性の解決をしています --> トランザクションの確認を実行しています。 ---> パッケージ tmpwatch.x86_64 0:2.11-5.el7 を インストール --> 依存性の処理をしています: psmisc のパッケージ: tmpwatch-2.11-5.el7.x86_64 --> トランザクションの確認を実行しています。 ---> パッケージ psmisc.x86_64 0:22.20-8.el7 を インストール --> 依存性解決を終了しました。 依存性を解決しました =========================================================================================================== Package アーキテクチャー バージョン リポジトリー =========================================================================================================== インストール中: tmpwatch x86_64 2.11-5.el7 base 依存性関連でのインストールをします: psmisc x86_64 22.20-8.el7 base トランザクションの要約 =========================================================================================================== インストール 1 パッケージ (+1 個の依存関係のパッケージ) 総ダウンロード容量: 179 k インストール容量: 537 k Is this ok [y/d/N]: y Downloading packages: (1/2): psmisc-22.20-8.el7.x86_64.rpm | 140 kB 00:00 (2/2): tmpwatch-2.11-5.el7.x86_64.rpm | 38 kB 00:00 ----------------------------------------------------------------------------------------------------------- 合計 141 kB/s | 179 kB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction インストール中 : psmisc-22.20-8.el7.x86_64 インストール中 : tmpwatch-2.11-5.el7.x86_64 検証中 : tmpwatch-2.11-5.el7.x86_64 検証中 : psmisc-22.20-8.el7.x86_64 インストール: tmpwatch.x86_64 0:2.11-5.el7 依存性関連をインストールしました: psmisc.x86_64 0:22.20-8.el7 完了しました! [root@dctest01 work]#

これでインストールが出来た。

2.コマンドの実行(基本)

さて、それでは実際にコマンドを実行してみよう。
tmpwatchコマンドには色々とオプションがいっぱい付いているけど、基本的には以下のように実行する。

bash
tmpwatch [オプション] 時間(hour) 削除対象ディレクトリ

実行例がこちら。
例)/work/logフォルダ配下にある、240時間アクセスが無いファイルを削除する。

bash
tmpwatch 240 /work/log

削除対象とするディレクトリの指定には、ワイルドカードも利用できる。

3.コマンドの実行(ちょっと応用、オプション利用をする)

3-1.指定する時間の種類を変更する

デフォルトでは、この時間ってのは「指定した時間の間、アクセスが無いもの」を対象とする。
以下のオプションを指定することで、この時間をそれぞれ変える事ができる。

  • -u, –atime :アクセス時刻
  • -m, –mtime :修正時刻
  • -c, –ctime :ステータス変更時刻
  • -M, –dirmtime :フォルダ修正時刻

実行例がこちら。
例)「/work/tmp」フォルダ配下にある、720時間以上前に変更が行われたファイルを削除する。

bash
tmpwatch -m 720 /work/tmp

3-2.削除対象となるファイルの種類を区別する

削除を行うファイル、行わないファイルの種類を区別させることもできる。
関連するオプションが以下。

  • -a, –all :通常はソケットファイル等は削除しないが、ファイルの拡張子に関わらず全てのファイルを削除する
  • -d, –nodirs :ディレクトリの削除は行わない
  • -l, –nosymlinks :シンポリックリンクは削除しない
  • -U, –exclude-user=ユーザID :指定したユーザIDのファイル・ディレクトリは削除しない
  • -x, –exclude=対象のPATH :指定したPATHのファイル・ディレクトリは削除しない

特に、-Uと-x、-lオプションが使えるオプションだ。

実行例がこちら。
例)「/work/tmp」フォルダ配下にある、72時間以上前に変更が行われたファイルで、testユーザー以外のファイルを削除する。

bash
tmpwatch -m -U test 72 /work/tmp

shell
[root@dctest01 tmp]# ls -la 合計 4 drwxr-xr-x. 2 root root 4096 4月 29 19:48 . drwxr-xr-x. 4 root root 80 4月 29 19:45 .. -rw-r--r--. 1 test test 0 1月 1 00:00 test -rw-r--r--. 1 root root 0 1月 1 00:00 test1 -rw-r--r--. 1 root root 0 1月 1 00:00 test2 -rw-r--r--. 1 root root 0 1月 1 00:00 test3 -rw-r--r--. 1 root root 0 1月 1 00:00 test4 -rw-r--r--. 1 test test 0 1月 1 00:00 test5 -rw-r--r--. 1 root root 0 1月 1 00:00 test6 -rw-r--r--. 1 root root 0 1月 1 00:00 test7 -rw-r--r--. 1 root root 0 1月 1 00:00 test8 -rw-r--r--. 1 root root 0 1月 1 00:00 test9 [root@dctest01 tmp]# tmpwatch -m -U test 72 /work/tmp [root@dctest01 tmp]# ls -la 合計 0 drwxr-xr-x. 2 root root 29 4月 29 19:48 . drwxr-xr-x. 4 root root 80 4月 29 19:45 .. -rw-r--r--. 1 test test 0 1月 1 00:00 test -rw-r--r--. 1 test test 0 1月 1 00:00 test5 [root@dctest01 tmp]#

3-3.その他のオプション

テスト実行を行う場合、「-t」オプションを利用することで削除対象となるファイルの一覧を取得できる。
例)「/work/tmp」フォルダ配下にある、72時間以上前に変更が行われたファイルで、testユーザー以外のファイルを削除する。(テスト実行)

bash
tmpwatch -t -m -U test 72 /work/tmp

shell
[root@dctest01 tmp]# ls -la 合計 4 drwxr-xr-x. 2 root root 4096 4月 29 19:53 . drwxr-xr-x. 4 root root 80 4月 29 19:45 .. -rw-r--r--. 1 test test 0 1月 1 00:00 test -rw-r--r--. 1 root root 0 1月 1 00:00 test1 -rw-r--r--. 1 root root 0 1月 1 00:00 test2 -rw-r--r--. 1 root root 0 1月 1 00:00 test3 -rw-r--r--. 1 root root 0 1月 1 00:00 test4 -rw-r--r--. 1 test test 0 1月 1 00:00 test5 -rw-r--r--. 1 root root 0 1月 1 00:00 test6 -rw-r--r--. 1 root root 0 1月 1 00:00 test7 -rw-r--r--. 1 root root 0 1月 1 00:00 test8 -rw-r--r--. 1 root root 0 1月 1 00:00 test9 [root@dctest01 tmp]# tmpwatch -t -m -U test 72 /work/tmp removing file /work/tmp/test1 removing file /work/tmp/test2 removing file /work/tmp/test3 removing file /work/tmp/test4 removing file /work/tmp/test6 removing file /work/tmp/test7 removing file /work/tmp/test8 removing file /work/tmp/test9 [root@dctest01 tmp]# ls -la 合計 4 drwxr-xr-x. 2 root root 4096 4月 29 19:53 . drwxr-xr-x. 4 root root 80 4月 29 19:45 .. -rw-r--r--. 1 test test 0 1月 1 00:00 test -rw-r--r--. 1 root root 0 1月 1 00:00 test1 -rw-r--r--. 1 root root 0 1月 1 00:00 test2 -rw-r--r--. 1 root root 0 1月 1 00:00 test3 -rw-r--r--. 1 root root 0 1月 1 00:00 test4 -rw-r--r--. 1 test test 0 1月 1 00:00 test5 -rw-r--r--. 1 root root 0 1月 1 00:00 test6 -rw-r--r--. 1 root root 0 1月 1 00:00 test7 -rw-r--r--. 1 root root 0 1月 1 00:00 test8 -rw-r--r--. 1 root root 0 1月 1 00:00 test9

その他、以下のようなオプションが存在している。

  • -f, -force :root権限が無くても削除を実行する
  • -q, -quiet :標準出力を行わない(エラー時のみ出力)
  • -s, –fuser :fuserコマンドを用いて、ファイルを開いているかどうかを確認する
  • -v, –verbose :詳細内容を出力する(何秒かかるか等)

デフォルトでインストールされている事が多いので、知っているとスクリプトの作成などで捗りそうだ。