0

I have a game server. It auto-gets players’ screenshots and stores at /home/gameserver/serverfiles/screenshots.

I want to delete old screenshots when it reaches 1000. I mean if it’s 1000 screenshots, when a new screenshot comes, it auto-deletes the 1000th screenshot.

How can I do that, which scripts can I use? Can anyone give suggestion or codes?

Note: I am using Ubuntu 18.04 and my game server user hasn't sudo access.

I want to maintain my screenshot folder to hold max 1000 images.

αғsнιη
  • 36,350
GHOST
  • 357

1 Answers1

3

Assuming format to be any of the jpg, jpeg and png you can write a short script which counts and deletes all files if the count is >=1000. A short working example would be:

#!/bin/bash

count=`ls -l *.{jpg,jpeg,png} | wc -l`

if [ $count -gt 1000 ]
then 
    echo "Deleting old 1000 image files"
    for i in $(ls -lt *.{jpg,jpeg,png} | head -n 1000)
    do
        rm $i
    done
fi

You can then add this script to crontab to execute it every 10 minutes(for example). Type crontab -e to edit it.