- ファイル検索するコマンド
find [オプション] [検索文字列]
例)「bak」の文字列が含まれたファイル検索
% find -name "*bak"
- 現在から3日前まで(現在時間〜72時間前)
「-mtime 日数」 : 指定した日数より前に更新されたファイル
$ find ./ -mtime -1
- Tempフォルダで30日前の古いファイル削除
[root@localhost ]# cd /tmp
[root@localhost tmp]# find ./ -mtime +30 | xargs rm -f
- ファイルのみ検索
「-type f」 : ファイルのみを対象とし検索する
拡張子が「.bak」になったファイル検索する
% find ./ -type f -name "*.bak"
- ファイル検索後削除
% find ./ -type f -name "*.bak" | xargs rm
-「/tmp/」の古いファイルの削除処理(バッチ処理)
[root@localhost yum.repos.d]# cat /usr/local/bin/old_tmp_remove.bash
#!/bin/bash
MTIME=100
REGEX="/tmp/\.?(X11-unix|XIM-unix|font-unix|ICE-unix|Test-unix|hsperfdata_).*"
# /etc/cron.daily/tmpwatch で除外しているディレクトリを find コマンドで再現しつつ、削除。
find /tmp/ -type f -mtime +$MTIME -regextype posix-extended ! -regex $REGEX -ls -delete
# 何もないフォルダも削除。
find /tmp/ -type d -empty -mtime +$MTIME -regextype posix-extended ! -regex $REGEX -ls -delete