#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0

    Post Script to go Into Directories and Find/Delete files


    I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have posted, and have found help in others posts as well. Please see below shell code I have started. I want to go to the ORDATA which is the variable for /opt/data in this case directory, find the folders with most stuff in it and cd into each one and then cd into the backup folder and do a couple of those find commands.

    How do I make it go out of the first directory and into the next and so on and so forth? I know i can use the while loop in some capacity, but not quite sure.

    for example /opt/data/ has 4 directories /mpw, /docprep, /edfiles, /vgdata and under each of those directories is a /backup directory as well, I want to start at /opt/data, go into each the 4 directories, go into the backup directories and do those find and delete commands i came with until its went into all 4 backup directories.

    Below is what I have so far.


    Code:
    #!/bin/ksh
    #Author:Emmanuel Iroanya Jr
    #Date:April 2nd, 2013
    #Purpose: The purpose of this is to check data and rollout directory and keep file space optimal. 
    
    if [ "$1x" = "x" ]
    	then
    	echo "You must supply an environment parameter to this script"
    	echo "example:  $0 tb82"
    	exit
    fi
    
    . /opt/origenate/$1/config/usrconfig.sh 	#Sources Environments Admin Config Variables
    
    cd $ORDATA 								#Environments Data Storage Directory
    du -sk */ | sort -n -r | head -n 10 |awk '{print $2}' |while read d
    do 
    	cd "${d}" 							
    	cd backup
    	find *.zip -type f -a -mtime +3 -exec rm {} \;
    	find *.pdf -type f -a -mtime +3 -exec rm {} \;
    	find *.CSV -type f -a -mtime +3 -exec rm {} \;
    	find *.txt -type f -a -mtime +3 -exec rm {} \;
    done
    Last edited by gkelly1117; April 3rd, 2013 at 09:09 AM. Reason: Bad title
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    857
    Rep Power
    388

    Cool


    Originally Posted by gkelly1117
    How do I make it go out of the first directory and into the next and so on and so forth? I know i can use the while loop in some capacity, but not quite sure.
    Try this:
    Code:
    . . .   E t c   . . . 
    cd $ORDATA 								#Environments Data Storage Directory
    du -sk */ | sort -n -r | head -n 10 |awk '{print $2}' |while read d
    do 
    	cd $ORDATA/${d}/backup
    	find *.zip -type f -a -mtime +3 -exec rm {} \;
    	find *.pdf -type f -a -mtime +3 -exec rm {} \;
    	find *.CSV -type f -a -mtime +3 -exec rm {} \;
    	find *.txt -type f -a -mtime +3 -exec rm {} \;
    done
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    Thanks for all that helped,

    I got it to work to a close enough standard that I would be able to run it daily.

    Thanks for the help.

    final code:

    Code:
    #!/bin/ksh
    #Author:Emmanuel Iroanya Jr
    #Date:April 3rd, 2013
    #Purpose: The purpose of this is to check the $ORDATA directory and keep file space optimal. 
    
    if [ $# -lt 2 ];
    	then
    	echo "You must supply an environment parameter and OrigID to this script"
    	echo "example:  $0 or_tb86 dlrfund"
    	exit
    fi
    
    . /opt/orig/$1/config/usrconfig.sh 
    
    cd $ORDATA 							
    du -sk */ | sort -n -r | head -n 10 |awk '{print $2}' |while read d
    do 
    	find ${d}/backup ${d}/tmp ${d}/output ${d}/$2/ -type f -mtime +5 '(' -name '*.zip' -o -name '*.CSV' -o -name '*.txt' -o -name '*.pdf' -o -name '*.control' -o -name '*.xml' ')' -exec echo rm -f {} \;
    done

IMN logo majestic logo threadwatch logo seochat tools logo