Home > Enterprise >  Keep files number by deleting old files?
Keep files number by deleting old files?

Time:10-27

I want to keep my files based on the date it was modified from GitHub not the local file when i clone my repo... I am bit stuck how to do that at the moment, any help would be appreciated..

#!/bin/bash
LIMIT=30
NO=0
NUMBER=$(find logs/ -name "*.json" |wc -l)
if [[ $NUMBER -gt $LIMIT ]]  #if number greater than limit
 then
    del=$(($NUMBER-$LIMIT))
   if [ "$del" -lt "$NO" ]
    then
     del=$(($del*-1))
   fi
   echo $del
   FILES=$(ls -dt logs/*.json| tail -n "$del" | xargs rm)
  git rm -f "${FILES[@]}"
  #delete the originals
  git commit -m "Deleting old files"
 fi

CodePudding user response:

You can use

git log --name-only --pretty=format: --since="1 month ago" --until="1 day ago"

to get the list of files that were modified in the last month. You can use

git log --name-only --pretty=format: --since="1 month ago" --until="1 day ago" | xargs rm

to remove them. You can use

git commit -m "Deleting old files"

to commit the changes.

You can use

git log --name-only --pretty=format: --since="1 month ago" --until="1 day ago" | xargs rm && git commit -m "Deleting old files"

to do all of the above in one command.

  • Related