Why the for loop for a single $1 parameter without wild cards? Why the removal of today's and older files when you want 6+ days? Why the /* appended to the ${_DIR} ?
Please post an execution log after setting the -x option.
"still doesn't work" is NOT something that people trying to help can build on, and thus doesn't answer my first question. And, don't switch topic / code / targets between posts - first you wanted to remove files, now it's directories.
Some hints:
rmdir fails on non-empty directories.
are folders older than 6 days around, at all?
find can take multiple starting directories - no need for a for loop.
Here is what I arrived at with your help of the definitions in your last response.
The reason I"m using a for loop is so I can check what path is being passed in as certain files/directories have different retention requirements.
So, at the end of my script(s), I'd put this:
#::-- End of Processing - Purge & Logout --::#
#::-- Add all paths in double quotes seperated by a space that should be purged --::#
#::-- PLEASE KEEP the outside set of double quotes as shown --::#
_DLIST=""${_INTRAPATH}" "${_ERRORINTRAPATH}""
Purge; Logout "0"
For this purpose ( & script) I only need to pass in the log and error directories since nothing major happens. However, for data integration scripts, data exports, the _DLIST variable would be inclusive of many more.
Then my function looks like such:
Purge () {
for _DIR in $_DLIST; do _LD="$(basename "${_DIR}")";
#::-- If basename directory is in YYYY_MMDD format, strip off all + 1 --::#
if [[ $_LD == *"${_YEAR}_"* ]]; then _DIR="${_DIR::${#_DIR}-10}"; fi
#::-- Directions: --::#
#::-- Add relevant if statements using basename directory --::#
#::-- Add value to _AGE variable indicating purge folder/files older than X days --::#
#::-- If basename directory is in "YYYY_MMDD" format, use one folder up --::#
#::-- If directory is "/u01/Cloud_Automation/Applications/APP/Files/PBCS_Reset_Service_Logs/2018_0615
# use "_Logs" [as long as it's unique - otherwise use full name] for filter in if statement --::#
if [[ $_DIR == *"_Logs"* ]] || [[ $_DIR == *"_Errors"* ]]; then _AGE="14"; fi
#::-- [fail-safe] will only execute if _DIR & _AGE are defined --::#
if [ -n "$_DIR" ] && [ -n "$_AGE" ]; then
find "$_DIR" -mtime +$_AGE -print0 | xargs -0 rm -rf
fi
done
}
As an example, for this purpose, the following to paths are passed in:
If the baseline folder is detected as YEAR_MONTHDAY format, I strip that off first before purging or else it wouldn't hit the other YEAR_MONTHDAY folders in the /PBCS_Reset_Service_*/ directory.