Shell Script To Clean Lighttpd Web Server Cache

by on April 28, 2008 · 1 comment

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.

  1. #!/bin/bash
  2. # Shell script to clean web server cache stored at /var/www/cache/ directory.
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2007 nixCraft project <>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit for more information.
  9. # -------------------------------------------------------------------------
  11. # Cache dir path
  12. CROOT="/var/www/cachelighttpd/"
  14. #Deleting files older than 10 days
  15. DAYS=10
  17. # Lighttpd user and group
  18. LUSER="lighttpd"
  19. LGROUP="lighttpd"
  21. # start cleaning
  22. find ${CROOT} -type f -mtime +${DAYS} | xargs -r /bin/rm
  24. # if directory missing just recreate it
  25. if [ ! -d $CROOT ]
  26. then
  27. mkdir -p $CROOT
  28. chown ${LUSER}:${LGROUP} ${CROOT}
  29. fi

4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Dean Hall

    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

Previous Script:

Next Script: