Question: 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?

Answer: 
You can use `GNU find`:

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

```
Using [perl](/questions/tagged/perl "show questions tagged 'perl'") (portable and no problem with space in filenames) :

```
perl -Mautodie -wle '
    for (<"*.txt">) {
        open my $fh, "<", $_;
        my @list = <$fh>;
        close $fh;
        if (grep { /stringA/ } @list and grep { /stringB/ } @list) {
            $h->{$_}->{size} = (stat($_))[8];
        }
    }
    END{
        print join "\n", sort { $h->{$a}->{size} <=> $h->{$b}->{size} } keys %$h;
    }
'

```