74

I have a problem with Bash on Ubuntu on Windows. If I type "open (filename)" on Mac terminal, it opens the file with the right program but if I try to use it on Windows bash, it says: "Couldn't find a file descriptor referring to the console".

I have also tried xdg-open and gnome-open but none of them works. How can the issue be fixed, and how does the open command work?

starball
  • 20,030
  • 7
  • 43
  • 238
Pets
  • 741
  • 1
  • 5
  • 3

4 Answers4

73

Instead of open u can use xdg-open which does the same thing, independently of application i.e. pdf, image, etc. It will open a new virtual terminal (I have tried this on Linux)

Example:

xdg-open ~/Pictures/Wallpapers/myPic.jpg

xdg-open ~/Docs/holidays.pdf

Winter
  • 3,894
  • 7
  • 24
  • 56
Sharath BJ
  • 1,393
  • 1
  • 12
  • 16
43

For linux, use xdg-open. open is for Mac OS. open in linux is an name alias of openvt (open virtual terminal).

To simplify it, you can append the following line to ~/.bashrc or ~/.zshrc depends on the shell you are using.

alias o="xdg-open" # o stands for open 

Then next time you can just type like the following to reduce some keyboard strokes.

o file_name.pdf

Lance
  • 551
  • 4
  • 5
  • 1
    Off-topic: I also define my remote commands like that! `s` shows running jobs `p` prints most recent output `o` displays the file – Tijs Maas Dec 06 '18 at 15:27
  • Alternatively, define `open` as an alias or function to make all your normal scripts work alike on OS X and Linux. https://github.com/fatso83/dotfiles/commit/7d9b685e9ba1edba243fc89e78095b27c5ace37b#diff-faf0d10d25b6ac7fcd15598e065e4f66R21 – oligofren Mar 05 '19 at 12:08
24

That's because open is a Mac specific command, it is not available under Linux (ubuntu), Mac open can execute a file (if the file is executable), or open the file into a text editor (if it is a document or text file) or open a directory.

hcheung
  • 3,377
  • 3
  • 11
  • 23
  • Makes sense. But there is similar command on Linux and Ubuntu called "xdg-open" so I think there is a way to implement it on Windows bash as well @hcheung. I just installed xdg-utils package but it opens files and folders inside bash. Is there any way to open files through windows programs? – Pets Feb 26 '17 at 03:11
  • I'm window-free for more than 10 years! Hope someone could help. – hcheung Feb 26 '17 at 05:57
  • I have a `/bin/open` app on my Linux OS. There indeed is an `open` cmd for Linux. – Todd Sep 20 '18 at 18:04
0

For opening a file to write in Ubuntu on Windows bash, you can type -:

nano filename.txt

The above command will allow you to write/edit in the file after which, you can use ctrl+x and then press 'y' to save. Check/view the file content using -:

cat filename.txt