38

Ubuntu noob here. How do I open a file in a working directory? I have already tried

./MyFile.jpg

And it replies with

bash: ./MyFile.jpg: Permission denied

Now, that got me thinking that I had to go and change the permissions. So I used chmod to make the file permission "rwx" (I'm not sure if making executable permissible is going to do what I want, it just seemed logical) and tried

./MyFile.jpg

But this returned

bash: ./MyFile.jpg: cannot execute binary file

Which makes me think the chmod was not the right way to go. So I changed the permission back so that it was not "rwx", but just "rw-". Now I'm stuck feeling pretty foolish because I can't open a simple file in Terminal. I can open it by not using Terminal and double clicking on the file while on the Desktop, but I want to teach myself how to navigate using the Terminal. I also tried

sudo ./MyFile.jpg

but that didn't work either. Any help?

6
  • 3
    You're trying to open a JPG Image file as an executable with the shell (bash) of course it doesn't work. The correct command would be eog Myfile.jpg. Jan 5, 2013 at 17:29
  • .jpg file? you want to execute an image file? no you cannot.
    – nickanor
    Jan 5, 2013 at 17:30
  • @nickanor a file ending in .jpg can be an executable. Linux generally does not check the last part of a file ;)
    – Rinzwind
    Jan 5, 2013 at 21:54
  • 3
    @UriHerrera I think the question is How would I discover that "eog" is the program I need to open this file?
    – Izkata
    Jan 6, 2013 at 2:50
  • 1
    +1 because despite having used ubuntu for years, I occasionally hit the same problem with a file type I hadn't seen before.
    – Izkata
    Jan 6, 2013 at 2:52

3 Answers 3

49

If you want to use the default program for opening a file you can use

gnome-open YourFile

or (as Florian Diesch mentioned in a comment) you can use the following command which also works on every XDG-compliant desktop environment (including KDE, LXDE, and XFCE):

xdg-open YourFile

You have to use a command to open a file. In your case you could use

eog MyFile.jpg

No need to mess with permissions. Eog is the eye of gnome. Type man eog for more information. It can open the following image files: ani, bmp, gif, ico, jpeg, pcx, png, pnm, ras, svg, tga, tiff, wbmp, xbm, and xpm.

For other file types you need other commands. For example:

  1. A pdf file called YourMajesty.pdf can be opened with evince YourMajesty.pdf.
  2. A text file named MyFile.txt can be opened using gedit MyFile.txt and that also holds for many other files containing text (but not having a .txt extension).

If you add a & at the end of your command (for instance, eog MyPhoto.jpg &) you can still continue using the same terminal for other commands.

2
  • 3
    I've found that aliasing xdf-open to the simple command go works wonders. In Ubuntu you can open a graphical file folder at the current directory simply by typing go ..
    – Hooked
    Jan 7, 2013 at 19:57
  • gnome-open is good!
    – hiway
    Apr 29, 2014 at 8:44
10

If you want to open a file, use gnome-open. Think of it as equivalent to double-clicking on the file in Nautilus.

gnome-open MyFile.jpg

If you want to execute a script or a program, then make its execute permission is set, and type the filename prefixed with the path: (You would never do this with a JPG image, because it is not an executable program.)

./executable-name
1
  • 8
    if you use xdg-open instead of gnome-open it works on KDE and XFCE, too Jan 5, 2013 at 17:40
4

In the command line it is not possible to just mention a file and have automatically start the appropriate program with that file. So you must invoke that program (in this case you can use "Eye of Gnome" hence eog).

Try

eog Myfile.jpg

You can also get back the control of your terminal immediately if you tell eog to launch "in the background" by invoking it with eog Myfile.jpg &.

Another hint: use tab to let the terminal autocomplete your input.

1
  • This answer is incorrect. xdg-open does this.
    – lindhe
    Aug 21, 2015 at 15:20

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