[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need to delete filename from all subdirectories in Linux

Posted on 2010-11-30
8
Medium Priority
?
985 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 20

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

656 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