Last Updated: February 25, 2016
·
667
· sean9999

find and delete files matching a regex pattern

On my servers, i create alot of js and css files that are automatically generated as caches. They become obsolete as often as the codebase changes (which is very often), so finding and deleting them all every so often is a good idea. They all conform to the same pattern:

  1. they are all in folders called "raw"
  2. they all either have a .js and a .css extension
  3. Their names are md5() hashes of their contents.

This command lists the files:

find -L /var/www/domains/ -regextype posix-extended -iregex '.*/raw/[a-f0-9]{32}\.(js|css)'

Print the number of matches

find -L /var/www/domains/ -regextype posix-extended -iregex '.*/raw/[a-f0-9]{32}\.(js|css)' | wc -l

delete them:

find -L /var/www/domains/ -regextype posix-extended -iregex '.*/raw/[a-f0-9]{32}\.(js|css)' -delete