Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I delete all occurances of a file in Linux?

How can I delete one file from every place on my disk using Linux? In Windows' I'd open up a command window and use the command

    del archive.txt /s

but what's the Linux equivalent?


Dave's Answer:

Alright, I know about the del command in DOS, but I don't know about the "/s" flag. I had to dig for quite a while before I finally found out that the "/s" switch deletes all files from subdirectories. As I suspected, what you're trying to do is delete all files with the specified name regardless of where they are on your PC.

First off, this seems rather dangerous! If nothing else, I'd certainly start out in my own home directory (just use cd without any arguments and you'll be taken there directly) to ensure that I didn't accidentally delete a file that some other program (or user!) needed.

In any case, the Linux command you seek is find and it's far more powerful than the DOS del command, but, um, more complex too.

To find all occurrences of a specific filename at the current point in the file system and below, use this command:

  find . -name archive.txt

The output will be a list of fully-qualified filenames like "./save/archive/archive.txt" and similar. There are a couple of ways to delete this set of files, but I always use the xargs command in a pipe:

  find . -name archive.txt | xargs rm

If you're still unsure, add the "-i" flag to the rm command and it'll ask you to confirm each and every deletion before it occurs.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

the method works .. however if the filename has spaces.. the \'find\' command doesn\'t escape the space characters so xargs wld give the wrong input to rm..
how do I escape the spaces?

Posted by: Kevin at May 29, 2005 4:10 AM

True enough! You need to use "-print0" in the find command and the "-0" flag to xargs. That shields the spaces in filenames from being misinterpreted in the pipeline and it all works fine.

Posted by: Dave Taylor at May 29, 2005 4:12 AM

you may also want to specify "-type d" to be sure you're only deleting _FILES_

Posted by: monsieur at May 17, 2006 5:11 AM

how would i remove a file with alot of spaces in the name. the filename is actually a sentense. with a "." at the end. it dosent show up in gui view but in terminal it shows up as " blah blah balh.~ "

How do i get rid of that

Posted by: M@ at October 18, 2006 9:18 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]