Sponsored Content
Top Forums UNIX for Beginners Questions & Answers For loop to accept params and delete folder/files Post 303018815 by SIMMS7400 on Friday 15th of June 2018 06:44:16 PM
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:


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; 06-15-2018 at 07:49 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

2. UNIX for Dummies Questions & Answers

How can i delete files in folder by date?

Hi, I have some files on a folder and i want to delete all the files that were created on July. Thanks, Kobi. (9 Replies)
Discussion started by: kobibn
9 Replies

3. Shell Programming and Scripting

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 Replies

4. Shell Programming and Scripting

Delete files in a folder with a specific ending

Hi I have files that end with .txt.txt that i want to delete. But I also have files that end with .txt that I want to leave intact. How do I specifically delete files that end with .txt.txt in a folder. thanks (5 Replies)
Discussion started by: kylle345
5 Replies

5. Shell Programming and Scripting

need to navigate into specified folder and delete the files

Hi all, I need to write a script to naviagate into the list of specified folder and delete the files in it. I have mentioned the list of folders in a external file so it can be reusable. The issue is am facing now is that, i am not understating on how to navigate into the folder locations... (6 Replies)
Discussion started by: Nithz
6 Replies

6. Shell Programming and Scripting

rsync delete specific files - from different target folder

Hi, I need to use rsync to delete multiple files(only specified files not all) using --delete option, these files are located in different target folders. Instead of running rsync command multiple times for each file, can we achieve this with one time execution? your help is much... (0 Replies)
Discussion started by: MVEERA
0 Replies

7. Shell Programming and Scripting

For loop for number of files in a folder

Hi All, Need a for loop which should run for number of files in a folder and should pass the file name as parameter to another shell script for each loop. Please help me. Thanks. (2 Replies)
Discussion started by: chillblue
2 Replies

8. UNIX for Dummies Questions & Answers

Script to delete files in different folder

Hi Experts, i need a little help. i have different folder that contain files that need to be deleted. but those folder contains huge amoung of same with 3 different extention. what i used to do is to delete them using the rm commande rm *.ext *.ext1 *.ext3 what i want to do is to have... (1 Reply)
Discussion started by: yprudent
1 Replies

9. Shell Programming and Scripting

Script to delete files in a folder older than 2 days

hi i need a script to delete the files older than 2 days... if my input is say in a folder versions A_14122012.txt A_15122012.txt A_16122012.txt A_17122012.txt i want my output to be A_16122012.txt A_17122012.txt thanks in advance hemanth saikumar. (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

10. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies
rmf(1)							      General Commands Manual							    rmf(1)

NAME
rmf - remove folder (only available within the message handling system, mh) SYNOPSIS
rmf [+folder] [-help] [-[no]interactive] OPTIONS
Prints a list of the valid options to this command. Asks for confirmation before deleting a folder. By default, rmf deletes a folder and its messages without asking for confirmation. If you specify the -interactive option, rmf asks if you are sure before deleting the folder. You are advised to use this option, since when rmf deletes a folder its contents are lost irretrievably. DESCRIPTION
The rmf command removes all of the messages within the current folder, and then removes the folder itself. If there are any files within the folder which are not part of MH, they are not removed, and an error message is displayed. You can specify a folder other than the current folder by using the +folder argument. If you do not specify a folder, and rmf cannot find the current folder,rmf asks you whether you want to delete +inbox instead. If the current folder is removed, it makes +inbox current. Note that the rmf command irreversibly deletes messages that do not have other links, so use it with caution. If the folder being removed is a sub-folder, the parent folder becomes the new current folder, and rmf tells you that this has happened. This provides an easy mechanism for selecting a set of messages, operating on the list, then removing the list and returning to the current folder from which the list was extracted. Using rmf to delete a read-only folder deletes the private sequence and current message information from the file, without affecting the folder itself. If you have sub-folders within a folder, you must delete all the sub-folders before you can delete the folder itself. PROFILE COMPONENTS
Path: To determine the user's Mail directory EXAMPLES
This example shows how rmf asks for confirmation when the -interactive option is used: % rmf -interactive +test Remove folder "test"? y FILES
The user profile. SEE ALSO
rmm(1) rmf(1)
All times are GMT -4. The time now is 07:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy