Posts or Comments 19 November 2008

OS X / Unix & Server Architecture Matt | 20 May 2008 11:50 pm

Recursively Remove Files By Extension

I’m finally getting around to cleaning out dreamweaver LCK files from a large website. We have one developer that uses dreamweaver so these files are useless. So we’ve got these LCK files all over the place in this site. I’m just sick of looking at them.

Enter a shell one liner …

find . -name '*.LCK' -type f -print0 | xargs -0 /bin/rm -f

That command recursively looks in and under the directory I’m in for any files with the LCK extension and removes them.

Here’s a shell script you can save that will prompt you for an extension name to remove.

#!/bin/sh

echo "Enter Extension"
read filepattern
echo "Files matching *.$filepattern will be removed."
echo "Is this correct? y|n"
read confirmation

if  [ $confirmation = y ]; then
	find $PWD -name “*.$filepattern” -type f -print0 | xargs -0 /bin/rm -f
else
	echo “quitting”
	exit;
fi

Or if you don’t want any confirmation of the file extensions you’re about to delete.

#!/bin/sh

echo "Enter Extension"
read filepattern
find $PWD -name "*.$filepattern" -type f -print0 | xargs -0 /bin/rm -f
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Slashdot
  • StumbleUpon
  • Technorati

One Response to “Recursively Remove Files By Extension”

  1. on 08 Oct 2008 at 4:11 pm 1.Andy said …

    Thanks, just wanted to say that this was very helpful for me.

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply