Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
1  
Wouldn't open *feedback* be enough, actually? –  slhck Jan 4 '13 at 18:18
add comment

1 Answer

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.

share|improve this answer
    
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 '13 at 18:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.