|
|
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? 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.
More Useful Unix and Linux Help Articles:
✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔ Can I script renaming files based on an XML data map?I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔ Test for valid numbers in a Bash shell script?In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔ Review: iSSH for the iPad/iPhoneIf you're running an online business like I am, there are times when you need to connect and log in to the server...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Unix and Linux Help
(Article 3973,
Written by Dave Taylor)
Tagged: Previous: How can I fix "too little virtual memory" in Windows? Next: Stop Outlook from asking for an account password? Reader Comments To Date: 6Dave Taylor said, on May 29, 2005 4:12 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. monsieur said, on May 17, 2006 5:11 AM:
you may also want to specify "-type d" to be sure you're only deleting _FILES_ M@ said, on October 18, 2006 9:18 PM:
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 Jonathan said, on March 5, 2009 9:50 AM:
Hi, this question could be a little simple. I have google but i don´t find the answer. Jim said, on November 15, 2012 7:43 PM:
@monsieur it should be "type -f" to only delete files: find . -type f "*.txt" will locate all files with txt extensions. UNIX is case-sensitive, so you may also want to test for all variations of txt (txt, TXT, TxT, etc) by using the 'iname' argument: find . -type f -iname "*.txt"
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
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?