Operating System - HP-UX
1854752 Members
10798 Online
104102 Solutions
New Discussion

Need script help with "find" command

 
SOLVED
Go to solution
Debbie Fleith
Regular Advisor

Need script help with "find" command

I need to create a shell script to move files beginning with "M." in a directory that are older than "today's date", no matter what time, into a history directory (leaving only today's files in the main directory).

I thought I had a good start with the following find statement:
===========
find /main_dir \( -name 'M.*' \) -mtime +1 -exec mv {} /main_dir/hist \;
===========
But the -mtime +1 doesn't move yesterday's dated files.

How can I accomplish what I need?
11 REPLIES 11
A. Clay Stephenson
Acclaimed Contributor

Re: Need script help with "find" command

Hi Debbie:

Change your +1 to +0. Also, you MAY want to restrict to regular files only; int that case add the -type f argument as well.

It is always a good idea to replace a potentially hazardous command with an innocuous command like -exec echo {} \; before doing the real thing.

If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: Need script help with "find" command

Use +0 instead in the find command.
Helen French
Honored Contributor

Re: Need script help with "find" command

Hi,

Use '+0' instead of '+1'

find . -mtime +0 ......

HTH,
Shiju
Life is a promise, fulfill it!
James R. Ferguson
Acclaimed Contributor

Re: Need script help with "find" command

Hi Debbie:

Yet another useful method is to use the '-newer' argument with a reference file that you create with a date and time to granularize your returns:

# touch -mt 200202081648 /tmp/ref
# find /dir -type f -newer /tmp/ref

Regards!

...JRF...
Debbie Fleith
Regular Advisor

Re: Need script help with "find" command

Thanks all for the mtime +0 advice, it is now moving yesterday's files OK.

However, the script is also trying to move files I've already moved to the /main/hist directory again, and I'm getting errors that I'm trying to mv files on top of each other...how do I just move files directly under /main and not further subdirectories down?
Craig Rants
Honored Contributor

Re: Need script help with "find" command

You may want to date stamp the movement of your files and thus you could be more specific with you find and moves. i.e...

DATE=`date +%m%d%y`

mv filename filename.$DATE

thus when you do searches you could key of the date and only move files without a date stamp.

Just a thought,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Darrell Allen
Honored Contributor
Solution

Re: Need script help with "find" command

Hi Debbie,

To add to the "newer" idea...

"-mtime +0" will tell find to select files older than 24 hours ago. If you run that find command at 3pm you will miss all files created yesterday between 3pm and midnight.

To get all files older than today, use a reference file and "not" logic with the "-newer" arg:

touch `date +%m%d`0000 /tmp/ref
find /main ! -newer /tmp/ref

Use the appropriate "-name" args to get the files you want. Also note /tmp/ref would be selected by "! -newer" if it was in /main.

To keep from moving files in subdirectories of /main, you could capture find's output to a file, use grep, awk, sed, or whatever to select only files in /main itself. But since you want files that have an earlier date than today and if files don't have a future date you could do:

DTE=`date '+%b %e'`
for file in `ll /main | grep -v "$DTE" | grep -v total | grep ^- | awk '{print $NF}'`
do
mv /main/$file /main/hist/$file
done

This selects regular files (grep ^-), omits ll's total line, and omits files with todays date. Of course, files dated a year or years ago today would be omitted also.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Kenny Chau
Trusted Contributor

Re: Need script help with "find" command

Hi Debbie,

You can use this script:

find /main_dir \( -name 'M.*' \) -mtime +1 -print > /tmp/tmpfile
while read file
do
file1=`echo $file | cut -d/ -f3`
if [ "$file1" != "hist" ] ; then
mv $file /main_dir/hist/$file1
fi
done < /tmp/tmpfile

Hope this helps.
Kenny.
Kenny
Steven Sim Kok Leong
Honored Contributor

Re: Need script help with "find" command

Hi,

>> ...how do I just move files directly under /main and not further subdirectories down?

Use the -prune option in find.

If the current entry is a directory, prune will cause find to skip that directory. This can be useful to avoid walking certain directories, or to avoid recursive loops.

Hope this helps. Regards.

Steven Sim Kok Leong
harry d brown jr
Honored Contributor

Re: Need script help with "find" command

Debbie,

find /main_dir \( -name 'M.*' \) -mtime +0 |
grep -v "\/main_dir\/hist" |
xargs -i mv {} /main_dir/hist


live free or fir
harry
Live Free or Die
Steve Steel
Honored Contributor

Re: Need script help with "find" command

Hi


For your information

The -mtime option is based upon

"-1" last 24 hours,
"1" between 24 and 48 hours
"+1" 48 or more hours old.

the full explanation of the argument n is
in the man page under expressions and your
HP-UX Reference at www.docs.hp.com .


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)