Search code examples
linuxbashgrepgnu

How do I change the order in which grep looks at files/prints results?


I have a directory with a bunch of files with numerical filenames. They don't have leading zeroes, so if I do something like grep hello * in that directory I might get something like this:

22:hello, world!
6:hello
62:"Say hello to them for me."

I'd rather have the result be like this:

6:hello
22:hello, world!
62:"Say hello to them for me."

The first thought that occured to me was to sort the results numerically with grep hello * | sort -n but then I lose grep's colors, which I'd like to keep. What's the best way to do that?


Solution

  • ls * | sort -n | xargs -d '\n' grep hello