Solved

Using Wildcards for Path with Unix Find Command

Posted on 2010-09-01
6
568 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks! I guess I just need to remove that extra \* and everything works as expected!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

7 Experts available now in Live!

Get 1:1 Help Now