Need a script to delete all files in a directory then email once complete, I have the below as a template but unsure if this will work – of course no email component has been added!
#!/bin/sh
DIR="/var/www/public_html/docs/files/"
LIST=`ls -l $DIR | grep -v "total 0"`
FILES=`ls $DIR`
cd $DIR
if [ -z "$LIST" ]
then
exit
else
echo "Files Delete:"
echo $FILES
rm -f *
fi
Update:
#!/bin/sh
DIR="/home/test/test/docs/test/"
cd $DIR
rm -f *
Some notes:
You use all-caps DIR, LIST and FILES, but all-caps variables in shell scripts are, by convention, environment variables. You should use e.g.
instead.
To find how many files are in a directory use
You use both LIST and FILES; it seems like you’re tring to find out if there are any files before deleting them. There’s no point to this from a functionality point of view but if you must conditionally echo the list of files it’s better to make the decision this way.
Although you should be aware that this output cannot be reliably used to reconstruct the actual file names.
To actually delete the files you should again use
findPutting it all together
Here I have combined the “print files” and “delete files” steps into a single invocation of
find.