Reputation: 3
I've been tasked with creating a bash script to implement on our company's Asterisk servers which should do the following:
Search through the /var/spool/asterisk/monitor/
dir and its sub directories.
Find files of type .wav and .mp3 and remove these if their time stamp exceeds 60 days
Remove any empty directories thereafter the files have been removed.
Here is my code:
#!/bin/bash
set -e
ASTDIR=/var/spool/asterisk/monitor/; #VAR to point to asterisk dir
find ${ASTDIR}. -type f -name '*.wav' -mtime +60 -exec rm {} \;#remove wav > 60 days
find ${ASTDIR}. -type f -name '*.mp3' -mtime +60 -exec rm {} \;#remove mp3 > 60 days
find ${ASTDIR} -type d -empty -delete ; # Delete any empty directories
exit;
The problems I'm experiencing with this however, are plenty. I feel like my logic is solid but the script just doesn't seem to generate the output I would like/expecting.
The amount of files is quite large so its understandable that find would sometimes take a long time to run.
I've ran the command a few times with debugging on, with output like the following:
+ find /var/spool/asterisk/monitor/. -type f -name '*.wav' -mtime +60 -exec rm '{}' ';'
It executes and perpetually hangs as far as I can see, I decided impatience may be an issue so I decided to let it sit, I came back later to see that it had supposedly finished running however the mp3 files which were older than 60 days hadn't been removed, leading me to believe that the find command did not actually run.
I then swapped the find wav command with find mp3 which took a while to run, and did not delete everything.
I am so confused to what I may be doing wrong?
With both find --> rm commands not working, I commented them out to attempt the final find command which would delete empty directories
EVEN THIS did not delete all empty directories. I ran my script in one window and ran watch -n1 ls -l | head -n20
in a window opposite it, clearly some empty dirs got deleted, but not all of them. I confirmed this because I knew which directories should get deleted but the command still left some empty directories behind!
Cherry on top, please shed some light on my dilemma!
This is CentOS server.
UPDATE
Editing the script to look as follows, solved my problems for me:
#!/bin/bash set -e
cd /var/spool/asterisk/monitor
find . -type f -name '*.mp3' -mtime +60 -delete
find . -type f -name '*.wav' -mtime +60 -delete
find . -type d -empty -delete
Upvotes: 0
Views: 82
Reputation: 42716
You're searching in ${ASTDIR}.
(note the appended dot) which probably doesn't exist. Try this instead:
#!/bin/bash
astdir=/var/spool/asterisk/monitor/
find "$astdir" -type f -iname '*.wav' -mtime +60 -delete
find "$astdir" -type f -iname '*.mp3' -mtime +60 -delete
find "$astdir" -type d -empty -delete
You should always quote variables in case there are spaces in them, there's no need for semicolons at the end of the lines, find
has a built-in capability to delete files, and you don't need to exit
from a script. I also used the iname
expression to ensure a case insensitive search; my Asterisk installation creates *.WAV
files.
You can do boolean statements with find as well using -or
and -and
but it requires grouping with parentheses that need to be escaped so probably just easier to run multiple commands:
find "$astdir" \( -type f \( -iname '*.mp3' -or -iname '*.wav' \) -mtime +60 \) -or \( -type d -empty \)
Upvotes: 1