For loop to accept params and delete folder/files

Hi Folks -

I'm trying to build a simple for loop to accept params and then delete the folder & files on the path older than 6 days.

Here is what I have:

Purge () {

    for _DIR in "$1" 
    do
        find "${_DIR}"/* -mtime +0 -exec rm {} \;
    done
}

I would be passing in paths such as:

/home/oracle/Cloud_Automation/Applications/APPNAME/Logs/PBCS_Reset_Service_Logs
/home/oracle/Cloud_Automation/Applications/APPNAME/Errors/PBCS_Reset_Service_Errors

I'm not sure why my find command isn't working.

Version - GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

Thank you!

Not too clear. What exactly goes wrong?

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.

Thank you, Rudi. Error on my part, the age should be "6", not "0".

Here is the log for this portion:

+ Purge /home/oracle/Cloud_Automation/Applications/APPNAME/Logs/PBCS_Reset_Service_Logs
+ for _D in '"$1"'
+ find /home/oracle/Cloud_Automation/Applications/APPNAME/Logs/PBCS_Reset_Service_Logs -mtime +6 -type d -exec rmdir '{}' ';'

Here is my amended for loop - still doesn't work. I've tried with and without the "/" "/*" etc etc

Purge () {

    for _D in "$1"
    do
        find ${_D} -mtime +6 -type d -exec rmdir {} \;
    done
}

I only have 1 param for now, my intent is to make it more dynamic once I get the delete syntax down...

"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.

Hi RudiC -

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:

/home/oracle/Cloud_Automation/Applications/APPNAME/Logs/PBCS_Reset_Service_Logs/2018_0615
/home/oracle/Cloud_Automation/Applications/APPNAME/Errors/PBCS_Reset_Service_Errors/2018_0615

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.

Let me know if I'm way off base here - thanks!