shell刪除文件

1.刪除7天前的文件 

find /usr/local/nginx/logs/ -mtime +7 -name "*-access.log" -exec rm -rf {} \;

先找到文件夾:/usr/local/nginx/logs/

文件名字:-name "*-access.log"

7天前:-mtime +7

注意:雖然我的文件名稱是日期,但是刪除的7前天的文件,這個查找不是按照文件名稱來查找的,是按照創建日期來查找的

2.循環刪除指定文件夾中的子文件夾中的7天前文件

#!/bin/sh
#============ get the file name ===========
Folder_A="/home/wwwroot/default/web.shen021.com/logs"
for file_a in ${Folder_A}/*
do
    temp_file=`basename $file_a`
    find ${Folder_A}/$temp_file/ -mtime +7 -name "20*" -exec rm -rf {} \;
done

 

 

 

 

#!/bin/bash
#########################
#刪除7天之前的文件      #
#2019年6月15日18:12:26  #
#########################
path=/opt/teach/shell/project/test/
find $path -type f  -mtime +7 | xargs rm -rvf
#find $path -type f -mtime +3 -exec rm -rvf {} \;
#這裏的{} 實際上是將前面的結果套進去使用 \換行 ;結束一條命令 xargs以空格爲定界符


#find常規用法
#find path -option -exec shll
#找到名稱以test開頭的文件,並且列出大小
#find /opt -name "test*" -type f -exec ls -lh {} \;
#找到3天之前的文件
find /opt -mtime +3
#找到3天以內的文件
find /opt -mtime -3
#找到3天以上4天以內的文件
find /opt -mtime 3
#找到大小超過15M的文件
find /opt -size 15m -type f
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章