I have some backup files which are created in a daily manner. After some time they take a lot of space and want to delete them according to their time stamp like delete 1 month olders. Thanks
Gabriel9999 3 Newbie Poster
Dani AI
Generated
Daily backups quickly consume space; the first step is agreeing what "1 month" means and picking a safe, repeatable method. showed a quick command-line prune and provided a scripted Windows solution. The takeaway: one-liners are fast but easy to misapply (for example the numeric example shown equals roughly six months, not one), while a small script or a backup-aware tool is safer for regular automation.
Choose a clear retention policy: either a rolling window (30 days) or a calendar/monthly policy. For production, consider a simple rotation like keeping the last 7 daily, 4 weekly and 12 monthly backups (GFS). Decide whether to select by filesystem modification time (mtime) or by parsing timestamps embedded in filenames — archive creation methods can alter mtime, so parsing names can be more reliable when available.
Operational safety checklist: always run a dry-run that only lists candidates, test the process in a sandbox folder, do not run destructive commands as root, and keep at least one offsite copy until pruning is proven. For recurring pruning prefer backup-aware tools (rsnapshot, borg, restic, duplicity) that implement pruning/forget policies natively and avoid race conditions. If a simple custom script is preferred, schedule it via cron or a systemd timer and capture stdout/stderr to a log file.
Example (dry-run) shell outline that avoids the one-liner approach and works on both GNU and BSD date/stat. Replace the final echo with a safe delete only after verification.
# list files older than 30 days (dry-run)
cutoff=$(date -d '30 days ago' +%s 2>/dev/null || date -v -30d +%s)
for f in /path/to/backups/*; do
[ -f "$f" ] || continue
mtime=$(stat -c %Y "$f" 2>/dev/null || stat -f %m "$f" 2>/dev/null)
[ -n "$mtime" ] && [ "$mtime" -lt "$cutoff" ] && echo "$f"
done
# once verified, replace 'echo "$f"' with 'rm -f -- "$f"' (careful) Always verify listed files before deleting and keep a tested restore procedure in place.
Recommended Answers
Jump to Post— Reverend Jim 6,665I had to set up a batch file to do this years ago in vbScript. Now I would write it in Python but here is the original. I suggest you try it in a test folder to make sure it does what you want.
' ' ' Name: ' ' …
All 2 Replies
drewgen 0 Newbie Poster
Heym
Have you tied:
find /your/folder -mtime +182
When you ready to delete use this (be careful)
find /your/folder -mtime +182 -exec rm {} +
Please be careful!
Reverend Jim 6,665 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
I had to set up a batch file to do this years ago in vbScript. Now I would write it in Python but here is the original. I suggest you try it in a test folder to make sure it does what you want.
' '
' Name: '
' '
' delete_older_than.vbs '
' '
' Description: '
' '
' This script deletes all files more than the given number of days old. '
' '
' Usage: '
' '
' delete_older_than ## [folder] [-r] [-d] '
' '
' ## is a number of days. '
' folder is the folder (current by default) to do the deletes in '
' -d delete (default is list files to be deleted but don't delete them) '
' -r recursively delete in all subfolers '
' '
' Note: '
' '
' You are not asked to verify the delete so use caution. '
' '
' Audit: '
' '
' 1999-10-06 rj original code '
' '
set wso = CreateObject("Wscript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
set arg = Wscript.Arguments
'check the command line arguments
if arg.count = 0 then
howto = array( _
"delete_older_than ## [folder] [-r]", _
"", _
" ## files older than this number of days are deleted", _
" folder root folder (current by default) to do the deletes in", _
" -d delete the files (default is list files to be deleted)", _
" -r recursive delete on all sub-folders", _
"", _
" You are not asked to verify the delete so use caution")
wscript.echo join(howto,vbcrlf)
wscript.quit
end if
'set defaults
numdays = -1
recurse = false
delete = false
folder = fso.GetAbsolutePathName(".")
'get any values specified on the command line
for i = 0 to arg.count - 1
select case true
case isnumeric(arg(i))
numdays = cint(arg(i))
case arg(i) = "-r"
recurse = true
case arg(i) = "-d"
delete = true
case else
folder = arg(i)
end select
next
'check for missing or invalid (less than zero) numdays parameter
if numdays < 0 then
wscript.echo "You didn't specify the number of days"
wscript.quit
end if
'determine the cutoff date (the oldest date retained)
cutoff = cstr(DateAdd("d",-numdays,date()))
'clean up the given folder
call CleanFolder(folder,cutoff,recurse,delete)
' This function does the actual deleting. It was made into a separate function so that '
' it could be called recursively if the -r option was selected. '
sub CleanFolder (ByVal foldername, ByVal cutoff, ByVal recurse, ByVal delete)
dim fold 'the current folder object '
dim file 'for stepping through the files collection '
dim files 'the files collection within a folder '
dim folder 'for stepping through the subfolders collection '
dim folders 'the subfolders collection within a folder '
'get a collection of all files in the folder
set fold = fso.GetFolder(foldername)
set files = fold.files
'process all the files in the folder
for each file in files
datestamp = left(file.datelastmodified,10)
if datestamp < cutoff then
if delete then
Wscript.Echo "deleting", file.DateLastModified, file.Path
fso.DeleteFile file.Path
else
Wscript.Echo "listing", file.DateLastModified, file.Path
end if
end if
next
'process all the subfolders in the folder
if recurse then
set folders = fold.subfolders
for each folder in folders
call CleanFolder(folder.path,cutoff,recurse,delete)
next
end if
end sub Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.