tags:

views:

102

answers:

4

Hi all,

I could have sworn you could do the following:

ls *.{java, cpp}

but that does not seem to work. I know this answer is probably on the site somewhere but I couldn't find it via search.

For instance, if I want to be able to use the globbing with a find command, I would want to do something like

find . -name "*.{java,cpp}" | xargs grep -n 'TODO'

Is this possible without resorting to using the -o binary operator?

A: 

But this does work in Bash:

$ ls
a.h  a.s  main.cpp  main.s
$ ls *.{cpp,h}
a.h  main.cpp

Are you sure you're in Bash? If you are, maybe an alias is causing the issue: try /bin/ls *.{java,cpp} to make sure you don't call the aliased ls.

Or, just take out the spaces in your list inside the {} -- the space will cause an error because Bash will see *.{java, as one argument to ls, and it will see cpp} as a second argument.

Mark Rushakoff
+1  A: 

ls *.{java,cpp} works just fine for me in bash...:

$ ls *.{java,cpp}
a.cpp    ope.cpp  sc.cpp  weso.cpp
helo.java   qtt.cpp  srcs.cpp

Are you sure it's not working for you...?

find is different, but

find -E . -regex '.*\.(java|cpp)'

should do what you want (in some versions you may not need the -E or you may need a -regextype option there instead, "man find" on your specific system to find out).

Alex Martelli
My `find` doesn't know about `-E` so I have to escape the parentheses and pipe character: `find . -regex '.*\(java\|cpp\)'` or use the `-regextype` option with one of these arguments: posix-awk, posix-egrep or posix-extended.
Dennis Williamson
Yep, I did mention that some versions (e.g. GNU I believe) want -regextype, others (e.g. BSD I believe) what -E instead.
Alex Martelli
+3  A: 

It is likely that you are seeing an error message such as this:

ls: cannot access *.{java: No such file or directory
ls: cannot access ,cpp}: No such file or directory

If that's the case, it's because of the space after the comma. Leave it out:

ls *.{java,cpp}

For future reference, it is more helpful to post error messages than to say "it's not working" (please don't take this personally. It's meant for everyone to see. I even do it sometimestoo often).

Dennis Williamson
I knew I was being stupid and that it had worked at some point in the past. You're right, it was the silly space I had put in there.Thank you
I82Much
A: 

For your particular example, this may also do what you want

grep -rn TODO . --include '*.java' --include '*.cpp'
gnibbler