Mac OS X Terminal: Search output of another command with grep

1

I am trying to get the output of an ls command and search that list for a regex expression... Effectively, it will search every file name in the folder for the regex expression. I put together a simple example of what I thought was the right way to do this, but clearly was not:

$ ls ~/Desktop/testFolder | grep -rn "contents"

I know, I can make a script for this where I pipe the output from the 'ls' to a text file, and then grep that.. but I am asking how to do this WITHOUT a multistep process or script.

FALL3N

Posted 2011-06-30T01:10:23.820

Reputation: 431

Answers

4

Your command should work, although you do not need the -r option in the grep command. What happens when you try running your command that is not what you expected?

ls /path | grep string

The above works for me.

You could also try looking at the find command:

find ~/Desktop/testFolder -iname '*contents*'

Hope this helps and good luck!

baraboom

Posted 2011-06-30T01:10:23.820

Reputation: 784

thanks a lot guys! and those were some fast damn responses! – FALL3N – 2011-06-30T01:25:38.723

2

Your only mistake is setting the -r option on grep.

Spiff

Posted 2011-06-30T01:10:23.820

Reputation: 84 656

1

Do you want: ls ~/Desktop/testFolder/*contents*

glenn jackman

Posted 2011-06-30T01:10:23.820

Reputation: 18 546

yes, but I understand how a regular 'ls' works, no problems there – FALL3N – 2011-07-07T04:38:28.273