find: how to no have errors (stderr) when no files exists

daks asked:

i have a custom script which gzip log files when they are one day old. In my script all errors (stderr) are logged to a file and at the end of the script i use this file to know if its execution was good or not.

This part of my code looks like

# redirection of stderr to file
exec 2> /tmp/errors.txt

# gzip old log files
find *.log -mtime +1|xargs gzip -f

# test if error log file is empty
if [ -s /tmp/errors.txt ]; then

My problem is: this code works great if there is at least one file to gzip, but if not it raises an error when i don’t want to.

I’ve tried different solutions to solve this problem with no success.

Do anyone know how to solve this problem?

I answered:

I’d get rid of find completely.

For instance:

exec 2> /tmp/errors.txt

for file in *.log; do
    gzip -f $file

if [ -s /tmp/errors.txt ]; then

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.