Script to monitor a folder and compress the content We have a Debian Squeeze server where we upload files through SFTP, and I wrote this script to automatically compress uploaded files.

The script is scheduled through a cron job in an unprivileged user's crontab.

As of now, the script works and does its job.

But I'm pretty novice to Bash scripting, and I'm wondering if it has any major flaws. (e.g. If the file name contains " what happens? Does it have security flaws?)

Also, am I doing it right, or there are better ways to do it?

#!/bin/bash

# Use the newline character
# as the field separator.
IFS=$'\n'

# The folder to watch.
dir="/path"

# Get the folder's content, recurively.
for item in $(find "${dir}")
do

        # If the item is a regular file.
        if [ -f "${item}" ]
        then

                # Check if the file is in use 
                # by someone else. E.g. The SFTP
                # daemon because the user is still
                # uploading it.
                lsof -n "${item}" &gt; /dev/null

                # If no one is using the file.
                if [ "$?" -ne 0 ]
                then

                        # Get the file extension.
                        ext=$(echo "${item}" | awk -F "." '{ print $NF }')

                        # If the file is not a zip archive
                        # and does not exists a zip archive for it
                        if [ "${ext}" != "zip" -a ! -f "${item}.zip" ]
                        then
                                # Create the zip archive.
                                zip "${item}.zip" "${item}" &gt; /dev/null
                        fi

                fi

        fi

done
Slightly different than @kojiro's answer, using find for everything:

find /path \
     -type f \
     ! -iname "*.zip" \
     ! -exec /usr/sbin/lsof -n "{}" \; \
     ! -exec test -f "{}.zip" \; \
     -exec zip -q "{}.zip" "{}" \; \
     -exec rm "{}" \;


(last step only if the removal of the original is required).