• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Deleting ALL files of a certain name


From a command line (logged in as root) I want to DELETE all files on my box called "bad_file.jsp"

I did "find / | grep bad_file.jsp" and found over 70 of these, each in a different directory.  I don't want to keep ANY of them.

How can I do this WITHOUT manually deleting all 70 ?

4 Solutions
find / -name bad_file.jsp -delete

find . -name "bad_file.jsp" -print | xargs rm

start in / directory or the directory you wish to have the process "start" in.
Gabriel OrozcoSolution ArchitectCommented:
actually, for a well defined name, starting from / is not a problem. byut the sintax is:

find / -name "bad_file.jsp" -exec rm {} \;
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

exec rm will spawn a thread for every file - not a good thing if there are hundreds or more files.

Gabriel OrozcoSolution ArchitectCommented:
good point.

but when these hundreds or thousands of files are in the same directory, the pipe redirection cannot hold that many files and fails (been there, done that) so it falls back to the good 'ol -exec rm {} \;  that can work a bit more slowly, yes, but surely will do the work.
point being, there's many ways to skin this cat.
Gabriel OrozcoSolution ArchitectCommented:
I want to have a go too!:

My approach might be:

find / | grep bad_file.jsp

carries out your search. This is then pipes into gawk or awk, which takes th name and turns it into a command.

I like this approach because I can run the above, and check that what is printed out is the commands that I want the system to run. Once I'm happy, then I run the above, but pipe it though a shell:

find / | grep bad_file.jsp | gawk '{print "rm -Rf " $0}' | /bin/sh

This then executes the commands.

Although the above is longer as a command, it means that you can check exactly what commands are going to be carried out...and the awk element gives you infinite flexibility if you wanted to say change the owner of files in a certain folder to the name of the file...or any other wierd and wonderful transalation that you might consider.

Hope that helps:)

...perhaps a competition now for the most unreadable solution...or perhaps the one with least chars?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now