Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

For loop to accept params and delete folder/files

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SIMMS7400
SIMMS7400's Unix or Linux Image
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 19 June 2018, 4:15 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
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:




Code:
Purge () {

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


I would be passing in paths such as:




Code:
/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!
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SIMMS7400
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 4:36 AM EDT
Location: Aachen, Germany
Posts: 12,916
Thanks: 429
Thanked 3,966 Times in 3,647 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SIMMS7400
SIMMS7400's Unix or Linux Image
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 19 June 2018, 4:15 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
Thank you, Rudi. Error on my part, the age should be "6", not "0".


Here is the log for this portion:




Code:
+ 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





Code:
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...
    #4  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SIMMS7400
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 4:36 AM EDT
Location: Aachen, Germany
Posts: 12,916
Thanks: 429
Thanked 3,966 Times in 3,647 Posts
"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.
Sponsored Links
    #5  
Old Unix and Linux 6 Days Ago   -   Original Discussion by SIMMS7400
SIMMS7400's Unix or Linux Image
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 19 June 2018, 4:15 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
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:




Code:
#::-- 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:




Code:
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:




Code:
/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!

Last edited by SIMMS7400; 6 Days Ago at 06:49 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Request for Shell script to move files from Subfolder to Parent folder and delete sub folder srivarun15 Shell Programming and Scripting 6 1 Week Ago 03:24 PM
need to navigate into specified folder and delete the files Nithz Shell Programming and Scripting 6 10-03-2011 02:30 AM
delete all the files in folder a which exist in folder b viv1 Shell Programming and Scripting 6 07-28-2009 05:29 AM
How can i delete files in folder by date? kobibn UNIX for Dummies Questions & Answers 9 10-25-2007 06:04 PM
delete all folders/files and keep only the last 10 in a folder melanie_pfefer Shell Programming and Scripting 3 11-17-2006 02:33 PM



All times are GMT -4. The time now is 05:32 AM.