Results 1 to 3 of 3

Thread: Bash one liner to rm files based on content

  1. #1
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    [SOLVED] Bash one liner to rm files based on content

    Hi,

    I need either a one liner or a bash script to recursively search a folder/sub folders for binary files (windows .lnk files) containing a specific text string. If it finds a match it must delete the file.

    After googling for a number of hours I've come accross the below one liner.

    Why does it not work?

    Code:
    find . -name '*.lnk' -print0 | xargs -0 grep -I "C:" | xargs -0 rm -f
    Last edited by john_spiral; November 19th, 2009 at 03:53 PM.

  2. #2
    Join Date
    May 2006
    Beans
    1,790

    Re: Bash one liner to rm files based on content

    Quote Originally Posted by john_spiral View Post
    Hi,

    I need either a one liner or a bash script to recursively search a folder/sub folders for binary files (windows .lnk files) containing a specific text string. If it finds a match it must delete the file.

    After googling for a number of hours I've come accross the below one liner.

    Why does it not work?

    Code:
    find . -name '*.lnk' -print0 | xargs -0 grep -I "C:" | xargs -0 rm -f
    If you run it without the last part, you will see why.

    Code:
    find . -name '*.lnk' -print0 | xargs -0 grep -I "C:"
    The output from 'grep' is not suitable as input to 'xargs'.

  3. #3
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Bash one liner to rm files based on content

    thanks for the heads up on grep/xargs.

    Finally got it to work with the below command:

    Code:
    grep -r C: * | sed 's/Binary file//g' | sed 's/ matches//g' | xargs -I '{}' rm "{}"
    elegant: no
    works: yes
    Last edited by john_spiral; November 19th, 2009 at 03:58 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •