Question
I want to run a cron job that delete all files in a directory when it exceeds a number. For example when it become 1000 files, then delete all files in that directory. The goal is clearing cache directory.
How-To
Let's do this step by step. If your cache folder path is ./Cache, you can count the number of file under it this way:
Based on the above description, we can write a simple shell:
- delCache.sh
- #!/bin/sh
- CACHE_DIR='./Cache'
- T=5
- if [ "$#" -gt 0 ]; then
- T=$1
- fi
- echo -e "\t[Info] Threshold to clean Cache...$T"
- if [[ `find $CACHE_DIR -type f | wc -l` -ge $T ]]; then
- echo -e "\t[Info] Delete Cache files..."
- find $CACHE_DIR -type f -exec rm -f {} \;
- fi
Supplement
* [Linux 命令] find : 尋找特定字串的檔案或目錄
* Linux find 命令使用詳解
沒有留言:
張貼留言