Find the answer to your Linux question:
Page 1 of 5 1 2 3 4 5 LastLast
Results 1 to 10 of 42
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Help with a bash script that finds certain words

    while read file; do
    echo $file
    done < <(find /home -type f -exec grep -qE 'cookie|browser|mozilla|chrome|safari' {} \; -print0)
    That is what I have so far, I'm trying to write a script that will search for those particular words, and report them to the screen in the following format: username, line with bad word found, and the Path.

    My question is, how do I take my output and make it readable?

  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Saint Paul, MN
    Look at the "printf" command.

  3. #3
    I don't understand what you mean by that.

  4. $spacer_open
  5. #4
    how about:
    dirs=$(ls $basedir)
    for user in $dirs; do
      echo -e "\nUsername: $user"
      for badword in cookie browser mozilla chrome safari; do
        found=$(find $basedir/$user -type f -exec grep -qE $badword {} \; -print)
        if [ -n "$found" ]; then
          echo bad word: $badword
          echo filepath: $found

  6. #5

    thanks for that, when I run the script, it will display the Username: xxx but seems to get hung up after that.

  7. #6
    Did you change anything in it? try a simplified version:

    dirs=$(ls $basedir)
    for user in $dirs; do
      echo -e "\nUsername: $user"
      for badword in cookie browser mozilla chrome safari; do
        find $basedir/$user -type f -exec grep -H $badword {} \; 

  8. #7
    I get stuff like this:

    /home/smith/vim/runtime/syntax/vim.vim:syn keyword vimOption contained altkeymap arabic autowrite backupcopy bdir bin bomb bt ccv charconvert cinoptions cms comments conceallevel cpo cscopequickfix cst cursorline dex digraph ead ei equalal

    I was getting a lot of junk with cookie, browser... that I tried different words, including bomb, which you see in the output. Is there a way to omit these type of these files from being flagged?

  9. #8
    I don't know what you mean by excluding certain words. I thought the whole point was to have a list of keywords, and look for occurrences of them in all files found. Did you try the 2nd command I posted?

    Maybe post what you've tried?

  10. #9
    maybe it would make more sense with a hypothetical. Let's use the word "gun".

    I need the script to flag "gun" in a sentence like "I'm going to shoot you with a gun." as well as "She jumped off the starting blocks before the gun sounded."

    But, then give me the option to see that the sentence "She jumped off the starting blocks..." is ok and stop it from being flagged next time I run the script. On the contrary, "I'm going to shoot you..." is a threat that needs to be reported every time until further action is taken.

    Does this make a bit more sense?

  11. #10
    So it sounds like you need to generate a "white list" (or would it be "black list"?) file, containing lines like "She jumped off", etc. Then in the above loop, you can check that file for the string generated by the find..grep command.

Posting Permissions

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