Solved

Using Wildcards for Path with Unix Find Command

Posted on 2010-09-01
6
579 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
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.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 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