Solved

Using Wildcards for Path with Unix Find Command

Posted on 2010-09-01
6
574 Views
Last Modified: 2012-08-13
I am trying to create a script that will go into a directory /ce_raid/To_Be_Produced_preMEDIA/*FolderName*/DONE and purge its contents based on age. The DONE folder will always be located under the primary folder, but the primary folder name changes based on client. Any idea how to get that to work??? What I have tried fails, see below. :-( Thanks!

#!/bin/sh
# Scriptname:  TBP_Done.sh
# Written by:  ME
# Date Updated:  09/01/10
# Purpose: Cleanup script for the To_Be_Produced_preMEDIA Done folders.

# Set some variables
DATE=`date | awk '{print $1$2$3$6}'`
HEADERDATE=`date | awk '{print $1,$2,$3,$6}'`
LOGFILE=/var/log/TBP_Done/${DATE}.log
TBP_DONE="/ce_raid/X_Admin_Share/Tina_Purge"

# Create LOGFILE and input header
touch $LOGFILE
echo "TBP_Done cleanup log"  >> $LOGFILE
echo "Date: $HEADERDATE" >> $LOGFILE
echo " " >> $LOGFILE

# Cleanup of the TBP_Done Directory
echo "Cleanup of TBP_Done" >> $LOGFILE

# This deletes files and empty directories older than 30 days.
find "${TBP_DONE}/*/DONE/*" -type f -mtime +30 -exec ls -l {} \; >> $LOGFILE
find "${TBP_DONE}/*/DONE/*" -type f -mtime +30 -exec rm -f {} \;
find "${TBP_DONE}/*/DONE/*" -empty -type d -mtime +30 -exec ls -l {} \; >> $LOGFILE
find "${TBP_DONE}/*/DONE/*" -empty -type d -mtime +30 -exec rm -rf {} \;

echo "Completed cleaning TBP_Done" >> $LOGFILE
0
Comment
Question by:tmbolton
6 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 33578716
How about:
for f in ${TBP_DONE}/*/DONE/* ; do find $f -type f -mtime +30 -exec ls -l {} \; >> $LOGFILE ; done;

Open in new window

0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 33578727
Sorry.. It should be: (No /* after DONE)
for f in ${TBP_DONE}/*/DONE ; do find $f -type f -mtime +30 -exec ls -l {} \; >> $LOGFILE ; done;

Open in new window

0
 

Author Comment

by:tmbolton
ID: 33579536
I get an error when I run the modified script below.

find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

Am I missing something?

#!/bin/sh
# Scriptname:  TBP_Done.sh
# Written by:  Bolton
# Date Updated:  09/01/10
# Purpose: Cleanup script for the To_Be_Produced_preMEDIA Done folders.

# Set some variables
DATE=`date | awk '{print $1$2$3$6}'`
HEADERDATE=`date | awk '{print $1,$2,$3,$6}'`
LOGFILE=/var/log/TBP_Done/${DATE}.log
TBP_DONE="/ce_raid/X_Admin_Share/Tina_Purge"

# Create LOGFILE and input header
touch $LOGFILE
echo "TBP_Done cleanup log"  >> $LOGFILE
echo "Date: $HEADERDATE" >> $LOGFILE
echo " " >> $LOGFILE

# Cleanup of the TBP_Done Directory
echo "Cleanup of TBP_Done" >> $LOGFILE

# This deletes files and empty directories older than 30 days.
for f in ${TBP_DONE}/*/DONE ; do find $f -type f -mtime +30 -exec ls -l {} \; >> $LOGFILE ; done;
for f in ${TBP_DONE}/*/DONE ; do find $f -type f -mtime +30 -exec  rm -f {} \; done;
#find "${TBP_DONE}/Folder1/DONE/" -type f -mtime +30 -exec ls -l {} \; >> $LOGFILE
#find "${TBP_DONE}/Folder1/DONE/" -type f -mtime +30 -exec rm -f {} \;
#find "${TBP_DONE}/Folder1/DONE/" -empty -type d -mtime +30 -exec ls -l {} \; >> $LOGFILE
#find "${TBP_DONE}/Folder1/DONE/" -empty -type d -mtime +30 -exec rm -rf {} \;

echo "Completed cleaning TBP_Done" >> $LOGFILE ; done;
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 33579768
find is capable of processing more than one directory.

So why not

find ${TBP_DONE}/*/DONE  -type f -mtime +30 -exec ls -l {} \; >> $LOGFILE

It's kind of an implicit loop ...

wmp
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33580418
Let's simplify the script a bit
#!/bin/sh

DATE=`date +%a%b%d%Y`
HEADERDATE=`date "+%a %b %d %Y"`
LOGFILE=/var/log/TBP_Done/$DATE.log
TBP_DONE=/ce_raid/X_Admin_Share/Tina_Purge

exec 1>$LOGFILE
echo "TBP_Done cleanup log"
echo "Date: $HEADERDATE"
echo

echo "Cleanup of TBP_Done"

# This deletes files and empty directories older than 30 days.
find $TBP_DONE/*/DONE/* -type f -mtime +30 -exec rm -f {} \; -ls
find $TBP_DONE/*/DONE/* -empty -type d -mtime +30 -exec rmdir {} \; -ls

echo "Completed cleaning TBP_Done"

Open in new window

0
 

Author Closing Comment

by:tmbolton
ID: 33580856
Thanks! I guess I just need to remove that extra \* and everything works as expected!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now