Home > OS >  Finding file names that contains 2 characters and then finding which of those has a number in it
Finding file names that contains 2 characters and then finding which of those has a number in it

Time:03-29

I have a task that I need to find every /usr/bin file that contains 2 characters. I do that by using: ls /usr/bin |grep -x .. and then I get:

7z ar as at bc cc dc du ex hg id ld m4 nc nl nm od pg pr sg tr ul vi wc xz.

And now I need to select which of these file names contains numbers, how do I do that?

I can't use find command becuz the task strictly says that I need to use grep or maybe even awk. Thanks for helping in advance.

CodePudding user response:

Starting with your approach, you could pipe the output from your grep into a second grep which selects only lines that have at least one digit:

 ls /usr/bin |grep -x ..|grep `[0-9]`

Of course your approach is already flawed a bit in the sense that if you happen (just as example) in /usr/bin a file containing a newline character, this would not be processed correctly, because grep sees the input as a set of lines....

  • Related