LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-25-2024, 02:14 PM   #1
Clutch2
LQ Newbie
 
Registered: Feb 2008
Location: Northern Michigan
Distribution: Debian on Raspberry Pi's
Posts: 14

Rep: Reputation: 0
I need a script to delete oldest files in directory to keep from using up all space


My security system sends alert video to a couple pi's in case the pc that does the monitoring is stolen. The problem is sometimes I space off and forget to delete files before I run out of storage.

Anyone have a script that I can run as a cron job to keep the directory contents under a specified size by deleting the oldest files. The directory right now is half full and has 6000 files in it to give you an idea.

Thanks,
 
Old 01-25-2024, 02:32 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
A job for logrotate
 
Old 01-25-2024, 02:54 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,729

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Or multilog…’tho log rotate is likely extant. multilog is not likely installed, unless OP is a DJB fan.

Last edited by scasey; 01-25-2024 at 02:56 PM.
 
Old 01-26-2024, 09:27 AM   #4
Clutch2
LQ Newbie
 
Registered: Feb 2008
Location: Northern Michigan
Distribution: Debian on Raspberry Pi's
Posts: 14

Original Poster
Rep: Reputation: 0
I found a script that works for me. It was on another site. Am I allowed to post a link to it, I don't want to break any rules here.
 
Old 01-26-2024, 09:32 AM   #5
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
Quote:
Originally Posted by Clutch2 View Post
I found a script that works for me. It was on another site. Am I allowed to post a link to it, I don't want to break any rules here.
yes, you can post that script.

use code tags.
 
Old 01-26-2024, 09:59 AM   #6
Clutch2
LQ Newbie
 
Registered: Feb 2008
Location: Northern Michigan
Distribution: Debian on Raspberry Pi's
Posts: 14

Original Poster
Rep: Reputation: 0
From askunbuntu. I added a check to make sure all three parameters were supplied. It seems to work for me.



Code:
#!/bin/bash
#Usage = sh limit-directory-size.sh /media/computer/mypartition 88 120 ("/media/computer/mypartition" = the directory to be limited / "88"=the percentage of the total partition this directory is allowed to use / "120"=the number of files to be deleted every time the script loops (while $Directory_Percentage > $Max_Directory_Percentage)

if [ $# -lt 3 ]; then
  echo 1>&2 "$0: not enough arguments"
  exit 2
elif [ $# -gt 3 ]; then
  echo 1>&2 "$0: too many arguments"
  exit 2
fi
# The three arguments are available as "$1", "$2", "$3"

#Directory to limit
Watched_Directory=$1
echo "Directory to limit="$Watched_Directory

#Percentage of partition this directory is allowed to use
Max_Directory_Percentage=$2
echo "Percentage of partition this directory is allowed to use="$Max_Directory_Percentage

#Current size of this directory
Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 )
echo "Current size of this directory="$Directory_Size

#Total space of the partition = Used+Available
Disk_Size=$(( $(df $Watched_Directory | tail -n 1 | awk '{print $3}')+$(df $Watched_Directory | tail -n 1 | awk '{print $4}') ))       
echo "Total space of the partition="$Disk_Size

#Curent percentage used by the directory
Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}')
echo "Curent percentage used by the directory="$Directory_Percentage

#number of files to be deleted every time the script loops (can be set to "1" if you want to be very accurate but the script is slower)
Number_Files_Deleted_Each_Loop=$3
echo "number of files to be deleted every time the script loops="$Number_Files_Deleted_Each_Loop

#While the current percentage is higher than allowed percentage, we delete the oldest files
while [ $Directory_Percentage -gt $Max_Directory_Percentage ] ; do
    #we delete the files
    find $Watched_Directory -type f -printf "%T@ %p\n" | sort -nr | tail -$Number_Files_Deleted_Each_Loop | cut -d' ' -f 2- | xargs rm
    #we delete the empty directories
    find $Watched_Directory -type d -empty -delete
    #we re-calculate $Directory_Percentage
    Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 )
    Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}')
done
 
