LoginSignup
80

More than 5 years have passed since last update.

tmpwatchコマンドで定期的にファイルを削除する。

Last updated at Posted at 2015-03-03

tmpwatchコマンドとは

使い所

  • ある一定の時間を過ぎたファイルやディレクトリを削除したい時
  • 一時ファイルを定期的に削除したい時
  • 定期的に◯時間アクセスがないログファイルとかを削除したい時

etc...

使い方

$ tmpwatch [option] [hours] [dirs]

dirsの指定にはワイルドカード(*)が使える。

一定期間アクセスがないファイルを削除する

240時間アクセスが無い/tmp/下のファイルを削除。

$ tmpwatch 240 /tmp

一定期間過ぎたファイルやディレクトリを削除する

更新日が720時間以上古いファイルを削除

$ tmpwatch -m 720 /path/to/log

ディレクトリは除外したい場合は-d

/tmp内で最終更新から240時間たったファイルを削除。ただしディレクトリは削除しない。

$ tmpwatch -m 240 -d /tmp

特定のディレクトリは除外

/tmpディレクトリ内の/tmp/hoge以外のファイルを対象として、24時間アクセスのないものを削除

$ tmpwatch 24 /tmp -x /tmp/hoge

オプション一覧

  • -u, –atime –アクセス時刻
  • -m, –mtime –更新時刻
  • -c, –ctime –ステータス変更時刻
  • -M, –dirmtime –ディレクトリ更新時刻
  • -a, –all –種類に関わらずすべてのファイルを削除する
  • -d, –nodirs –ディレクトリは削除しない
  • -f, –force –root権限なくても削除を行う
  • -l, –nosymlinks — シンポリックリンクは削除しない
  • -q, –quiet –コマンドライン表示しない(エラー時のみ表示)
  • -s, –fuser –fuserコマンドを使ってファイル状態を確認
  • -t, –test –テストモード(実際削除処理は走らない)
  • -U, –exclude-user=user –指定するユーザのファイルを削除対象外
  • -v, –verbose –詳細モード
  • -x, –exclude=path –指定するファイル・フォルダを削除対象外にする

cronへの登録はこちらを参照

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
80