?
Solved

Using Wildcards for Path with Unix Find Command

Posted on 2010-09-01
6
Medium Priority
?
592 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
ATEN's HDBaseT Presentation at InfoComm 2017

Hear ATEN Product Manager YT Liang review HDBaseT technology, highlighting ATEN’s latest solutions as they relate to real-world applications during her presentation at the HDBaseT booth at InfoComm 2017.

 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 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