PDA

View Full Version : How do I delete all files with a certain name under subdirectories?



SNYP40A1
July 29th, 2008, 07:09 AM
I want to delete all files with a specific name found below some subdirectory? Does anyone know a command to do this (I prefer to keep all other files :-) I think it will involve the find command, but not sure how to chain it to delete.

mssever
July 29th, 2008, 07:14 AM
I want to delete all files with a specific name found below some subdirectory? Does anyone know a command to do this (I prefer to keep all other files :-) I think it will involve the find command, but not sure how to chain it to delete.

rm */filename #or
rm dir{1,2,3}/filename #or
for i in *; do
[[ -d "$i" ]] && rm "$i"/filename
done

ghostdog74
July 29th, 2008, 07:59 AM
find /path -type f -name "filename" -delete

SNYP40A1
July 29th, 2008, 05:51 PM
find /path -type f -name "filename" -delete


Thanks, this also works:

find . -name "dcc*" -delete

Not sure why the -type f is needed.

ghostdog74
July 29th, 2008, 06:16 PM
see the man page for definition of -type f

Martin Witte
July 29th, 2008, 07:27 PM
Thanks, this also works:

find . -name "dcc*" -delete

Not sure why the -type f is needed.

'-type f' is needed if you just want to delete regular files, and not eg. directories