How can I write a bash script to search all files in current directory for multiple strings?

the_endian

I want to have a bash script which:

  1. Runs the "strings" command on each file in current directory
  2. Searches the output of strings for each file for specific terms using grep

I have the following, but the script output does not show any matches:

#!/bin/bash

echo "Searching files in directory for secrets and urls"

for file in ./*
do
   echo "=====$file====="
   strings ${file} | egrep -wi --color 'secret\|password\|key\|credential|\http'
done

I've also tried strings $file | egrep -wi --color 'secret\|password\|key\|credential|\http' and eval "strings ${file} | egrep -wi --color 'secret\|password\|key\|credential|\http'" but these do not appear to work. The script outputs the filenames, but not the matches.

Kusalananda

You're using egrep which is the same as grep -E, i.e. it enables the use of extended regular expressions.

In an extended regular expression, | is an alternation (which is what you want to use), and \| matches a literal | character.

You therefore want

grep -w -i -E 'secret|password|key|credential|http'

or

grep -i -E '\<(secret|password|key|credential|http)\>'

where \< and \> matches word boundaries.

Or

grep -w -i -F \
    -e secret      \
    -e password    \
    -e key         \
    -e credential  \
    -e http

... if you want to do string comparisons rather than regular expression matches.

Additionally, you will want to always double quote variable expansions. This would allow you to also process files with names that contain whitespace characters (space, tab, newline) and names that contain filename globbing characters (*, ?, [...]) correctly:

#!/bin/sh

for name in ./*; do
    [ ! -f "$name" ] && continue    # skip non-regular files

    printf '==== %s ====\n' "$name"
    strings "$name" | grep ...
done

See also

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how do i write a such a bash script that can search, rename, and replace files

From Dev

How can I modify the script to search for root directory for jpg files

From Dev

How to write a unix command or script to remove files of the same type in all sub-folders under current directory?

From Dev

How can I backup directory and files including all sub directory and files using shell script?

From Dev

How to run a script on all *.txt files in current directory?

From Dev

In a Linux script, how to remove all files & directories but one, in current directory?

From Dev

How can i search for a string in all the files in a directory ? I'm getting out of memory exception

From Dev

How do I use grep to search the current directory for all files having a given string and then move these files to a new folder?

From Dev

How can I write a script to delete all the old WiFi profiles but keep the current work profiles

From Dev

How can I recursively delete all files of a specific extension in the current directory?

From Dev

How can I recursively delete all files of a specific extension in the current directory?

From Dev

How can I run "tail -f" on all log files in the current directory — except for one?

From Dev

How can I delete all the files in the current directory except one file?

From Dev

How can I write a small script to clean every directory's files when the computer starts?

From Dev

How can I write a small script to clean every directory's files when the computer starts?

From Dev

how can I find files matching multiple extensions from current directory with a single command?

From Dev

Can I search for multiple strings in one "find" command in batch script?

From Dev

In Vista Explorer, how can I search the contents of all files in a directory for a word?

From Dev

How can I wrap the current directory in strings in CMD?

From Dev

How to copy all of the files from one directory to another in a bash script

From Dev

How can I add the current directory to the search path in UNIX?

From Dev

How can I limit locate command to search current directory?

From Dev

How can I simplify this bash script that prints the number of files in working directory?

From Dev

A try to write a batch script to find all the .c files in the current directory and compile them

From Dev

How can I write a bash script that sends a graphical notification to all users' X displays?

From Dev

How would I write a bash function that can extract all common archive files?

From Dev

How can I copy multiple files in the same directory with different names but same extension in bash?

From Dev

How can I set PATH in the `dash`(or bash) shell so that it doesn't search any dirs ? because empty PATH searches current directory

From Dev

How can I reference the current directory of a script when sourcing it?

Related Related

  1. 1

    how do i write a such a bash script that can search, rename, and replace files

  2. 2

    How can I modify the script to search for root directory for jpg files

  3. 3

    How to write a unix command or script to remove files of the same type in all sub-folders under current directory?

  4. 4

    How can I backup directory and files including all sub directory and files using shell script?

  5. 5

    How to run a script on all *.txt files in current directory?

  6. 6

    In a Linux script, how to remove all files & directories but one, in current directory?

  7. 7

    How can i search for a string in all the files in a directory ? I'm getting out of memory exception

  8. 8

    How do I use grep to search the current directory for all files having a given string and then move these files to a new folder?

  9. 9

    How can I write a script to delete all the old WiFi profiles but keep the current work profiles

  10. 10

    How can I recursively delete all files of a specific extension in the current directory?

  11. 11

    How can I recursively delete all files of a specific extension in the current directory?

  12. 12

    How can I run "tail -f" on all log files in the current directory — except for one?

  13. 13

    How can I delete all the files in the current directory except one file?

  14. 14

    How can I write a small script to clean every directory's files when the computer starts?

  15. 15

    How can I write a small script to clean every directory's files when the computer starts?

  16. 16

    how can I find files matching multiple extensions from current directory with a single command?

  17. 17

    Can I search for multiple strings in one "find" command in batch script?

  18. 18

    In Vista Explorer, how can I search the contents of all files in a directory for a word?

  19. 19

    How can I wrap the current directory in strings in CMD?

  20. 20

    How to copy all of the files from one directory to another in a bash script

  21. 21

    How can I add the current directory to the search path in UNIX?

  22. 22

    How can I limit locate command to search current directory?

  23. 23

    How can I simplify this bash script that prints the number of files in working directory?

  24. 24

    A try to write a batch script to find all the .c files in the current directory and compile them

  25. 25

    How can I write a bash script that sends a graphical notification to all users' X displays?

  26. 26

    How would I write a bash function that can extract all common archive files?

  27. 27

    How can I copy multiple files in the same directory with different names but same extension in bash?

  28. 28

    How can I set PATH in the `dash`(or bash) shell so that it doesn't search any dirs ? because empty PATH searches current directory

  29. 29

    How can I reference the current directory of a script when sourcing it?

HotTag

Archive