1

I'm writing a humble file manager and am seeking a list of different file types and what default applications Ubuntu uses to open them with.

So far I've only figured out "text" open with gedit, "image" open with eog (eye-of-gnome) and "audio" open with ogg123. I'm not sure if ogg123 is a default application though.

I'd appreciate a little help expanding the list in the code below:

function OpenFileWithMagic () {

local FileType
FileType=$(file -i "$DfName/$2" | grep -oP '(?<=: ).*?(?=/)')
logger "bafman - OpenFileWithMagic - FileType: $FileType DfName: - $DfName parm 1: $1 2: $2 3: $3 4: $4"

case $FileType in

text)
    # TODO: expand list of file types and associated applications to open them with
    if [[ $4 == root ]]; then
        gsu gedit $DfName/$2
    else
        gedit $DfName/$2
    fi ;;
image)
    eog $DfName/$2 ;;
audio)
    ogg123 $DfName/$2 ;;
esac

} ### OpenFileWithMagic ()
export -f OpenFileWithMagic

Notes: export is only required because file is opened when double-clicking from yad dialog box. I also need help figuring out "magic" numbers and how one might interpret them. Perhaps an educating link?


Edit This is not a duplicate of How to open file with default application from command line? because:

  • That question is how to open a file from the command line. This question is how do it from a bash script within a case structure.
  • The answer there would result in rhythembox being used for sound files which is not wanted. ogg123 is more suited for playing sounds without a new window being opened and closing with python error messages.
  • The answer there would use xdg-open /bin/mv resulting in:

    gvfs-open: /bin/mv: error opening location: No application is registered as handling this file
    
  • With the above bash script the file -i /bin/mv command results in:

    /bin/mv: application/x-executable; charset=binary
    

After filtering out processing for all desired file types within the bash script then the left overs could be passed to xdg-open which is used in the duplicate candidate.


Reply to comments As pointed out by DK Bose major file types can be found in /usr/share/mime/types. Here there are additional types to be considered such as Video (a suitable default Ubuntu video player needs to be sourced), Application (rather than running it, info about the application needs to be displayed) and Message (I have no clue how to display this). Additional mime types needing further research are inode, model, multipart and x-content.

rinzwind pointed out ogg123 is not a default sound file player in Ubuntu. A suitable installed-by-default Ubuntu application to play sound files is canberra-gtk-play as answered October 5, 2016 by wjzndrea in this Q&A: Can line draw characters (or colors) be added to a Bash file list menu?.

  • Have you looked at the contents of /usr/share/mime? For example, there's /usr/share/mime/types. – Justice for Monica May 6 '17 at 1:52
  • @DKBose No I've never heard of it but I'll certainly check it out. Thank you. – WinEunuuchs2Unix May 6 '17 at 2:01
  • 2
    "I'm not sure if ogg123 is a default application though." Nope. – Rinzwind May 6 '17 at 7:30
  • @Rinzwind thanks. Someone mentioned the default one on another Q&A I asked last year. I'll have to dig it up. – WinEunuuchs2Unix May 6 '17 at 15:00
  • XY Problem: You want to open files with the default program, and you think the way to solve it is by some silly bash script parsing the output of some command. – muru May 8 '17 at 8:01
10

Most modern desktop programs don't use file but the Shared MIME-info Database for file types and the MIME applications associations specification for the default applications.

In Bash you can use xdg-mime to guess the MIME type of a file and to get the default application for a given MIME type:

$ xdg-mime query filetype bla.sh
application/x-shellscript
$ xdg-mime query default application/x-shellscript
emacs.desktop

With xdg-open you can just open a file with the default application so you don't actually need to care about MIME types yourself:

$ xdg-open bla.sh
  • +1 for now but I need some time to check it out before accepting. I hope you understand. – WinEunuuchs2Unix May 6 '17 at 3:12
  • This answer is useful to change program design. Display file names with file type using xdg-mime query filetype beside it. Next put xdg-mime query default beside that. When double clicking file name use the existing file type instead of calling file -i as the current code uses. Answer accepted. – WinEunuuchs2Unix May 6 '17 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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