Clean directory (first segment) and file (second segment) of specific dir

Clean directory (first segment) and file (second segment) of specific dir

[weblogic@pamesapp01 bin]$ cat cleanEpartsTmpFile.sh 
#!/bin/bash
export KEEP_DAYS=3
export DIR=/opt/data/public/eparts/tmpfile/
echo Start to clean $DIR
echo Befor House Keeping $DIR ... Total Files: `ls -l $DIR | wc -l`
find $DIR -type d -mtime +$KEEP_DAYS -maxdepth 1 -print -exec rm -rf {} \;
echo After House Keeping $DIR ... Total Files: `ls -l $DIR | wc -l`
export DIR2=/opt/data/public/eparts/tmp/
echo Start to clean $DIR2
echo Befor House Keeping $DIR2 ... Total Files: `ls -l $DIR2 | wc -l`
find $DIR2 -mtime +$KEEP_DAYS -print -exec rm -rf {} \;
echo Befor House Keeping $DIR2 ... Total Files: `ls -l $DIR2 | wc -l`

說明:

  • find指令的"-type d"可以指定查詢的唯folder
  • "-mtime +2" 代表modify day超過兩天
  • "-maxdepth 1"代表往下查詢一個階層(因為rm掉folder的話,再往下查就會有ERROR,雖不影響動做,但很難看...)