------------------------------------------------------------------------------------------------------------------
保留最近7天的文件-------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python
# coding=utf-8
import os
import datetime
import logging
def delete_old_files(directory):
# 获取当前的日期和时间
current_time = datetime.datetime.now()
# 设置删除的时间阈值 -- 7天
delete_before = current_time - datetime.timedelta(days=1)
# 创建日志文件
log_file = "/root/log_file.log"
logging.basicConfig(format='%(asctime)s %(message)s', filename=log_file, level=logging.INFO)
# 遍历指定目录下的所有文件和文件夹
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
# 获取文件的创建时间
created_time = datetime.datetime.fromtimestamp(os.path.getctime(file_path))
# 判断文件的创建时间是否大于阈值
if created_time < delete_before:
os.remove(file_path)
logging.info("Deleted file: {}".format(file_path))
if __name__ == "__main__":
path_list = {"/usr/local/1", "/usr/local/2"}
for directory_path in path_list:
delete_old_files(directory_path)
------------------------------------------------------------------------------------------------------------------
保留最近3个文件夹-------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python
# coding=utf-8
import os
import glob
import shutil
import logging
def delete_old_folder(folder_path):
# 列出文件夹中的所有文件夹
folders = glob.glob(os.path.join(folder_path, "*"))
# 创建日志文件
log_file = "/root/delete_warbak.log"
logging.basicConfig(format='%(asctime)s %(message)s', filename=log_file, level=logging.INFO)
# 对文件夹按创建时间进行排序
folders.sort(key=os.path.getctime)
# 保留最新的三个文件夹
folders_to_keep = folders[-3:]
# 删除除最新的三个文件夹外的所有文件夹及其内部文件
for folder in folders:
if folder not in folders_to_keep:
shutil.rmtree(folder)
logging.info("Deleted folder: {}".format(folder))
if __name__ == "__main__":
# 指定目录路径和要保留的文件数量
path_list = {"/usr/local/1", "/usr/local/2"}
#num_files_to_keep = 3
# 调用删除旧文件函数
for folder_path in path_list:
delete_old_folder(folder_path)
~
------------------------------------------------------------------------------------------------------------------
删除临时文件------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
#!/bin/bash
# coding=utf-8
# 指定要删除文件的目录列表
directory_list=(
"/usr/local/1"
"/usr/local/2"
"/usr/local/3"
)
# 删除目录列表中的所有文件
function delete_files() {
for directory in "${directory_list[@]}"; do
rm -rf "$directory"/*
echo "Deleted files in ${directory}"
done
}
# 调用删除文件函数
delete_files
------------------------------------------------------------------------------------------------------------------
保留最近3个文件---------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python
# coding=utf-8
import os
import glob
import shutil
import logging
def delete_old_files(directory, num_files_to_keep=3):
# 获取目录下的所有文件,并按修改时间从新到旧排序
files = glob.glob(os.path.join(directory, "*"))
files.sort(key=os.path.getmtime, reverse=True)
# 创建日志文件
log_file = "/root/delete_files_warbak.log"
logging.basicConfig(format='%(asctime)s %(message)s', filename=log_file, level=logging.INFO)
# 删除除最新的 num_files_to_keep 个文件外的其他文件
for i in range(num_files_to_keep, len(files)):
file = files[i]
os.remove(file)
logging.info("Deleted file: {}".format(file))
if __name__ == "__main__":
# 指定目录路径和要保留的文件数量
path_list = {"/usr/local/1", "/usr/local/2", "/usr/local/3"}
#directory = '/root'
num_files_to_keep = 3
# 调用删除旧文件函数
for directory in path_list:
delete_old_files(directory, num_files_to_keep)