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 delete a log from n days ago in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to delete the log of n days ago in Linux, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Delete File command:

Find corresponding directory-mtime + days-name "file name"-exec rm-rf {}\

Instance command:

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.shchmod + 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/shfind / 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 > / dev/null 2 > & 1

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

After completing the above three steps, you will no longer worry about whether the hard disk space is full every day, it is time to clean up the log files, and will no longer receive the alarm message that the server's hard disk space is insufficient. Feel free to read a book and drink coffee.

After each task is added, be sure to restart the crond service, otherwise it will not take effect

The code is as follows:

Service crond restart

Create your first Shell script

1. Write a script

A) create a file with the touch command: touch my_script

B) Open the my_script file with the vim editor: vi my_script

C) use the vim editor to edit the my_script file as follows:

#! / bin/bash tells shell what program to use to interpret the script # My first scriptls-l. *

two。 Allow Shell to execute it

Chmod 755 my_script

3. Execute my_script script

. / my_script

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

On how to delete the log of n days ago in Linux to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Servers

Wechat

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

12
Report