Old 01-26-2024, 10:17 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,799

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
In the current directory leave the newest 5000 files and delete all the older ones:
Code:
ls -t . | sed '1,5000d' | xargs -rd '\n' rm
Quick and not too dirty. But does not consider the disk space.

Note that

xargs -rd '\n' rm

safely handles filenames with space characters, in contrast to just

xargs rm

Please correct that in your script!

Last edited by MadeInGermany; 01-26-2024 at 10:30 AM.
 
2 members found this post helpful.
Old 01-26-2024, 11:33 AM   #8
Clutch2
LQ Newbie
 
Registered: Feb 2008
Location: Northern Michigan
Distribution: Debian on Raspberry Pi's
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you for the improvement. I edited the script.
 
Old 01-26-2024, 11:57 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,719

Rep: Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911Reputation: 5911
It depends on how much time you might want to spend to simplify the script.

Without knowing anything about your security system, files are usually named using a timestamp of YYYYMMDD... so they naturally sort without using their real timestamp. I also assume the alert is a certain number of seconds so I would guess their size is fairly the same. If true you should be able to calculate the number of files to delete and delete them using just one rm command.

logrotate may not be enough in this case because the number of files per day is random.

On the other hand, I would guess you really only need to keep a certain number of days of files so that would still keep it more efficient.

Last edited by michaelk; 01-26-2024 at 12:20 PM.
 
Old 02-06-2024, 09:25 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd say logrotate should be able to handle it, see eg https://opensource.com/article/21/10/linux-logrotate.
Just select a reasonable min num of files to keep. (ie the newest N files) using the 'rotate' option.
The rest gets deleted automatically unless otherwise specified eg call a script to scp them ....

Obviously try it on a test machine (create dummy file sets to test) until you're sure you have all the correct settings.
 
Old 02-06-2024, 09:42 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,663
Blog Entries: 4

Rep: Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944Reputation: 3944
“logrotate” will already be in your crib tab sequence for other reasons and it is quite a general-purpose tool. It (or one of its brethren) should be ideal for this. Simply edit the configuration file.
 
Old 02-07-2024, 09:09 AM   #12
jmccue
Member
 
Registered: Nov 2008
Location: US
Distribution: slackware
Posts: 691
Blog Entries: 1

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
I am trying to understand your request, so I will rephrase:

You want to delete the oldest 'n' files no matter the how old? For example, if all your files that directory are less than say 1 day old, you want to delete the 'n' oldest files ?

If so, I would say MadeInGermany's should be fine.

But since this directory is a kind of security directory, I would suggest you delete all files older that a specified number of days.

For example, this will delete files with a mod date older than 2 days. This will allow you to resend files if the pi person said they did not get today's file(s).

Code:
find YOURDIR -type f -mtime +2 -exec rm '{}' \;

Last edited by jmccue; 02-07-2024 at 09:12 AM. Reason: grammer
 
Old 02-09-2024, 08:46 AM   #13
Clutch2
LQ Newbie
 
Registered: Feb 2008
Location: Northern Michigan
Distribution: Debian on Raspberry Pi's
Posts: 14

Original Poster
Rep: Reputation: 0
I'm keeping alert video clips as long as I can given storage space on the machine I'm sending a copy to. The script I posted uses a percentage of storage and deletes in age order as I use up that percentage.

At this time I consider my problem solved.

Thanks ever one for the various approaches.
 
Old 02-09-2024, 10:57 AM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,799

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
If something else fills up your disk, would your script delete all your alert video clips?
Maybe a second criterium makes sense, for ex only consider +2 days old files for deletion.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting three oldest files if 7 days have passed since the oldest riahc3 Linux - Newbie 7 05-15-2017 08:39 AM
Shell Script to delete oldest folders in particualr directory prathamesh7478 Linux - Newbie 19 02-06-2017 08:33 AM
Shell script for deleting the oldest directory within a directory kauuttt Linux - Newbie 6 07-14-2013 05:11 AM
[ncftp] how to delete oldest directory and file using ncftp commands booyeeka Linux - Newbie 3 08-17-2010 11:23 AM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 03:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration