{"id":174410,"date":"2020-04-06T10:26:03","date_gmt":"2020-04-06T02:26:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=174410"},"modified":"2020-03-24T11:31:07","modified_gmt":"2020-03-24T03:31:07","slug":"remove-linux-file","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/remove-linux-file.html","title":{"rendered":"\u5982\u4f55\u4f18\u96c5\u5730\u5220\u9664 Linux \u4e2d\u7684\u5783\u573e\u6587\u4ef6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u4e5f\u8ddf\u6211\u4e00\u6837\uff0c\u662f\u4e00\u53ea\u8981\u628a\u7684\u81ea\u5df1\u7535\u8111\u6587\u4ef6\u5b89\u6392\u7684\u6761\u7406\u6709\u5e8f\uff0c\u628a\u6ca1\u7528\u7684\u6587\u4ef6\u4f1a\u53ca\u65f6\u5220\u6389\u7684\u7a0b\u5e8f\u733f\u5462?\u5982\u679c\u662f\u7684\u8bdd\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u6109\u5feb\u5730\u63a2\u8ba8\u4e0b\u6587\u7ae0\u7684\u5185\u5bb9\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7559\u4e0b\u6765\u51d1\u51d1\u70ed\u95f9\u561b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4e0b\u9762\u8981\u4ecb\u7ecd\u7684\u662f\u4eca\u5929\u7684\u4e3b\u89d2\u2014\u2014 tmpwatch \uff0c\u5b83\u80fd\u5e2e\u52a9\u6211\u4eec\u9012\u5f52\u5220\u9664\u5728\u7ed9\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u8bbf\u95ee\u7684\u6587\u4ef6\u548c\u7a7a\u76ee\u5f55\u3002<\/p>\n

\u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 find \u547d\u4ee4\u67e5\u627e\u5e76\u5220\u9664\u8d85\u8fc7 x \u5929\u672a\u8bbf\u95ee\u7684\u6587\u4ef6\uff0c\u4e0d\u8fc7 tmpwatch \u53ef\u4ee5\u4e00\u6b65\u5230\u4f4d\uff0c\u4f55\u4e50\u800c\u4e0d\u4e3a?<\/p>\n

tmpwatch \u9ed8\u8ba4\u6839\u636e\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u8bbf\u95ee\u65f6\u95f4(access time)\u6765\u51b3\u5b9a\u5220\u9664\u54ea\u4e9b\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6839\u636e inode \u6539\u53d8\u65f6\u95f4(inode change time)\u3001\u4fee\u6539\u65f6\u95f4(modification time)\u6765\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n

\u901a\u5e38\uff0ctmpwatch \u7528\u4e8e\u5220\u9664 \/tmp \u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u4ee5\u53ca\u5176\u5b83\u5730\u65b9\u5176\u4ed6\u65e0\u7528\u7684\u6587\u4ef6\uff0c\u5982\u65e7\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n

\u91cd\u8981\u8b66\u544a!!<\/strong><\/div>\n

\u4e0d\u8981\u5728 \/(\u6839\u76ee\u5f55)\u4e2d\u8fd0\u884c tmpwatch!<\/p>\n

\u4e0d\u8981\u5728 \/(\u6839\u76ee\u5f55)\u4e2d\u8fd0\u884c tmpwatch!!<\/p>\n

\u4e0d\u8981\u5728 \/(\u6839\u76ee\u5f55)\u4e2d\u8fd0\u884c tmpwatch!!!(\u4e09\u904d\u8b66\u544a! ^ - ^ )<\/p>\n

\/ \u76ee\u5f55\u5305\u542b Linux \u7cfb\u7edf\u8fd0\u884c\u6240\u5fc5\u9700\u7684\u91cd\u8981\u6587\u4ef6\uff0c\u800ctmpwatch \u5e76\u6ca1\u6709\u5185\u7f6e\u4fdd\u62a4\u673a\u5236\u9632\u6b62\u5728\/ \u76ee\u5f55\u4e0a\u8fd0\u884c\uff0c\u4e00\u65e6\u90a3\u4e9b\u91cd\u8981\u7684\u6587\u4ef6\u88ab\u5220\u9664\u4e86\uff0c\u540e\u679c\u4e0d\u582a\u8bbe\u60f3!\u6240\u4ee5\uff0c\u5c0f\u4f19\u4f34\u4eec\u5728\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4\u7684\u65f6\u5019\u4e00\u5b9a\u8981\u614e\u91cd!<\/p>\n

\u5b89\u88c5 tmpwatch<\/strong><\/div>\n

\u5927\u591a\u6570 Linux \u53d1\u884c\u7248\u7684\u9ed8\u8ba4\u5b58\u50a8\u5e93\u4e2d\u90fd\u63d0\u4f9b tmpwatch \u7684\u5b89\u88c5\uff1a<\/p>\n

\u5728 Fedora \u4e0a\uff1a<\/p>\n

$ sudo dnf install tmpwatch <\/pre>\n

\u5728 CentOS \u4e0a\uff1a<\/p>\n

$ sudo yum install tmpwatch <\/pre>\n

\u5728 openSUSE \u4e0a\uff1a<\/p>\n

$ sudo zypper install tmpwatch <\/pre>\n

\u5728 Debian \u53ca\u5176\u884d\u751f\u7248\u672c(\u5982 Ubuntu )\u4e0a\uff0ctmpwatch \u53c8\u53eb tmpreaper\uff1a<\/p>\n

$ sudo apt install tmpreaper <\/pre>\n
\u4f7f\u7528 tmpwatch\/tmpreaper \u5220\u9664\u6307\u5b9a\u65f6\u95f4\u5185\u672a\u8bbf\u95ee\u7684\u6587\u4ef6<\/strong><\/div>\n

tmpwatch \u548c tmpreaper \u7684\u7528\u6cd5\u51e0\u4e4e\u76f8\u540c\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u4e8c\u8005\u662f\u4e00\u6837\u7684\u547d\u4ee4\u3002\u4e3a\u4e86\u4fbf\u4e8e\u63cf\u8ff0\uff0c\u672c\u6587\u4ee5tmpwatch \u4e3a\u4f8b\u8fdb\u884c\u8bb2\u89e3\uff0c\u4f7f\u7528\u57fa\u4e8e Debian \u7cfb\u7edf\u7684\u670b\u53cb\u53ef\u4ee5\u5c06\u4e0b\u9762\u7684 tmpwatch \u6539\u4e3a tmpreaper\u3002<\/p>\n

1. \u5220\u9664\u8d85\u8fc7 X \u5929\u672a\u8bbf\u95ee\u7684\u6587\u4ef6<\/strong><\/span><\/div>\n

\u4f8b\uff1a\u5220\u9664 \/var\/log\/ \u6587\u4ef6\u5939\u4e2d\u8d85\u8fc7 10 \u5929\u672a\u8bbf\u95ee\u7684\u6240\u6709\u6587\u4ef6\u548c\u7a7a\u76ee\u5f55<\/p>\n

tmpwatch 10d \/var\/log\/<\/pre>\n
2.\u5220\u9664\u8d85\u8fc7 X \u5929\u672a\u4fee\u6539\u7684\u6587\u4ef6<\/strong><\/span><\/div>\n

\u524d\u6587\u63d0\u5230\uff0c tmpwatch \u9ed8\u8ba4\u6839\u636e\u8bbf\u95ee\u65f6\u95f4\u6765\u5220\u9664\u6587\u4ef6\u7684\uff0c\u73b0\u5728\u6211\u4eec\u4f7f\u7528 -m \u9009\u9879\u6765\u6839\u636e\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4(modification time)\u6765\u5220\u9664\u6587\u4ef6\u3002<\/p>\n

\u4f8b\uff1a\u5220\u9664 \/var\/log\/ \u6587\u4ef6\u5939\u4e2d\u8d85\u8fc7 10 \u5929\u672a\u4fee\u6539\u7684\u6587\u4ef6<\/p>\n

tmpwatch -m 10d \/var\/log\/ <\/pre>\n

\u4e0a\u9762\u4e24\u4e2a\u547d\u4ee4\u4e2d\u7684 d \u662f\u65f6\u95f4\u53c2\u6570\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n

d - \u5929\u6570
\nh - \u5c0f\u65f6
\nm - \u5206\u949f
\ns - \u79d2\u6570
\n\u9ed8\u8ba4\u65f6\u95f4\u53c2\u6570\u662f\u5c0f\u65f6 \u3002\u5047\u5982\u60f3\u5220\u9664\u8fc7\u53bb 10 \u4e2a\u5c0f\u65f6\u672a\u4fee\u6539\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u8fd9\u79cd\u5f62\u5f0f\uff1a<\/p>\n

tmpwatch -m 10 \/var\/log\/<\/pre>\n
3. \u5220\u9664\u7b26\u53f7\u94fe\u63a5<\/strong><\/span><\/div>\n

\u53ef\u4ee5\u4f7f\u7528 -s \u9009\u9879\u5220\u9664\u7b26\u53f7\u94fe\u63a5\uff1a<\/p>\n

tmpwatch -s 10 \/var\/log\/<\/pre>\n
4. \u5220\u9664\u6240\u6709\u6587\u4ef6(\u5305\u62ec\u5e38\u89c4\u6587\u4ef6\uff0c\u7b26\u53f7\u94fe\u63a5\u548c\u76ee\u5f55)<\/strong><\/span><\/div>\n

tmpwatch \u4e0d\u4ec5\u4ec5\u53ef\u4ee5\u5220\u666e\u901a\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5220\u9664\u4e00\u4e9b\u7279\u6b8a\u6587\u4ef6\uff0c\u6bd4\u5982\u7b26\u53f7\u94fe\u63a5\u3001\u76ee\u5f55\u3001\u7ba1\u9053\u6587\u4ef6\u7b49\u7b49\u3002\u8fd9\u4e2a\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u4f7f\u7528 -a \u9009\u9879\uff1a<\/p>\n

tmpwatch -s 10 \/var\/log\/<\/pre>\n
5. \u5220\u9664\u65f6\u6392\u9664\u76ee\u5f55<\/strong><\/span><\/div>\n

\u5982\u679c\u4e0d\u60f3\u5220\u9664\u67d0\u4e2a\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528 --nodirs \u9009\u9879\uff0c\u5728\u5220\u9664\u65f6\u6392\u9664\u5bf9\u8be5\u76ee\u5f55\u7684\u5220\u9664\uff1a<\/p>\n

tmpwatch -am 10 --nodirs \/var\/log\/ <\/pre>\n
6. \u6d4b\u8bd5\u5220\u9664(\u4e0d\u5b9e\u9645\u5220\u9664\u4efb\u4f55\u5185\u5bb9)<\/strong><\/span><\/div>\n

\u8fd9\u91cc\u8981\u518d\u6b21\u5f3a\u8c03\uff0c\u5728\u5bf9\u91cd\u8981\u76ee\u5f55\u8fdb\u884c\u6587\u4ef6\u5220\u9664\u65f6\uff0c\u4e0d\u8981\u6025\u7740\u4f7f\u7528 tmpwatch \u547d\u4ee4!\u4e0d\u59a8\u5148\u770b\u770b\u547d\u4ee4\u8fd0\u884c\u4e4b\u540e\u5220\u9664\u7684\u6587\u4ef6\u6709\u54ea\u4e9b\uff0c\u4e0d\u7136\u5220\u9519\u4e86\u8111\u58f3\u53c8\u75bc\u4e86\u3002\u3002(\u517b\u6210\u4e00\u79cd\u597d\u4e60\u60ef!)<\/p>\n

\u53ef\u4ee5\u4f7f\u7528 -t \u8fdb\u5165\u6d4b\u8bd5\u6a21\u5f0f\uff1a<\/p>\n

tmpwatch -t 30 \/var\/log\/<\/pre>\n

CentOS 7 \u4e0b\u8f93\u51fa\uff1a<\/p>\n

removing file \/var\/log\/wtmp \r\nremoving directory \/var\/log\/ppp if empty \r\nremoving directory \/var\/log\/tuned if empty \r\nremoving directory \/var\/log\/anaconda if empty \r\nremoving file \/var\/log\/dmesg.old \r\nremoving file \/var\/log\/boot.log \r\nremoving file \/var\/log\/dnf.librepo.log <\/pre>\n

\u57fa\u4e8e Debian \u7684\u7cfb\u7edf\u4e0b\u8f93\u51fa\uff1a<\/p>\n

$ tmpreaper -t 30 \/var\/log\/ \r\n(PID 1803) Pretending to clean up directory `\/var\/log\/'. \r\n(PID 1804) Pretending to clean up directory `apache2'. \r\nPretending to remove file `apache2\/error.log'. \r\nPretending to remove file `apache2\/access.log'. \r\nPretending to remove file `apache2\/other_vhosts_access.log'. \r\n(PID 1804) Back from recursing down `apache2'. \r\n(PID 1804) Pretending to clean up directory `dbconfig-common'. \r\nPretending to remove file `dbconfig-common\/dbc.log'. \r\n(PID 1804) Back from recursing down `dbconfig-common'. \r\n(PID 1804) Pretending to clean up directory `dist-upgrade'. \r\n(PID 1804) Back from recursing down `dist-upgrade'. \r\n(PID 1804) Pretending to clean up directory `lxd'. \r\n(PID 1804) Back from recursing down `lxd'. \r\nPretending to remove file `\/var\/log\/\/cloud-init.log'. \r\n(PID 1804) Pretending to clean up directory `landscape'. \r\nPretending to remove file `landscape\/sysinfo.log'. \r\n(PID 1804) Back from recursing down `landscape'. \r\n[...] <\/pre>\n

\u4e0a\u9762\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u5176\u5b9e\u5e76\u6ca1\u6709\u771f\u6b63\u5220\u9664\u6587\u4ef6\uff0c\u53ea\u662f\u8fdb\u884c\u6a21\u62df\u5220\u9664\uff0c\u544a\u77e5\u4f60\u54ea\u4e9b\u6587\u4ef6\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n

\u5728\u786e\u4fdd\u8981\u5220\u9664\u7684\u6587\u4ef6\u90fd\u662f\u6b63\u786e\u7684\u65f6\u5019\uff0c\u65b9\u53ef\u53bb\u6389 -t \u9009\u9879\u518d\u6267\u884c tmpwatch \u8fdb\u884c\u771f\u6b63\u5220\u9664\u3002<\/p>\n

7. \u5f3a\u5236\u5220\u9664<\/strong><\/span><\/div>\n

tmpwatch \u9ed8\u8ba4\u4e0d\u4f1a\u5220\u9664\u5f53\u524d\u7528\u6237\u6ca1\u6709\u5199\u8bbf\u95ee\u6743\u7684\u6587\u4ef6\u3002\u4f46\u662f\u5982\u679c\u4f60\u5fc5\u987b\u8981\u5220\u9664\u90a3\u4e9b\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528-f \u9009\u9879\u8fdb\u884c\u5f3a\u5236\u5220\u9664\uff1a<\/p>\n

tmpwatch -f 10h \/var\/log\/<\/pre>\n
8. \u5220\u9664\u65f6\u8df3\u8fc7\u67d0\u4e9b\u6587\u4ef6<\/strong><\/span><\/div>\n

\u82e5\u60f3\u5728\u5220\u9664\u65f6\u4fdd\u7559\u6307\u5b9a\u7684\u6587\u4ef6\uff0c\u4e5f\u5c31\u662f\u8bf4\u5217\u5165\u767d\u540d\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528 --protect \u9009\u9879\u3002\u5047\u8bbe\u6211\u4eec\u8981\u4fdd\u7559\u6240\u6709 txt \u7c7b\u578b\u7684\u6587\u4ef6\uff1a<\/p>\n

tmpreaper --protect '*.txt' -t 10h \/var\/log\/ <\/pre>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

(PID 2623) Pretending to clean up directory `\/var\/log\/'. \r\n(PID 2624) Pretending to clean up directory `apache2'. \r\nPretending to remove file `apache2\/error.log'. \r\nPretending to remove file `apache2\/access.log'. \r\nPretending to remove file `apache2\/other_vhosts_access.log'. \r\n(PID 2624) Back from recursing down `apache2'. \r\n(PID 2624) Pretending to clean up directory `dbconfig-common'. \r\nPretending to remove file `dbconfig-common\/dbc.log'. \r\n(PID 2624) Back from recursing down `dbconfig-common'. \r\n(PID 2624) Pretending to clean up directory `dist-upgrade'. \r\n(PID 2624) Back from recursing down `dist-upgrade'. \r\nPretending to remove empty directory `dist-upgrade'. \r\nEntry matching `--protect' pattern skipped. `ostechnix.txt' \r\n(PID 2624) Pretending to clean up directory `lxd'. <\/pre>\n
\u8bbe\u7f6e cron job \u5b9a\u671f\u81ea\u52a8\u5220\u9664\u6587\u4ef6<\/strong><\/div>\n

(\u5077\u5077\u5730\u544a\u8bc9\u4f60\uff0ctmpwatch\/tmpreaper \u4e0e cron job \u4e00\u8d77\u98df\u7528\u66f4\u4f73\u54e6\u3002)<\/p>\n

\u8fdb\u5165 cron job \u4efb\u52a1\u7f16\u8f91\u7a97\u53e3\uff1a<\/p>\n

# crontab -e <\/pre>\n

\u6dfb\u52a0\u4e00\u4e2a\u5468\u671f\u4efb\u52a1\uff1a<\/p>\n

0 1 * * * \/usr\/sbin\/tmpwatch 30d \/var\/log\/ <\/pre>\n

\u4e0a\u9762\u7684\u4ee3\u7801\u8bbe\u7f6e\u4e86 tmpwatch \u6bcf\u5929\u51cc\u6668 1 \u70b9\u8fd0\u884c\uff0c\u5e76\u5220\u9664 30 \u5929\u4e4b\u524d\u7684\u6587\u4ef6\u3002<\/p>\n

\u4e0d\u4e86\u89e3 corn job \u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u4e0a\u7f51\u641c\u4e0b\u5b83\u7684\u521d\u5b66\u8005\u6307\u5357\u54c8\u3002<\/p>\n

\u5b89\u88c5 tmpreaper \u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u65e5\u5e38 cron job(\/etc\/cron.daily\/Tmpreaper)\u3002\u5b83\u4ece \/etc\/timereaper.conf \u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u5e76\u6267\u884c\u3002\u9ed8\u8ba4\u8bbe\u7f6e\u7684\u662f\u5220\u9664 7 \u5929\u4ee5\u524d\u7684\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 TMPREAPER.conf \u6587\u4ef6\u4e2d \u201cTMPREAPER_TIME=7d\u201d \u6765\u66f4\u6539\u8fd9\u9879\u8bbe\u7f6e\u3002<\/p>\n

\u5199\u5728\u6700\u540e<\/strong><\/div>\n

\u6700\u540e\u5728\u63d0\u9192\u4e00\u4e0b\uff0c\u5728\u5220\u9664\u6587\u4ef6\u7684\u65f6\u5019\u4e00\u5b9a\u8981\u4ed4\u7ec6\u68c0\u67e5\u597d\u8def\u5f84\uff0c\u4ee5\u514d\u6570\u636e\u4e22\u5931\u3002<\/p>\n

tmpwatch \u548c tmpreaper \u624b\u518c\u9875\uff1a<\/p>\n

$ man tmpwatch \r\n$ man tmpreaper<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u4e0b\u9762\u8981\u4ecb\u7ecd\u7684\u662f\u4eca\u5929\u7684\u4e3b\u89d2\u2014\u2014 tmpwatch \uff0c\u5b83\u80fd\u5e2e\u52a9\u6211\u4eec\u9012\u5f52\u5220\u9664\u5728\u7ed9\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u8bbf\u95ee\u7684\u6587\u4ef6\u548c\u7a7a\u76ee\u5f55\u3002 \u5f53 […]<\/p>\n","protected":false},"author":1920,"featured_media":173831,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-174410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=174410"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174410\/revisions"}],"predecessor-version":[{"id":174441,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174410\/revisions\/174441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/173831"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=174410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=174410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=174410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}