I'm getting an issue on piping in linux,

ls | grep feedback

I have an alias for xdg-open which is open, how can do to make the filename from grep goes directly to open

this is not working

ls | grep feedback | open
  • 1
    Wouldn't open *feedback* be enough, actually?
    – slhck
    Jan 4, 2013 at 18:18

1 Answer 1


xargs builds and execute command lines from standard input.

The syntax is simple:

ls | grep feeback | xargs open

This will not work if the file name contains a space. For example, for a file called foo bar:

ls | grep foo | xargs open

The above would attempt to open a file called foo and then a file called bar, since the space usually separates arguments.

In this case, you can use quotes to delimit the filename:

ls | grep foo | xargs -I '{}' open '{}'

However, this approach may fail if the filename contains spaces.

Another option would be using find, which avoids pipes altogether:

find -maxdepth 1 -name '*foo*' -exec open '{}' \;

Here, -maxdepth 1 makes find search only in the current directory (i.e., not including subdirectories), -name '*foo*' searches for files containing the string foo in their name and -exec open {} \;, replacing {} with the filename that has been found. find will deal automatically with special characters.

Through command substitution, a similar result can be achieved:

open "$(ls | grep feedback)"

This will work, even if the file contains spaces.

  • Caveat when the file names contains spaces. I added that to your answer. If you think the change is too radical, feel free to roll back.
    – slhck
    Jan 4, 2013 at 18:28

You must log in to answer this question.

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