?
Solved

Need to delete filename from all subdirectories in Linux

Posted on 2010-11-30
8
Medium Priority
?
978 Views
Last Modified: 2013-12-05
I have a single filename that exists thousands of times in the subdirectory tree, and I need to delete it everywhere it exists.

Easy enough in DOS:

delete /s myfile.txt

A similar approach in Linux doesn't work:

rm -r myfile.txt

because it will only remove from the current directory. What is the best approach?
0
Comment
Question by:jbbarnes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1000 total points
ID: 34237962
That's a task for "find":

find /start/dir -type f -name myfile.txt | xargs rm

find searches all subdirectories according to the given criteria. Results are passed to xargs which in turn calls rm with a number of parameters (= files to delete) suitable for this command.

wmp

0
 

Author Comment

by:jbbarnes
ID: 34238034
Hi, I can see how that would work. Thanks.

I do get an error, though, when I try it:

rm: missing operand

The xargs man page gives an example along these lines:

find /start/dir -type f -name myfile.txt -print | xargs rm -f
0
 
LVL 2

Assisted Solution

by:koffu
koffu earned 1000 total points
ID: 34238058
in previous post command is valid, but with space after "xargs rm ". Also, if your filename will have spaces or another special symbols - it will not work.
try
find /start/dir -type f -name myfile.txt -delete
or
find /start/dir -type f -name myfile.txt -print0 | xargs -n1 -0 -I{} rm {}
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34238060
Well,

"-print" should be the default action of "find", and "rm -f" is for omitting the confirmation prompt before deleting a write-protected file.

Both things are not vital for the command to work.
Try adding "-print", perhaps your "find" would need it.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34238087
Not every find implementation has the "-delete" option, and "xargs -n1" would fork a new instance of rm for each one of the "thousands" of files, which is suboptimal for performance.
Never heard of the need to have a space after rm.

0
 
LVL 2

Expert Comment

by:koffu
ID: 34238282
rm also will accept a some filenames list to remove, but if user have a thousands copies of this file - multidelete is not a good solution. To avoid performance issues we can use -P key to run rm processes in parallel or use -n 10 for example.
without space we will get something like 'rmsomefile', not valid 'rm somefile'.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34238325
>> without space we will get something like 'rmsomefile', not valid 'rm somefile' <<

Nope. Did you ever run such a command, or is it all theory?
0
 
LVL 19

Expert Comment

by:simon3270
ID: 34238610
xargs will definitelyadd a space after the command.  The "missing operand" error is probbaly that there were no matching files from the "find" command, so xargs just ran the rm command with no files to delete.   The message does not appear if you give "rm" the "-f" parameter.

If you want to run this more than once, you could run the "find" command to count the number of matching files e.g.

numfiles=$(find /start/dir -name myfile.txt | wc -l)
if [ $numfiles -gt 0 ]
then
  find /start/dir -name myfile.txt | xargs rm
fi

One other thing to try is to add "-t" to the xargs command - this will print out the command it is about to run, including all of the parameters:

find /start/dir -name myfile.txt | xargs -t rm
0

Featured Post

Independent Software Vendors: 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

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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 Month13 days, 10 hours left to enroll

801 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