1830979 Members
2018 Online
110018 Solutions
New Discussion

find and cp -p

 
SOLVED
Go to solution
Zulfikri
Advisor

find and cp -p

Hi all,

I need to write a shell script to find files created in a /backup folder starting on 0000 hrs until current.

I also would like to copy the result into a different folder ie /dump

The script so far that I have is as below:

#!/bin/ksh
WorkingDir=/export/home/b2b_supp/aperak
Date=`date +%m%d`
DateN=$Date
echo "$DateN""0000" > $WorkingDir/mydate.txt
DateW=`cat $WorkingDir/mydate.txt`
touch -m $DateW $WorkingDir/datefile.txt
find /opt/cyclone/data/TOPSMAL01/backup -newer $WorkingDir/datefile.txt -name *out >> $WorkingDir/todayfiles_msia.lis

How do I cp -p the files found into another folder?

Thanks in advace

Zul
Beware GATE$ of HELL!!
12 REPLIES 12
Elmar P. Kolkman
Honored Contributor
Solution

Re: find and cp -p

Your construction with the DATE vars seems a bit compicated. Why not just:

touch -mt ${DATE}0000 $WorkingDir/datefile.txt

As for the cp -p, you could do it like this:
find /opt/cyclone/data/TOPSMAL01/backup -newer $WorkingDir/datefile.txt -name '*out' -exec cp -p {} \;

Or you could use the lis file you create and use cpio or pax to copy the files.

A problem you might encounter, depending on where you like the files to be copied too, is the path the files have. Since you do the find with the absolute path, the complete directory will be copied. A better way might be to do a change directory to your backup directory and do a find from '.' .
Every problem has at least one solution. Only some solutions are harder to find.
Leif Halvarsson_2
Honored Contributor

Re: find and cp -p

Hi,
pipe the output to cpio

find ...... |cpio -pdvmux


Zulfikri
Advisor

Re: find and cp -p

Hi all,

Thanks for the kind input. Now I need to do some cleanup of the files that I copied into the /dump folder.

The file content would look like below as single line:-

UNA:+.? 'UNB+UNOA:3+9554000001105:14+3020476101404:14+031205:1313+23054++++0'UNH+2305400001+APERAK:D:96A:UN:EAN002'BGM+12E::9+33596+6'DTM+137:20031204:203'RFF+ON:142260085N'UNT+5+2305400001'UNZ+1+23054'

I only interested in the value after BGM+12E::9+ and before +6'DTM+137 which is 3359. I know that sed can do this but I could not get the correct command/syntax....

Again...many thanks in advance

Zul
Beware GATE$ of HELL!!
Mark Grant
Honored Contributor

Re: find and cp -p

How about

expr "`cat myfike`" : ".*BGM+12E::9+\(.*\)+6'DTM+137.*"
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: find and cp -p

obviously "myfyke" was supposed to say "myfile" and is the name of the file you have your data in :)
Never preceed any demonstration with anything more predictive than "watch this"
Jean-Louis Phelix
Honored Contributor

Re: find and cp -p

Hi

grep BGM file | sed 's;.*BGM+[^+]*+\([^+]*\)+.*;\1;'

But the returned value is 33596 ...

Regards
It works for me (© Bill McNAMARA ...)
Zulfikri
Advisor

Re: find and cp -p

Hi Mike,

I got the output using expr "`cat a51290TOPSMAL01out`" : ".*BGM+12E::9+\(.*\)+6'DTM+137.*"

I have about 500 files of a*out in that folder, so to run thru all files, what should I do in a better way?

Usually what I'do would be:-

#! /bin/ksh
ls file* >> file.lis
exec 3< file.lis

while read line <& 3
do
expr "`cat $line`" : ".*BGM+12E::9+\(.*\)+6'DTM+137.*" >> output.txt

done

Is there any better way? Sorry...I'm a newbie but like to learn....;-)

thanks again

Zul
Beware GATE$ of HELL!!
Jean-Louis Phelix
Honored Contributor

Re: find and cp -p

You can simply use :

grep BGM file* | sed 's;.*BGM+[^+]*+\([^+]*\)+.*;\1;'

Regards.
It works for me (© Bill McNAMARA ...)
Elmar P. Kolkman
Honored Contributor

Re: find and cp -p

I would go for the sed solution. And since you are not interested in filenames, I would do it like this:

cat file* | grep BGM | sed 's|BGM+12E::9+\(.*.\)+6\'DTM+137.*|\1' >> output
Every problem has at least one solution. Only some solutions are harder to find.
Mark Grant
Honored Contributor

Re: find and cp -p

#! /bin/ksh

for i in a*out
do
expr "`cat $i`" : ".*BGM+12E::9+\(.*\)+6'DTM+137.*" >> output.txt
done

or try Jean-Louis' option (above)
Never preceed any demonstration with anything more predictive than "watch this"
Zulfikri
Advisor

Re: find and cp -p

Hi ppl,

Many many thanks for the help. (I'm almost there!!..yippee)
Now I got a new request from the pointy head boss to find all files created from ie
12/04/03 0000hrs to 12/05/03 0100hrs, itead of 12/04/03 0000hrs to 12/04/03 2359

I cant think a way to do that except have to create 2 scripts.

May I ask for some suggestions.

best regards,

Zul
Beware GATE$ of HELL!!
john korterman
Honored Contributor

Re: find and cp -p

Hi,
if you create a file, e.g. file_begin with a last mod. time of 12/04/03 00:00 and the file file_end with a last mod. time of 12/05/03 01:00, this should work:

# find -type f \( -newer file_begin \) -a \( ! -newer file_end \) -exec ls -ltr {} \;

regards,
John K.

it would be nice if you always got a second chance