49

I want to delete all files with a given name in all the subdirectories of my home directory.

I tried:

rm -r file

in my home directory, but it didn't work because that file doesn't exist in that directory.

2 Answers 2

74

find . -name "filename" -delete

21

as an elaboration on @tante's answer, you may wish to ensure the file list used is correct before deleting those files:

find <source_dir> -name <filename> -print

if inspection shows valid list then

find <source_dir> -name <filename> -delete

another option if wanting this over many directories using a temporary holding directory:

mkdir <dest_dir>
for i in <list_of_directories>
do
  find "$i" -name <filename> -exec /bin/mv {} <dest_dir>
done

# check dest_dir
ls dest_dir
rm -rf <dest_dir>

As always, please ensure the accuracy of any scripts before execution and always have a backup ready in-case something goes wrong.

2
  • 3
    always put double quotes around variable substitutions, or your command won't work with file names containing certain special characters (whitespace, wildcards and backslashes). Nov 1, 2010 at 18:15
  • @Gilles - Yes, thanks for updating and ensuring this answer was correct.
    – Wayne
    Nov 3, 2010 at 23:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .