grep for a string and delete the files containing that string - Unix

This is a discussion on grep for a string and delete the files containing that string - Unix ; Hi, I have a number of files. I need to search them for a particular string in them. Also I need to remove the files containing that particular string. How to do this in UNIX ??...

+ Reply to Thread
Results 1 to 6 of 6

Thread: grep for a string and delete the files containing that string

  1. grep for a string and delete the files containing that string

    Hi,
    I have a number of files.
    I need to search them for a particular string in them.
    Also I need to remove the files containing that particular string.
    How to do this in UNIX ??

  2. Re: grep for a string and delete the files containing that string

    Gokul wrote:
    > Hi,
    > I have a number of files.
    > I need to search them for a particular string in them.
    > Also I need to remove the files containing that particular string.
    > How to do this in UNIX ??


    Check the man pages and have a play with find and grep.

    --
    Ian Collins.

  3. Re: grep for a string and delete the files containing that string

    On Sep 23, 9:33*am, Gokul wrote:

    > I have a number of files.
    > I need to search them for a particular string in them.
    > Also I need to remove the files containing that particular string.
    > How to do this in UNIX ??


    Something like:

    $ find . -type f -exec grep -q "a particular string" {} \; -exec
    rm -f {} +

    --
    Max


  4. Re: grep for a string and delete the files containing that string

    In article
    <1a969cdf-157e-48bc-ba1c-951c5154d09d@v16g2000prc.googlegroups.com>,
    Gokul wrote:

    > Hi,
    > I have a number of files.
    > I need to search them for a particular string in them.
    > Also I need to remove the files containing that particular string.
    > How to do this in UNIX ??


    rm $(grep -l string filenames)

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  5. Re: grep for a string and delete the files containing that string

    On Sep 23, 10:33 am, Gokul wrote:

    > I have a number of files.
    > I need to search them for a particular string in them.
    > Also I need to remove the files containing that particular string.
    > How to do this in UNIX ??


    If they're all in the same directory, and there aren't too many
    of them, Barry Margolin's suggest is the best. Otherwise:

    find ... | # generate list of filenames
    xargs grep -l string | # reduce to only those with string
    xargs rm | # remove

    --
    James Kanze (GABI Software) email:james.kanze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  6. Re: grep for a string and delete the files containing that string


    On Sep 24 2008 02:13, James Kanze wrote:
    > On Sep 23, 10:33 am, Gokul wrote:
    >
    >> I have a number of files.
    >> I need to search them for a particular string in them.
    >> Also I need to remove the files containing that particular string.
    >> How to do this in UNIX ??

    >
    > If they're all in the same directory, and there aren't too many
    > of them, Barry Margolin's suggest is the best. Otherwise:


    Actually it's the worst, because it does not even handle spaces in
    filenames and as such may delete innocent files.

    rm -f $(grep -l string /etc/passwd\ with\ spaces.backup)

    And say goodbye to your user database.

+ Reply to Thread