logmanager/logarchiver

53 lines
963 B
Plaintext
Raw Permalink Normal View History

#!/bin/sh
2011-02-23 15:39:35 +01:00
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)
2011-02-23 15:39:35 +01:00
EOF
exit
}
while getopts hnie: o
2011-02-23 15:39:35 +01:00
do
case "$o" in
h) help ;;
e) e="$OPTARG" ;;
n) n=echo ;;
i) i=-i ;;
*) help ;;
2011-02-23 15:39:35 +01:00
esac
done
shift `expr $OPTIND - 1`
2011-02-23 15:39:35 +01:00
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
2012-07-30 12:02:42 +02:00
if [ 0 -ge $# ]
then
echo "Error: DIR expected"
help
fi
2011-02-23 15:39:35 +01:00
for d
do
find "${d}" -name archive -prune -o -type f \( -name "${e}" \) -exec sh -c '
2011-02-23 15:39:35 +01:00
if [ X = "X`fuser "$1" 2>/dev/null`" ]
then
a="`dirname "$1"`/archive"
'"${n}"' mkdir -p -- "$a"
'"${n}"' mv '$i' -- "$1" "$a"
2011-02-23 15:39:35 +01:00
fi ' -- '{}' \;
done