Two ways to delete outdated files in Linux are described in detail

Source: Internet
Author: User

I. Overview
In the actual C software development project, different software will generate files in different directories, due to the limited storage space of the disk, developers have to consider the deletion of outdated files in the directory. Generally speaking, there are two ways to delete outdated files, one is implemented in C programs, and one is implemented using crontab. In this paper, the specific implementation of the two methods are described in detail.
For illustrative purposes, the expired file in this article is suffixed with. C, which is stored in the/home/zhou/zhouzx/test directory and expires for 1 days.

second, in the C program to achieve outdated file deletion
In this approach, the main issues to consider are:
(1) How long do you want to delete files that expire? Where are the files stored? The file store path can be used to view the configuration of the file generation module, to see which directory the files are generated in, and how long the deletion expires is usually determined by the requirements.
(2) The document will not be extended at the same time, but at different times of the extended period, to this, how to deal with the program? Our idea is to delete a file that has been extended over the current time period, and then to perform the delete task after hibernation for a period of time, which is equivalent to periodically performing a delete task.

The complete C program code is as follows:

/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: DELEXPIREDFILES.C* File ID: None* Content Summary: Create live Delete expired files* Other instructions: None* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20150729***********************************************************************/#include <stdio.h>#include <stdlib.h>#include <time.h>#include <sys/time.h>Redefine data type typedef signed INT int32;typedef unsigned int uint32;typedef unsigned char uint8;//function declaration void Sleep (UIN T32 Icountms); void Delexpiredfiles (UINT8 *pszfilesavedir, UINT32 ifilesavedays); INT32 main ();*********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* -------------------------------------------------------------------* 20150729 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {UINT8 szfilesavedir[1024] = {0};UINT32 ifilesavedays = 0;//Get outdated Files directorysnprintf (Szfilesavedir, sizeof (Szfilesavedir)-1, "%s/zhouzx/test/", getenv ("HOME"));//Get expired file retention daysifilesavedays = 1;While (1)    {printf ("Now, begin to delete expired files.\n");//Call function to delete expired files that meet the criteriadelexpiredfiles (Szfilesavedir, ifilesavedays);Sleep (5 * 60 * 1000); After 5 minutes of rest, continue .printf ("Sleep 5 minutes, then go ahead.\n");    }return 0;}/*********************************************************************** Function Description: Program hibernation* Input parameters: icountms-sleep time (unit: MS)* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ------------------------------------------------------------------* 20150729 V1.0 Zhou Zhaoxiong created********************************************************************/void Sleep (UINT32 Icountms) {struct Timeval t_timeout = {0};if (Icountms <)    {t_timeout.tv_sec = 0;t_timeout.tv_usec = Icountms *;    }Else    {t_timeout.tv_sec = icountms/1000;t_timeout.tv_usec = (icountms%) *;    }Select (0, NULL, NULL, NULL, &t_timeout); Call the Select function to block the program}/*----------------------------------------------------* Function Description: Delete Overdue expired files* Input parameters: pszfilesavedir-expired File Save directoryifilesavedays-expired File Save days* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ----------------------------------------------------* 20150729 V1.0 Zhou Zhaoxiong created----------------------------------------------------*/void Delexpiredfiles (UINT8 *pszfilesavedir, UINT32 ifilesavedays) {UINT8 szcmdbuf[1024] = {0};if (Pszfilesavedir = = NULL)    {printf ("Delexpiredfiles:input parameter is null!\n");return;    }//Execute shell command to delete expired files (the suffix of the expired file is. c)snprintf (szcmdbuf, sizeof (SZCMDBUF)-1, "Find%s-name \" *.c\ "-ctime +%d-exec rm-f {} \ \", Pszfilesavedir, IF Ilesavedays);system (SZCMDBUF);printf ("delexpiredfiles:exec command [%s] to delete expired files successfully!\n", szcmdbuf);}

third, the use of crontab to achieve outdated file deletion
With the crontab command, we can execute a specified system instruction or shell script at a fixed interval of time. With this feature, we can store instructions for deleting outdated files in a shell script, and then use the crontab command to periodically invoke the script to perform the delete operation.
In the crontab file, each line represents a task, each field of each row represents a setting, its format is divided into six fields, the first five is the time setting segment, the sixth paragraph is the command segment to execute, each field is separated by a space, the useless paragraph is replaced by *, the format is as follows:

minute   hour   day   month   week   command

which
Minute: Represents minutes, which can be any integer from 0 to 59.
Hour: Represents the hour, which can be any integer from 0 to 23.
Day: Represents a date, which can be any integer from 1 to 31.
Month: Represents the month, which can be any integer from 1 to 12.
Week: Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
Command: The commands to execute can be system commands or script files (such as shell files) that you write yourself.

The following are the steps:
1. Write a shell script to delete expired files, named del_expiredfiles.sh, stored in the/home/zhou/zhouzx/test directory. The contents of the file are:

#!/bin/kshFilepath="/home/zhou/zhouzx/test"days=1################################################################################################################################################################Main () {Find $FILEPATH-name"*.C"-ctime + $DAYS-type f-exec rm-rf {} \;}# # Execute main functionMain $*

2. Log in to the root user, execute the crontab–e command into the crontab file, and add the following to the file:

01/home/zhou/zhouzx/Test/del_expiredfiles.sh -D1

In contrast to the format of the tasks in the crontab file, we can see that the specific meaning of the additions is that the del_expiredfiles.sh file under the/home/zhou/zhouzx/test/directory is executed 1 o'clock every day.

3. Save the changes to the crontab file and observe the results of the command execution.

Iv. Summary
The above two methods of deleting outdated files are used in the actual software development projects, we should choose the appropriate method according to the actual situation of the project team.

My public number: ZHOUZXI, please scan the following two-dimensional code:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Two ways to delete outdated files in Linux are described in detail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.