linux 自动删除文件脚本 linux定时删除一个目录的脚本

linux \u4e0b\u5982\u4f55\u7f16\u5199\u5b9a\u65f6\u5220\u9664\u67d0\u76ee\u5f55\u4e0b\u6587\u4ef6\u7684\u811a\u672c\uff1f

\u81ea\u5df1\u7f16\u5199crontab\u8bbe\u5b9a\u8ba1\u5212\u4efb\u52a1\u5c31\u662f\u4e86\uff0c \u90a3\u4e2a\u670d\u52a1\u5c31\u662f\u505a\u8fd9\u4e9b\u5b9a\u65f6\u7684\uff0c\u81ea\u52a8\u5316\u7684\u5de5\u4f5c\u7528\u7684\u3002\u8981\u5199\u4ec0\u4e48\u811a\u672c\u7684\uff0c\u4e00\u4e2a\u547d\u4ee4\u641e\u5b9a\u3002
\u7ed9\u4f60\u4e2a\u624b\u518c\u9875\u81ea\u5df1\u770b\u5427\u3002

\u8865\u5145\u56de\u7b54\uff1a
\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\uff1a
crontab -e
\u6b64\u547d\u4ee4\u5c06\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u5668\u4ee5\u6dfb\u52a0\u65b0\u4efb\u52a1\u3002
* 20 * * * rm -rf /root/febhost/dat/* /root/febhost/log/*

\u7528\u5bf9\u5e94\u7684\u6587\u672c\u7f16\u8f91\u5668\u547d\u4ee4\u4fdd\u5b58\u9000\u51fa\uff08vi\u7f16\u8f91\u5668\u662f\u6309esc\u540e\u6253:wq\uff09

\u5982\u679c\u4f60\u5bf9\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u719f\u6089\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6539/etc/crontab\uff0c\u6211\u770b\u5230\u4f60\u662f\u8981\u5220\u9664root\u6587\u4ef6\u5939\u5185\u7684\u76ee\u5f55\uff0c\u5047\u8bbe\u4f60\u6709root\u6743\u9650\uff08\u56e0\u4e3a/etc/crontab\u6240\u6709\u8005\u662froot\uff0c\u5e76\u4e14\u53ea\u6709\u4ee5root\u8eab\u4efd\u6267\u884c\u5220\u9664\u547d\u4ee4\u624d\u80fd\u5220\u9664root home\u5185\u7684\u6587\u4ef6\uff09\uff0c\u7528\u4ee5\u4e0b\u547d\u4ee4\u6253\u5f00\u6b64\u6587\u4ef6\uff1a
vi /etc/crontab
\u5c06\u5149\u6807\u79fb\u52a8\u81f3\u6700\u540e\u4e00\u884c\uff0c\u6309o\u952e\u65b0\u5f00\u4e00\u884c\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a
* 20 * * * root rm -rf /root/febhost/dat/* /root/febhost/log/*

\u7ed9\u4f60\u89e3\u91ca\u4e0b\u6b64\u884c\u7684\u542b\u4e49\u5427\uff0c\u524d\u4e94\u4e2a\u4ee54\u4e2a\u7a7a\u683c\u5206\u9694\u5f00\u7684\u503c\u4f9d\u6b21\u8868\u793a\uff1a\u5206\u3001\u65f6\u3001\u65e5\u3001\u6708\u3001\u5468\uff0c\u5982\u679c\u53d6\u6240\u6709\u503c\u5c31\u662f\u6253*\u53f7\uff0c\u5982\u679c\u4f60\u60f3\u5468\u671f\u6027\u7684\u8fd0\u884c\u4e00\u4e2a\u4efb\u52a1\uff0ccrontab\u4e5f\u63a5\u53d7\u8303\u56f4\u6307\u5b9a\uff0c\u6bd4\u5982\u8bf4\u4e00\u5929\u4e2d\u7684\u65e98\u70b9\u5230\u665a6\u70b9\u6bcf\u9694\u4e00\u5c0f\u65f6\uff08\u5c31\u662f\u6bcf\u4e24\u5c0f\u65f6\uff0c\u5c06\u4f1a\u57288,10,12,14,16,18\u6267\u884c\uff09\u6267\u884c\u4f60\u8fd9\u4e2a\u5220\u9664\u547d\u4ee4\u8fd9\u6837\u5199\uff1a
* 8-18/2 * * * root rm -rf /root/febhost/dat/* /root/febhost/log/*
\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u662f\u5206\u949f\uff0c\u53d6\u503c\u8303\u56f4\uff1a0-59
\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u662f\u5c0f\u65f6\u3002\u53d6\u503c\u8303\u56f4\uff1a0-23
\u7b2c\u4e09\u4e2a\u5b57\u6bb5\u662f\u4e00\u4e2a\u6708\u4e2d\u7684\u7b2c\u51e0\u5929\uff0c\u53d6\u503c\u8303\u56f4\uff1a1-31
\u7b2c\u56db\u4e2a\u5b57\u6bb5\u662f\u4e00\u5e74\u4e2d\u7684\u7b2c\u51e0\u4e2a\u6708\uff0c\u53d6\u503c\u8303\u56f4\uff1a1-12
\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u662f\u4e00\u4e2a\u661f\u671f\u4e2d\u7684\u7b2c\u51e0\u5929\uff0c\u4ee5\u661f\u671f\u5929\u5f00\u59cb\u4f9d\u6b21\u7684\u53d6\u503c\u4e3a0\uff5e7\uff0c0\u30017\u90fd\u8868\u793a\u661f\u671f\u5929\u3002
\u4e0b\u4e00\u4e2a\u5b57\u6bb5\u662f\u6267\u884c\u8be5\u547d\u4ee4\u7684\u7528\u6237\u8eab\u4efd\uff0c\u8fd9\u91cc\u662froot\u7528\u6237\uff0c\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u5c31\u662f\u5b9a\u65f6\u8fd0\u884c\u7684\u547d\u4ee4\u3002

\u53ef\u4ee5\u4f7f\u7528find\u7684 ctime \u9009\u9879\uff0c
ctime\u7684\u610f\u601d\u662f\u6700\u540e\u4fee\u6539\u7684\u65f6\u95f4 +3 \u8868\u793a\u4e09\u5929\u524d\uff0c\u4e5f\u5c31\u662f\u5728 /backup_100 \u76ee\u5f55\u91cc \u627e\u5230\u4e09\u5929\u524d\u7684\u7b26\u5408 abc*\u7684\u6587\u4ef6\u53ca\u76ee\u5f55\u5e76\u5220\u9664\u3002
find /backup_100 -name "abc*" -ctime +3 -exec rm {} \;
\u5982\u679c\u6bcf\u5929\u5907\u4efd\u591a\u6b21\uff0c
\u53ef\u4ee5\u4f7f\u7528 NUM=`ls|wc -l`;DEL_NUM=$(($NUM-3)); for i in `ls -tr|head -$DEL_NUM`;do rm -rf $i;done


\u8fd9\u6837\uff0c\u9996\u5148\u5224\u65ad\u5f53\u524d\u5907\u4efd\u4e86$NUM\u6b21\uff0c\u4e5f\u5c31\u662f/backup/abcxxx\u91cc\u6709\u591a\u5c11\u4e2a\u6587\u4ef6\u5939\uff0c
\u7136\u540eNUM-3\u5c31\u662f\u9700\u8981\u5220\u9664\u7684\u6587\u4ef6\u5939\u7684\u4e2a\u6570\uff0c
\u7136\u540e\u4f7f\u7528\u5faa\u73af\uff0c
ls -tr|head -$DEL_NUM \u8fd9\u6837\u901a\u8fc7\u6309\u65f6\u95f4\u5347\u5e8f\u6392\u5217\u53d6\u524d$DEL_NUM\u4e2a\u76ee\u5f55\u540d\u5e76\u4f9d\u6b21\u5220\u9664\uff0c\u6700\u540e\u5c31\u53ea\u5269\u4e0b\u4e86\u6700\u65b0\u7684\u4e09\u4e2a\u76ee\u5f55\u4e86\u3002
\u7136\u540e\u628a\u8fd9\u6837\u7684\u811a\u672c\u6dfb\u52a0\u5230\u4f60\u7684\u81ea\u52a8\u5907\u4efd\u811a\u672c\u7684\u6700\u540e\u9762\u5c31\u53ef\u4ee5\u4e86

rm_/opt/scommnet/upload()
{
find / -type -f -mtime +2 -exec rm {} \;
}

然后把这个函数放入用户的.bash_profile中去,就可以开机自动删除2 天前创建或修改的文件了

如果你是root ,那么你的.bash_profile文件就在根目录下中,如果你是普通用户,那么这个文件,就在$HOME中
find命令在“/”下查找普通文件,修改时间在2日以内的文件,-exec是来执行命令,rm的,后面的{} \;是-exec执行命令时必须加上的东西
试下吧,祝你好运

在脚本中应该这样写:
rm -rf `/usr/bin/find /opt/scommnet/webchat/upload/* -ctime 2`

注意是 ` 不是单引号.

using System.IO;

File.Delete(Server.MapPath("upload")+"\\文件名");

扩展阅读:linux清理日志文件脚本 ... linux定时运行脚本文件 ... linux编写sh脚本文件 ... linux 强制删除文件 ... linux定时任务crontab ... 自动删除文件脚本bat ... linux定时删除日志脚本 ... linux删除命令rm-rf ... 自动删除指定的文件脚本 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网