Solved

Using Wildcards for Path with Unix Find Command

Posted on 2010-09-01
6
583 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help instaling linux on vmware 36 88
Linux neworking 4 115
Strongswan setup to Cisco ASA Site to site 5 481
FTP output from Wireshak 6 98
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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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