0

There is a string located within a file that starts with 4bceb and is 32 characters long. To find it I tried the following

Input:

find / -type f 2>/dev/null | xargs grep "4bceb\w{27}" 2>/dev/null

after entering the command it seems like the script is awaiting some additional command.

Leprekus
  • 34
  • 6
  • Since you are looking at _all_ the files in your system, are you sure that your command does not just take a long time? As long as there are no matches, you will not see any output, which could seem to you as if you are supposed to provide additional input. Additionally, in order to use Perl syntax with `grep` (which `\w` is), you need to add the `-P` flag. Otherwise, your grep command will not match what you intend. – buddemat Aug 12 '20 at 21:07

1 Answers1

0

Your command seems alright in principle, i.e. it should correctly execute the grep command for each file find returns. However, I don't believe your regular expression (respectively the way you call grep) is correct for what you want to achieve.

First, in order to get your expression to work, you need to tell grep that you are using Perl syntax by specifying the -P flag.

Second, your regexp will return the full lines that contain sequences starting with "4bceb" that are at least 32 characters long, but may be longer as well. If, for example your ./test.txt file contents were

4bcebUUUUUUUUUUUUUUUUUUUUUUUU31
4bcebVVVVVVVVVVVVVVVVVVVVVVVVV32
4bcebWWWWWWWWWWWWWWWWWWWWWWWWWW33
sometext4bcebYYYYYYYYYYYYYYYYYYYYYYYYY32somemoretext
othertext 4bcebZZZZZZZZZZZZZZZZZZZZZZZZZ32 evenmoretext

your output would include all lines except the first one (in which the sequence is shorter than 32 characters). If you actually want to limit your results to lines that just contain sequences that are exactly 32 characters long, you can use the -w flag (for word-regexp) with grep, which would only return lines 2 and 5 in the above example.

Third, if you only want the match but not the surrounding text, the grep flag -o will do exactly this.

And finally, you don't need to pipe the find output into xargs, as grep can directly do what you want:

grep -rnPow / -e "4bceb\w{27}"

will recursively (-r) scan all files starting from / and return just the ones that contain matching words, along with the matches (as well as the line numbers they were found in, as result of the flag -n):

./test.txt:2:4bcebVVVVVVVVVVVVVVVVVVVVVVVVV32
./test.txt:5:4bcebZZZZZZZZZZZZZZZZZZZZZZZZZ32
buddemat
  • 4,552
  • 14
  • 29
  • 49
  • This is a really helpful and detailed answer especially to someone like me who is just getting to know linux, I greatly appreciate it and really got to see just how far one can go when you know how to use grep correclt. I have one question though, when inputting the command line as you wrote it I start getting all of grep's errors like invalid argument and permission denied as it searches through the files, if i type `2>/dev/null` to supress the errors the console just does not output anything as if it got stuck. – Leprekus Aug 13 '20 at 18:01
  • @Leprekus: To suppress the error messages, you can add the `-s` flag to your `grep` call, as described here https://stackoverflow.com/questions/6426363/how-can-i-have-grep-not-print-out-no-such-file-or-directory-errors. The comments in that post are also helpful to understand some possible pitfalls. As long as `grep` finds no match, it may seem as if it were stuck, but remember, you are scanning from `/`, it may take a long time to get trough all your system's files... (Also, I'm glad my answer is helpful. If you feel that it answered your question, you can scknowledge that by accepting it). – buddemat Aug 14 '20 at 07:27