CentOS 如何找出符合所有含有特定內容檔案,然後刪除掉

找了好久, 都有效率不彰的問題, 最後試出來 locate 這指令很快, 以下是shell scripts:

#!/bin/bash

# 找出/home/vmail目錄以下所有含有1552047的檔案名稱,再過濾裡面是否有 5o6n6K+J6LWW55Cm54+K77yB 字串 
list=`locate /home/vmail 15520479 | grep -lr "5o6n6K+J6LWW55Cm54+K77yB" . 2>/dev/null`

if [[ -z "$list" ]]; then
  echo "No files matched, nothing to be done...";
  exit
fi

# delete files one by one
for match in $list
do
    echo "Removing matched file $match..."
    echo `rm $match`
    echo `updatedb`
done

echo "...done."

發表迴響