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 the oldest files automatically by linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces linux how to automatically delete the oldest files, the article is very detailed, has a certain reference value, interested friends must read it!

Push

Count the number of folders under the directory

Ls-l | grep'^ d' | wc-l

The grep'^ d 'here is the statistics folder. If you count the number of files, use grep' ^ -'

List the two oldest documents

Ls-tr | head-2 | xargs

Delete all files

Rm-rf *

Finish the job

#! / bin/bash# different projects have different paths array [0] = 'project1'array [1] =' project2'array [2] = 'com/project3'array [3] =' com/phase/project4'array [4] = 'project5'array [5] =' com/stor/sproject6'# project backbone directory is the same RELEASE= "/ opt/devapps/nexus/sonatype-work/nexus/storage/release/" for path in ${array [@]} Do # splice file path releasepath=$ {RELEASE} ${path} cd $releasepath # determine whether the directory if [$?-eq 0]; then echo $releasepath echo "Contains file:" # output all the contents echo * num= `ls-l | grep'^ d' | wc-l`; # determine whether the number of folders exceeds 5 (I only want to keep the latest 5 folders) if [$num-gt 5] Then # calculate the number of more than 5 num= `expr $num-5`expr `ls-tr | head-$num | xargs`echo "will delete file:" echo ${clean} #-N1 one file at a time ls-tr | head-$num | xargs-I-N1 rm-rf {} fi fidone

Perfect

In the actual deployment, it is inappropriate to delete files directly. It should be backed up to a certain directory first, and then delete the last backed up files when you run the script again.

#! / bin/basharray [0] = 'project1'array [1] =' project2'array [2] = 'com/project3'array [3] =' com/phase/project4'array [4] = 'project5'array [5] =' com/stor/sproject6'RELEASE= "/ opt/devapps/nexus/sonatype-work/nexus/storage/release/" # empty backup file BACKUP= "/ tmp/storage/" cd $BACKUPif [$?-eq 0] Then rm-rf * fi# removes the oldest files other than 5 files: for path in ${array [@]}; do releasepath=$ {RELEASE} ${path} cd $releasepath if [$?-eq 0]; then echo $releasepath echo "Contains file:" echo * num= `ls-l | grep'^ d' | wc-l`; if [$num-gt 5] Then num= `expr $num-5`xargs`ls-tr | head-$num | xargs`echo "will delete file:" echo ${clean} # it's safer to move files to a backup folder ls-tr | head-$num | xargs-I-N1 mv {} $BACKUP fi fidone

Automatic processing

Save the above script as removecode.sh, and then add crontab.

Crontab-e

Add the following line to run once a month:

0 01 * * / opt/project/removecode.sh > / opt/project/remove.log 2 > & 1 & the above is all the contents of the article "how to automatically delete the oldest files by linux". Thank you for reading! Hope to share the content to help you, more related knowledge, 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

Servers

Wechat

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

12
Report