Bash – grep for multiple strings in files, and then list the files in the order of size

bashgrepshell

I am in a folder with lots of .txt file, I would like to find all the files which contain both stringA and stringB (not necessarily on the same line), then list these files in the order of the size (from small to big)

I have tried the follows, but it doesn't work:

ls -lS | for f in *; do grep -q stringA $f && grep -l stringB $f; done

Does anyone have a good idea?

Best Answer

You can use GNU find:

find . -maxdepth 1 -exec grep -q stringA {} \; -exec grep -q stringB {} \; \
        -printf '%10s %p\n' | 
    sort -n
Related Question