linux implements automatic deletion of the oldest files in detail

  • 2020-06-15 11:00:19
  • OfStack

preface

Because the required project war package is stored on the company's file server, after a period of time, a large number of unused project files take up disk space, and useless files need to be deleted regularly. These files belong to different project directories and are created at different times. Some old projects have not been updated for years. Therefore, you can't just delete files in a directory on a regular basis, and you can't delete files days before by time. Without further ado, let's start with the detailed implementation process.

advance

Count the number of folders in the directory
ls -l | grep '^d' | wc -l
Here, grep '^d' Is the statistics folder, if the statistics file number, use grep '^-' List the two oldest files
ls -tr | head -2 | xargs Delete all files
rm -rf *

Completion of the


#!/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'
# The trunk directory of the project 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
 # Determines whether the directory exists 
 if [ $? -eq 0 ];
 then
  echo $releasepath
  echo "Contains file:"
  # Output everything 
  echo *
  num=`ls -l | grep '^d' | wc -l`;
  # Determine if the number of folders exceeds 5 I just want to keep the latest 5 Folders) 
  if [$num -gt 5 ];
  then
   # Computing more than 5 How much a 
   num=`expr $num - 5`
   clean=`ls -tr | head -$num | xargs`
   echo "will delete file:"
   echo ${clean}
   #-n1  Each processing 1 A file 
   ls -tr | head -$num | xargs -i -n1 rm -rf {}
  fi
 fi
done

perfect

In the actual deployment, I think it is not appropriate to delete files directly. I should first backup to a certain directory, and then delete the last backup files when I run the script again.


#!/bin/bash
array[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 the backup file 
BACKUP="/tmp/storage/"
cd $BACKUP
if [ $? -eq 0 ];
then
 rm -rf *
fi

# Remove more than 5 The oldest files outside of twenty 
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`
   clean=`ls -tr | head -$num | xargs`
   echo "will delete file:"
   echo ${clean}
   # It is safer to move the files to the backup folder 
   ls -tr | head -$num | xargs -i -n1 mv {} $BACKUP
  fi
 fi
done

Automatic processing

Save the above script as ES27en.sh, then add crontab.


crontab -e 

Add the following line to run once a month:


0 0 1 * * /opt/project/removecode.sh > /opt/project/remove.log 2>&1 &

conclusion


Related articles: