Solved

Need to delete filename from all subdirectories in Linux

Posted on 2010-11-30
8
959 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
8 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 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 250 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now