命令
find 對應目錄 -mtime +天數 -name "文件名" -exec rm -rf {} \;
詳情講解:
find /home/weblogic/tmp -mtime +10 -name "." -exec rm -rf {} ;
將/home/weblogic/tmp目錄下所有10天前帶"."的文件刪除
find
:linux的查找命令,用戶查找指定條件的文件
/home/weblogic/tmp
:想要進行清理的任意目錄
-mtime
:標準語句寫法
+10:查找10天前的文件,這里用數字代表天數,+30表示查找30天前的文件
".":希望查找的數據類型,".jpg"表示查找擴展名為jpg的所有文件,""表示查找所有文件,這個可以靈活運用
-exec
:固定寫法
rm -rf
:強制刪除文件,包括目錄
{} \;
:固定寫法,一對大括號+空格++;
查找指定文件夾并刪除
find /home/weblogic/tmp -mtime +10 -name "hello" -exec rm -rf {} \;
定時任務
操作步驟
- 新建一個腳本文件auto_del_threeMonthes_before.sh
# touch /home/weblogic/tmp auto_del_threeMonthes_before.sh
# chmod 777 auto_del_threeMonthes_before.sh
- 2.vi auto_del_threeMonthes_before.sh
編輯腳本文件如下:
#!/bin/sh
find /home/weblogic/tmp -mtime +91 -name "*.*" -exec rm -rf {} \;
ok,保存退出
3.#crontab -e
將腳本文件加入到系統計劃任務,到點自動執行
輸入:
- 2 * * */home/weblogic/tmp/auto_del_threeMonthes_before.sh
這里的設置是每天凌晨2點執行auto_del_threeMonthes_before.sh文件進行數據清理,可以研究一下cron,制定自己需要的計劃任務
測試使用腳本
準備測試腳本
測試定時任務使用這個腳本,測試該腳本是否可以每分鐘新建一個文件夾?
#! /bin/bash
cd /home
time=$(date "+%Y%m%d%H%M%S")
filename=${time}.txt
touch $filename
添加定時任務
crontab -e 添加定時命令
* * * * *sh /home/touch1.sh