User Script: Delete Files older than x Days and Empty Folder


Recommended Posts

Just want to share this User Script to delete Files and Folders older than 64 Days (change my_days).

 

I use it to delete old Surveillance Footage my IP-Cameras are transferring via FTP to my UNRAID Server.

 

#!/bin/bash

#name=Delete old surveillance recordings
#description=Delete files older than x days and empty folders
#arrayStarted=true

# Days to keep
my_days=64
# Folder to clean
my_dir=/mnt/user/ipcam/

# Check if directory exist and quit if not
if [ ! -d "$my_dir" ]; then
    echo ERROR: $my_dir does not exist
    exit 2
fi

# Delete files older than x days
find $my_dir/garten/* -type f -maxdepth 9999 -mtime +$my_days -delete
# Delete empty folders
find $my_dir/garten/* -type d -empty -delete

find $my_dir/garten2/* -type f -maxdepth 9999 -mtime +$my_days -delete
find $my_dir/garten2/* -type d -empty -delete

find $my_dir/haustuer/* -type f -maxdepth 9999 -mtime +$my_days -delete
find $my_dir/haustuer/* -type d -empty -delete

echo Total Files: $(find $my_dir -type f | wc -l)
echo Total Size:  $(du -hs $my_dir | awk '{ print $1; }')

exit 0

 

EDIT: find Documentation https://www.gnu.org/software/findutils/manual/html_mono/find.html

Edited by pixeldoc81
Update
  • Like 1
Link to comment
  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.