I was trying to get through level 4 of Over the Wire - Bandit wargame. The level requires you to look for a password stored in the only human-readable file in a folder.

Now, the files are only a few, so what I did was, for each file, check its type with the file command, and open the ASCII one as soon as I found it. I was wondering if there is a way to pass file results to the find command, in order to make this search automatically.

(Feels like a really trivial question, but still I couldn't find an answer).

Thank you in advance

  • 1
    what is find supposed to do here, look for the password? Jan 6 '18 at 14:36
  • try file * | grep -i text. the text file may not be ascii - it may be utf-8 or utf-16, and that will at least narrow down the list of possible files. then view them with, e.g. less.
    – cas
    Jan 6 '18 at 14:41

grep -I ignores non-ASCII files. This is a non-standard option, but implemented by many greps.

find thefolder -type f -exec grep -q -I . {} ';' -print

This would list all files in or below thefolder that were ASCII (according to grep) and non-empty (we're asking grep to look for a single character).

With GNU find, you can easily also ignore files that you don't have read access to:

find thefolder -type f -readable -exec grep -q -I . {} ';' -print

The only difference here is that you would avoid a few "permission denied" errors.

With grep implementation that can do recursive grepping:

grep -rlI . thefolder

Using file as you suggest:

find thefolder -type f -exec sh -c 'file "$1" | grep -qi text' sh {} ';' -print

This is very much slower than the first solution, but works if you trust file to work its magic correctly.

Ever so slightly quicker (only executes sh once or a few times):

find thefolder -type f -exec sh -c '
    for pathname do
        if file "$pathname" | grep -qi text; then
            printf "%s\n" "$pathname"
    done' sh {} +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.