logmanager/logarchiver

53 lines
963 B
Bash
Executable file

#!/bin/sh
e='*.log'
i=
n=
help() {
cat <<EOF
`basename "$0"` -h | [-i|-n] [-e EXP] DIR [DIR [...]]
Look for files in DIR and his subdirs named like EXP and moves them to DIR/archive.
-e EXP File must have this expression (std: "$e").
-n Dummy mode. Shows files, which will be moved if not dummy mode.
-i Asks you to move (see also mv)
EOF
exit
}
while getopts hnie: o
do
case "$o" in
h) help ;;
e) e="$OPTARG" ;;
n) n=echo ;;
i) i=-i ;;
*) help ;;
esac
done
shift `expr $OPTIND - 1`
if ( fuser "$0" >/dev/null 2>/dev/null )
then :
else
echo "fuser not found or does not work! logarchiver won't to destroy any files, so exit."
exit 1
fi
if [ 0 -ge $# ]
then
echo "Error: DIR expected"
help
fi
for d
do
find "${d}" -name archive -prune -o -type f \( -name "${e}" \) -exec sh -c '
if [ X = "X`fuser "$1" 2>/dev/null`" ]
then
a="`dirname "$1"`/archive"
'"${n}"' mkdir -p -- "$a"
'"${n}"' mv '$i' -- "$1" "$a"
fi ' -- '{}' \;
done