Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is probably an easy one, but I can't figure it out and it's pretty much not searchable. In a folder hierarchy I have exactly one file of type xyz. I want to find that file and open it with a terminal command.

find . -name *.xyz

This will return the file I'm looking for. Now how do I open it automatically, without typing the name?

find . -name *xyz | open

This doesn't work. It says it doesn't found the open command.

share

migrated from stackoverflow.com Oct 16 '11 at 16:20

This question came from our site for professional and enthusiast programmers.

3  
What kind of file is it? If its not an executable file, what program do you want to open the file with? Oh what Windows has done to our population =/ –  chown Oct 14 '11 at 20:43
    
This probably belongs on superuser... –  retracile Oct 14 '11 at 20:46
1  
I'm on a Mac not Windows... and I want to open the file with the default program. –  DrummerB Oct 14 '11 at 21:11
1  
find . -name *xyz | xargs open and find . -name *.xyz -exec open {} \; both worked. Thanks. –  DrummerB Oct 14 '11 at 21:12
    
I'm glad you found a solution. Do not forget to mark the solution that worked 'best' for you. Others will see your 'acceptance' ratio and this goes a long way when asking future questions. Users with a lower acceptance ratio often find their questions go ignored. =) –  matchew Oct 14 '11 at 21:50

4 Answers 4

up vote 4 down vote accepted

@retracile is correct. You need to open it with 'something'. However, I prefer to use exec over xargs.

find . -name '*.xyz' -exec cat {} \;

this will return cat fileFound.xyz; cat fileFound2.xyx; etc.. however, you are only expecting to find one file.

note that changing \; to + would return cat fileFound.xyz fileFound2.xyz depending on case the later maybe the preferred choice.

for more on this I would direct you to this question

share

You need to know what program you want to use to open the file. If it's an image, you could use gwenview or something.

Taking input and turning it into arguments is done with xargs. And you'll want to escape that wildcard...

find . -name \*xyz | xargs gwenview

And to handle spaces in filenames....

find . -name \*xyz | xargs -d"\n" gwenview
share

The command you're looking for is called xdg-open

Try your command with it:

xdg-open "$(find . -name '*xyz' | head -n1)"
share

As others have pointed out, your question isn't quite specific enough to answer clearly. So, guessing at another possible meaning: If you mean that you want to run it as an executable, (meaning that your program is an executable) tell the shell that you want to substitute the output of the find command as part of the command line:

$(find . -name *.xyz)
share

This site is currently not accepting new answers.