Find and removing the old files and zipping the files using shell script

Hi,

I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script.

script was return as follows.

find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +30 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +10 -a -mtime -30 -type f | xargs gzip -fq
find /cer_skyliv??/work/bil -mtime +10 -type f | xargs gzip -fq

If i am executing using the shell script receiving the below error message as

./cleanup.sh
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'zip: invalid option -- '
Try `gzip --help' for more information.
'zip: invalid option -- '
Try `gzip --help' for more information.
'zip: invalid option -- '
Try `gzip --help' for more information.
'zip: invalid option -- '

Though if we are executing the command explicitly i.e running the command its working fine . It seems using the shell script its not running .

could you please assist here.

I don't see the reason you use xargs here. Try modifying your code like:

find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;

And the other lines accordingly. It should work.

I hope this helps.

bakunin

i have tried use the above command using shell script. it is throwing the below error message .

 ./cleanup.sh
find: missing argument to `-exec'

command were added in shell script as follows.

vi cleanup.sh
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +30 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +10 -a -mtime -30 -type f | xargs gzip -fq
find /cer_skyliv??/work/bil -mtime +10 -type f | xargs gzip -fq

hm, i get no such error on any of my systems. Try to execute the command outside the script by hand and look if it works there. If it does, something in the scripts environment is amiss.

Which OS do you use and which version? If it is Solaris you should set your PATH to /usr/xpg4/bin instead of /usr/bin . Also check the output of

which find
type find

Because maybe you use an alias, link or whatever instead of the common binary.

I hope this helps.

bakunin

Please also show us the output you get from the command:

od -bc cleanup.sh

The diagnostic message you're getting could be caused by some unexpected character sequence between the -exec and the end of that line in your script. Although I don't get that diagnostic with the BSD-based find on macOS version 10.13.3, one might expect something like that if there is a null byte anywhere after the -exec or if there is a <carriage-return> just after the \; .

Can any one help the issue. Tried with many ways getting the same error message.

 ./cleanup.sh
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'm: invalid option -- '
Try `rm --help' for more information.
'zip: invalid option -- '
 vi ./cleanup.sh
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +30 -type f | xargs rm -f
find /cer_skyliv??/log -mtime +10 -a -mtime -30 -type f | xargs gzip -fq
find /cer_skyliv??/work/bil -mtime +10 -type f | xargs gzip -fq

xargs has pitfalls with special characters in file names. Risky with rm !
Replace them in find with -exec ... {} \; .

find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +30 -type f -exec rm -f {} \;
find /cer_skyliv??/log -mtime +10 -type f -exec gzip -fq {} \;
find /cer_skyliv??/work/bil -mtime +10 -type f -exec gzip -fq {} \;

Perhaps you can replace each -exec ... {} \; by the faster -exec ... {} + .
The + collects the arguments just like xargs .