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?

2
  • 1
    This might be the problem. Open probably doesn't accept standard in, so try open $(ls | grep navigation-drawer-config-) Commented Jan 12, 2016 at 19:00
  • 1
    Don't use ls here
    – tripleee
    Commented Apr 18, 2018 at 4:41

2 Answers 2

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.

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".

1
  • xargs is a good idea in general; but for this particular question, you should of course not be using ls | grep in the first place.
    – tripleee
    Commented Apr 20, 2018 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .