1748140 Members
3657 Online
108758 Solutions
New Discussion юеВ

Find and I need help

 

Find and I need help

Greetings all

hear is what I'm trying to do, creat a scripts that moves any files what are
more than 7 days old to a history dir

hear is my script.

NDAYS="7"
ARDIR="/u/if/archive"
HSDIR="/u/if/history"
find $ARDIR -xdev -type f -mtime +$DAYS -exec mv {} $HSDIR \;

end of scripts

could someone tell me what i did wronf with find.

cause the only thing it moved the files but it takes the file out of the
archive dir and then moves it to /u/if with the file name of history ???? whats
up with that.....

thanx

william
8 REPLIES 8
mary_76
New Member

Re: Find and I need help

try the following change
HSDIR="/u/if/history/"
a unix person
New Member

Re: Find and I need help

quote the variables thus ${varname}
the mv will then execute successfully
William_198
New Member

Re: Find and I need help

The Vars that you want me to quote with {} is that the var on the command line
or when i set them??

william
William_198
New Member

Re: Find and I need help

Mary,

I had tried this aswell and i ened up with the same results

thanx thou

william
Pawel Wozniak
New Member

Re: Find and I need help

Hi

I think that directory pointed by $HISDIR
did not exist when you run he script for the first time.
So mv treated $HISDIR as target file to be created.
And repeated the same for each found file.

You need to:
1. delete file, now pointed by $HISDIR
2. create directory pointed by $HISDIR
3. run the script again

I guess it helps.

Pawel
William_198
New Member

Re: Find and I need help

Greetings, nope, i did have the dir created that is what stumps
me,.....interesting

william

PS does anyone know a good HP-UX book on scripts and sercrets etc???

william
David Blankenship
New Member

Re: Find and I need help

The best shell programming book I have found is

Hands-On KornShell93 Programming by Barry Rosenberg.

I like this book because it has many many examples and it explains why certian
commands should no longer be used.

I found this book at Borders for around $45.

Take a look at it and see if it fits your needs.
Alan Riggs_1
Regular Advisor

Re: Find and I need help

If the directory from which you are moving files is flat, then you can make the
mv command work by properly bracing the variable names in the find command:

find ${DIR} -type f -exec mv {} ${NEW_DIR} \;

However, this will flatten any subdirectories under your original structure.
To preserve them, I have used a two-stage process in the past:

DIR=/tmp/archive
TARG=/tmp/history
cd $DIR
find . -type f |cpio -pdumx ${TARG}
find . -type f -exec rm {} \;

And, yes, I know there is a cpio flag on find, but I have experienced
unexpected results from it in the past and prefer the flexibility and control
of the real thing. There is probably a more elegant way to do this, but this
works.