0

I have created a script so I can search for 0kb files in a certain path, the output is to be redirected to a text file and then emailed. The script works except that the fie is empty either when it's emailed or when it's viewed after the script runs.

if [[ -n $(find /path/to/files/ -type f -empty -mmin +2) ]] then
> /tmp/output.txt ; mail -s "subject" -a /tmp/output.txt "email@address"
rm /tmp/ftr_output.txt
fi   

Not sure if I missed something so any help would be appreciated.

Thanks

2
  • 2
    You have explicitly instructed that /tmp/output.txt be truncated. Why do you expect content to be in there? What are you trying to achieve? Commented May 8, 2014 at 2:40
  • I'm guessing > /tmp/output.txt was supposed to be inside the $() Commented May 8, 2014 at 8:33

1 Answer 1

3

I guess you want something like this:

# Save find results in a text file
find /path/to/files/ -type f -empty -mmin +2 > /tmp/output.txt

# Check that the text file isn't empty (meaning no results from find)
if [ -s /tmp/output.txt ]
then
    # Send the text file along with an email
    mail -s "subject" -a /tmp/output.txt "email@address"
fi
# Remove the text file
rm /tmp/output.txt
Sign up to request clarification or add additional context in comments.

10 Comments

The test will always fail. Try ` find ... > /tmp/output.txt; if [[ -n /tmp/output.txt ]] ...`
@WilliamPursell That test will always pass :) Use -s
I suggest this awesome hack: if find .. | grep "" > file
wc -l file outputs 0 file, not just 0. For this approach, you could go with [[ -s, which checks for non-empty file and avoids counting lines at all.
You need the rm /tmp/output.txt after the fi so that it is removed even when it is empty. Arguably, the name should be decorated with $$ or created with the mktemp command.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.