0

How can I search every file on an entire Ubuntu machine for a particular word using SSH?

Note: I am using the Windows program PuTTY to run my SSH sessions, but once I'm connected I have an ordinary bash session.

Following this answer on UL.SE, I used this bash command:

find / -xdev -type f -print0 | xargs -0 grep -H "search for this text"

But this command returns output even when access is denied or when there are no matches, and since PuTTY limits viewable session history, I cannot see all of the results.

I'm doing a capture-the-flag challenge and I had to SSH to a Linux machine, I want to search every file on the entire machine for the string "flag". I have very limited knowledge about bash and its commands.

6
  • Is the string case-sensitive? Commented May 26, 2020 at 17:00
  • @NasirRiley I think so
    – bzr
    Commented May 26, 2020 at 17:03
  • Edit question to make it clearer: When you say entire machine. Do you mean with in a file? Do you know which files it may be in (user file, system file)? Commented May 26, 2020 at 17:34
  • Have you tried redirecting standard error to /dev/null? Have you tried less? Commented May 26, 2020 at 17:35
  • @ctrl-alt-delor I mean every single file on the server
    – bzr
    Commented May 26, 2020 at 19:10

1 Answer 1

0

Append 2> /dev/null to a command to hide errors. This is called redirecting stderr. So for your specific command it would be:

find / -xdev -type f -print0 | xargs -0 grep -H "search for this text" 2> /dev/null

But that's very verbose. You can also:

grep -rw "flag"

from grep(1):

       -r, --recursive
              Read all files under each directory, recursively, 
              following symbolic links only if they are on the 
              command line. Note that if no file operand is given, 
              grep searches the working directory.  This is 
              equivalent to the -d recurse option.

       -w, --word-regexp
              Select only those lines containing matches that form 
              whole words. The test is that the matching substring 
              must either be at the beginning of the line or preceded 
              by a non-word constituent character.  Similarly, it 
              must be either at the end of the line or followed by a 
              non-word constituent character.  Word-constituent 
              characters are letters, digits, and the underscore. 
              This option has no effect if -x is also specified.

Note that this will find the word "flag" in any file from the current directory. So if you want to search the whole machine, run this from /. If you want it to be case-insensitive, add -i. Another advantage of this is words like flagged will not be found.

Use grep -rw "flag" 2> /dev/null to filter out all of those Permission denied messages

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .