Shell Script To Clean Lighttpd Web Server Cache

in Categories Decision Making, File-management, Web Server last updated April 28, 2008

This shell script will clean lighttpd web server cache. You need to run this script via a cron job. Download install script at /etc/cron.daily directory.

# Shell script to clean web server cache stored at /var/www/cache/ directory. 
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
# Cache dir path
#Deleting files older than 10 days
# Lighttpd user and group
# start cleaning 
find ${CROOT} -type f -mtime +${DAYS} | xargs -r /bin/rm
# if directory missing just recreate it
if [ ! -d $CROOT ]
	mkdir -p $CROOT
	chown ${LUSER}:${LGROUP} ${CROOT}

Share this on:

1 comment

  1. I use a similar script to clean my lighttpd cache, but I use two find commands with slightly different options:

    find ${CROOT} -type f -mtime +${DAYS} -delete
    find ${CROOT} -depth -mindepth 1 -type d -empty -delete

    The first find command deletes files older than $DAYS, just like yours. I use the “-delete” option so I don’t fork an additional process to do deletion. If the list of files find wants to output is too large for bash, bash may bail out before executing xargs. Another option is to use “-exec rm -f ‘{}’ \;” to delete each file, but that forks a process for each file.

    The second find command deletes empty directories. It uses “-depth” to explicitly tell find to process directory contents first (so a directory whose contents are empty directories gets deleted also). (The “-delete” option implies “-depth”, but I like to be explicit.) The “-mindepth 1” option tells find not to do anything to (i.e., delete) ${CROOT} itself.

    I also use /bin/sh to improve performance on all my scripts (minimal, I know), and I don’t create the cache directory since it’s really a side-effect.

    My script in total:



    # Bail out if $dir doesn't exist.
    if test ! -d "$dir" ; then
    echo "'${dir}' does not exist." 1>&2
    exit 111

    # Delete all plain files older than $age days:
    find "$dir" -type f -mtime +${age} -delete

    # Delete all empty directories except $dir itself:
    find “$dir” -depth -mindepth 1 -type d -empty -delete

Leave a Comment