Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:
#!/bin/bash
FILEAGE=15
#Array of folders to clean
dir_array=(
"/srv//folderA"
"/srv/level1D//folderA"
)
#function to be used for deleting files. Needs to be called with a path
function dir_delete() {
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""
}
echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "${dir_array[@]}";do
if [ -d $d ];then
echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d
else
echo "##### Did not find directory: \$d #####"
echo ""
fi
done
My script is returning that it can't find the directories like so:
Did not find directory: /srv/*/folderA
Note: There will be files throughout these folders under 15 days that can't be deleted.
Update
Using @Kusalananda suggestion '/srv/'*'/folderA'
fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.
find /path/*/to/places
. The easiest option for me off the top of my head would befor rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
. – DopeGhoti Apr 12 '19 at 18:26*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it? – Kusalananda Apr 12 '19 at 18:53find
does support multiple search paths, but the*
is expanded in the wrong place. It should be expanded in the array, not in the execution of thefind
command. – Kusalananda Apr 12 '19 at 18:54