Home Man
Today's Posts

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

👤 Login to reply

Thread Tools Search this Thread
# 1  
Old 06-14-2018
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" 
        find "${_DIR}"/* -mtime +0 -exec rm {} \;

I would be passing in paths such as:


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!
# 2  
Old 06-15-2018
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.
# 3  
Old 06-15-2018
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"
        find ${_D} -mtime +6 -type d -exec rmdir {} \;

I only have 1 param for now, my intent is to make it more dynamic once I get the delete syntax down...
# 4  
Old 06-15-2018
"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.
# 5  
Old 06-15-2018
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 --::#
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

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.

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

Last edited by SIMMS7400; 06-15-2018 at 06:49 PM..
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06-13-2018 03:24 PM
Loop awk command on files in a folder dovah UNIX for Dummies Questions & Answers 3 11-06-2015 10:23 AM
Script to delete files in different folder yprudent UNIX for Dummies Questions & Answers 1 07-22-2012 08:42 AM
For loop for number of files in a folder chillblue Shell Programming and Scripting 2 07-05-2012 07:45 AM
loop through files in each folder and perform sed ymeyaw Shell Programming and Scripting 2 11-22-2011 03:09 AM
need to navigate into specified folder and delete the files Nithz Shell Programming and Scripting 6 10-03-2011 02:30 AM
Delete files in a folder with a specific ending kylle345 Shell Programming and Scripting 5 12-18-2009 12:47 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 01:09 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
Show Password