Find the answer to your Linux question:
Results 1 to 8 of 8
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 ...
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    how to delete file with matching 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

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    since you know bash, what have you done so far? a simple eg
    Code:
    for file in *
    do
        grep "LINUXFORUMS" file > /dev/null 2>&1
        if [ $? -eq 0 ]; then
              echo "Removing file $file"
              ## enter remove code here.
        fi
    done

  3. #3
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    thanks for the idea. I will try these one.

    Anymore suggestions?

  4. #4
    Just Joined! SuSEholic's Avatar
    Join Date
    Apr 2007
    Posts
    28

    try this...

    Try this...

    Code:
    grep -rl 'some text' /path/to/dir | xargs -d '\n' rm
    add -i for searching case-insensitive.

    Code:
    grep -rli 'some text' /path/to/dir | xargs -d '\n' rm

    Hope this help.

  5. #5
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    Anyone could help me on this? This a sample dunno if works

    #!/bin/bash
    dir=/home/user/dir

    echo "Type word:"
    read input
    if [ $input -eq "LINUXFORUM" ]
    then
    grep $input $dir
    ! -------> This is where I want to delete the file when a word "LINUXFORUM"
    has match on of the content of the file.
    fi

  6. #6
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    @Suseholic

    Thanks again for the idea. I think this is a good one. I'll check on this and create a simple script and post it here

  7. #7
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    This one did a trick. Thanks!

    #!/bin/bash

    echo "Please choose user:"
    read input1
    echo "Type a word.All spam contain this word will be deleted"
    read input2
    grep -rl $input2 /home/$input1/Maildir/.junkmail/new | xargs -d '\n' rm

  8. #8
    Just Joined! SuSEholic's Avatar
    Join Date
    Apr 2007
    Posts
    28
    You did it

    Thanks also for the script.

Posting Permissions

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