Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Deleting ALL files of a certain name

Posted on 2006-04-17
8
Medium Priority
?
236 Views
Last Modified: 2010-04-20
Hello.

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 ?

Thanks!
0
Comment
Question by:hankknight
8 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 400 total points
ID: 16472356
find / -name bad_file.jsp -delete

0
 
LVL 12

Assisted Solution

by:Heem14
Heem14 earned 400 total points
ID: 16472360
find . -name "bad_file.jsp" -print | xargs rm

start in / directory or the directory you wish to have the process "start" in.
0
 
LVL 19

Assisted Solution

by:Gabriel Orozco
Gabriel Orozco earned 400 total points
ID: 16474536
actually, for a well defined name, starting from / is not a problem. byut the sintax is:


find / -name "bad_file.jsp" -exec rm {} \;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

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

0
 
LVL 19

Expert Comment

by:Gabriel Orozco
ID: 16474722
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.
0
 
LVL 12

Expert Comment

by:Heem14
ID: 16474734
point being, there's many ways to skin this cat.
0
 
LVL 19

Expert Comment

by:Gabriel Orozco
ID: 16474808
yep!
0
 
LVL 22

Accepted Solution

by:
pjedmond earned 800 total points
ID: 16493139
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question