LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-02-2007, 09:07 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 280

Rep: Reputation: 30
delete file that match the content


Greetings!

I have a dir with 10,000 text files and growing. Do you have a script where if the word "LINUXFORUMS" has on the body of the text file, it will automatically delete the file. I cannot get to it since I'm only familiar to BASH. Anyone have a script similar to this or any ideas how can i do this? Any links where it can direct me to this kind of script?

TIA

--linux newbie
 
Old 04-02-2007, 10:13 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
A very basic version to do this would be:
Code:
#!/bin/sh
#
# Example rm-files.sh - deletes all of the files in the directory it is run
# from that contain the string LINUXFORUMS
#

for i in *; do
  if grep 'LINUXFORUMS' $i >/dev/null ; then
    rm -v $i
  fi
done
You could save this in ~/bin and use it by changing to the directory containing the files and running it as ~/bin/rm-files.sh. It should have the search string passed in as a parameter and it should have some sanity checking - do you really want it deleting all matching files if you mis-typed the search string? etc, etc...

Have a look at the Advanced Bash Scripting Guide - it's well worth a read.
 
Old 04-02-2007, 10:16 PM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
You don't really need a script:

Code:
grep -l "LINUXFORUMS" /someDirectory/* | awk '{print "rm -f \""$0"\""}' | bash

Last edited by macemoneta; 04-02-2007 at 10:23 PM.
 
Old 04-03-2007, 02:23 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
or

grep -l blah | xargs -n19 rm
 
Old 04-03-2007, 02:27 AM   #5
packets
Member
 
Registered: Oct 2005
Posts: 280

Original Poster
Rep: Reputation: 30
This is the one that fit the most but i will also the others. Thanks for all those who help

Quote:
grep -l blah | xargs -n19 rm
 
Old 04-03-2007, 02:47 PM   #6
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
If you are going to use xargs, you probably want:

Code:
grep -Z -l "LINUXFORUMS" /someDirectory/* | xargs -0 rm -f
Otherwise, embedded blanks and other special characters won't be handled properly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete text content syseeker Programming 5 02-07-2006 07:32 AM
[Udev] Can I delete the content of /dev? lokee Linux - Software 6 02-04-2005 08:30 PM
delete content form a file bru Programming 1 04-30-2004 07:41 AM
How does one delete directories with content as the SU? Ferrell Ramey Linux - General 2 08-22-2002 07:16 PM
How to delete a dir and all it's content? ReSync Linux - Newbie 2 06-29-2002 06:45 AM


All times are GMT -5. The time now is 04:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration