18

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.

  • 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. –  Oct 14 '11 at 21:11
  • 2
    find . -name *xyz | xargs open and find . -name *.xyz -exec open {} ; both worked. Thanks. –  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

6 Answers6

16

@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

matchew
  • 276
9

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
retracile
  • 191
  • find . -name "$1" -print | sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/" | xargs nvim. -print print the full file name on the standard output, followed by a new‐line. sed will surrounds each line with quotes to avoid adding -d flag to xargs which i believe is a gnu extention. ref to https://stackoverflow.com/a/1616589/13041067 – ali b Aug 04 '21 at 23:19
6

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

Try your command with it:

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

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)
mkj
  • 129
1

If you wanted to open all the files found by find in separate tabs in a single instance of vim, you could do this:

find . -name \*xyz | xargs vim -p
Cory Klein
  • 18,911
0

find . -name "hello.txt" -exec xdg-open {} +

I am using Linux and this command opened the text file in linux default text editor you might have to use open instead of xdg-open.

  • Welcome to the site, and thank you for your contribution. Please note that your answer seems to reiterate what was already stated in the accepted answer. You may want to consider expanding it so that the difference to that answer becomes more visible; otherwise it would be best placed as a comment to that answer (once you have sufficient reputation). – AdminBee Sep 21 '22 at 11:08