Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with find

Status
Not open for further replies.

minus0

Programmer
Feb 20, 2003
73
US
Is it possible to execute two commands as part of a find command?

for ex:

find . -name "whatever" -mtime +1 -exec command1 {} destination -exec command2 {} ;
my intention is to find files satisfying the criteria, perform a command say copy the found files and then
perform a second command say removing the found files in one shot.
I know mv can be used instead of cp and then rm but those were just examples of two commands

thanks for any suggestions

 
Minus0:

Another common technique is to pipe the output of the find command to a where clause and work on each object found:

find . -print|while read object
do
echo $object
done

Regards,


Ed
 
Iribach,

thanks for the tip, but if i wish to do something like
archive,zip and then remove the original files then do you think a

for file in `find ....`
do cmd1 ; cmd2
done

would be ideal? I was thinking of
find . -name "Xfiles" -exec tar cvf newfile.tar gzip newfile.tar rm Xfiles

Olded, can you give me the syntax to do what you suggested to suit my need?

Thanks a bunch for your help
 
Minus0;

The examples Iribach and I presented actually are good if you want to perform an operation one object at a time. That won't work with an archive.

I think you're trying to do too much with one find command - especially when performing something like tar. All the file objects have to be gathered before executing tar; tar has to complete before compressing the tar file, etc. I'd find all your objects and place them in a temp file (or a named pipe if you want to get fancy):

PIPE=/tmp/pfifo.$$
trap "rm -f $PIPE" 0

# all on one line
find . -name "xfile*" -print >> $PIPE ; tar cvf /tmp/tar.eds $(cat $PIPE); cat $PIPE|xargs rm

and then perform whatever operations you want to.

Regards,

Ed
 
Ed,

I started playing around with shell scripting a few days ago and have no clue about trap etc, but this is what I came up with, with your help of course

put the commands below in a script file

find . -name "XFiles*" -mtime -1 > TEMP.out
tar cvf MODTMP1.tar $(cat TEMP.out); gzip MODTMP1.tar; cat TEMP.out|xargs rm;

It does do what I wanted.

Again thanks a bunch for your help

Regds
Nask
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top