0

I'm trying to open a large list of .xml files using the following command:

ls | grep navigation-drawer-config- | open

However, this doesn't work. From my understanding of piping, this should pipe what grep finds in the directory matching that name pattern into open, and open should open the files. I'm able to open them by manually doing something like so:

open file1 file2

However, when I use the first command, I get back the help screen for the open command. Are you not able to combine grep with open? Or am I missing something else?

user34547
  • 111

2 Answers2

1

The issue is that you are not adding the filenames to the open command, rather you are feeding the output of the grep into the open command.

There are a few ways to do this, including

open navigation-drawer-config-*

Which will use GLOB expansion and is probably the fastest and simplest approach.

Or

open `ls | grep "navigation-drawer-config-"`

Which executes the ls and grep and then puts the output as the parameters for the open command.

Or

find . -name "navigation-drawer-config-*" -exec open {} +

Which will look in the current directory AND ALL SUBDIRECTORIES for an file starting with navigation-drawer-config- and "open" them.

tripleee
  • 3,308
  • 5
  • 36
  • 35
davidgo
  • 73,366
0

If you are on UNIX or most Unix-like operating systems (Linux, BSD, mac os) you can use xargs which is used to create pipe arguments for a line that does not use pipe |; so for your example

ls | grep navigation-drawer-config- | xargs open

For info, the first argument of xargs is (in the example) open to which it will provide the result of grep from its standard input as command-line arguments and make a "exec "open navigation-drawer-config".