Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set liunx to delete files regularly

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to set liunx to delete files regularly", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to set liunx to delete files regularly" can help you solve your doubts.

Liunx deletes files regularly

Linux is a system that automatically generates files, such as logs, emails, backups, etc. Although the hard drive is cheap now, we can have a lot of hard disk space for these files to waste, so it is a pleasant thing for the system to clean up some unneeded files regularly. You don't have to worry about whether you need to clean the log every day, and you don't have to receive alarm messages about insufficient hard disk space every day. If you want to have a good rest, let's leave this to the machine to carry out regularly.

1.find corresponding directory-mtime + days-name "file name"-exec rm-rf {}\

Find / opt/soft/log/-mtime + 30-name "* .log"-exec rm-rf {}\

Description:

Delete all files in the / opt/soft/log/ directory with ".log" 30 days ago. The specific parameters are as follows:

Find:linux search command, the user looks for files with specified conditions

/ any directory that opt/soft/log/: wants to clean up

-mtime: standard sentence writing method

+ 30: find files from 30 days ago, where the number of days is represented by numbers.

"* .log": the type of data you want to find. "* .jpg" means to find all files with the extension jpg, and "*" means to find all files. This can be used flexibly.

-exec: fixed writing method

Rm-rf: force deletion of files, including directories

{}\;: fixed writing, a pair of curly braces + spaces +\ +

two。 Scheduled tasks:

If it is too troublesome to execute the statement manually each time, you can write this small statement into an executable shell script file, and then set cron to schedule execution, then you can let the system automatically clean up the relevant files.

2.1Create a shell:

Touch / opt/soft/bin/auto-del-30-days-ago-log.sh

Chmod + x auto-del-30-days-ago-log.sh

Create a new executable file auto-del-30-days-ago-log.sh and assign runnable permissions

2.2 Edit the shell script:

Vi auto-del-30-days-ago-log.sh

Edit the auto-del-30-days-ago-log.sh file as follows:

#! / bin/sh

Find / opt/soft/log/-mtime + 30-name "* .log"-exec rm-rf {}\

Ok, save and exit (: wq).

2.3 scheduled tasks:

# crontab-e

Add the auto-del-30-days-ago-log.sh execution script to the scheduled task of the system and execute it automatically at the point.

Enter:

100 * / opt/soft/log/auto-del-7-days-ago-log.sh

The setting here is to perform the auto-del-7-days-ago-log.sh file data cleaning task at 00:10 every morning.

After reading this, the article "how to set liunx to delete files regularly" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report