Results 1 to 7 of 7

Thread: Check file type command?

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Beans
    471
    Distro
    Ubuntu 11.04 Natty Narwhal

    Check file type command?

    I'm working on a script to mount iso or img file, by combining these two
    http://ubuntuforums.org/showpost.php...20&postcount=2
    http://www.ubuntugeek.com/mount-isos...-nautilus.html

    Since it feel slightly unnecessary to create two script, do i wonder how I can determine if the file is img, iso or neither?

  2. #2
    Join Date
    Apr 2010
    Location
    Denver, Colorado
    Beans
    802
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Check file type command?

    ls -F *

    *=File location

    Will list the file extension not sure if thats what your looking for or not?
    KDE SC 4.4

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Beans
    1,805

    Re: Check file type command?

    The easiest would be to use the file extension. A simple script can look like this:
    Code:
    #!/bin/sh
    
    if [ -f $1 ]; then
      case $1 in
        *.img) mount -t udf -o loop $1 /media/temp ;;
        *.iso) mount -t iso9660 -o loop $1 /media/temp ;;
        *) echo "Unrecognized file" ;;
      esac
    fi
    It tests that the command line argument is an existing file and then checks if the file name ends with .img or .iso or just anything.
    ...

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Check file type command?

    Use the file command:
    Code:
    file -b path/to/file
    I didn't test it, but something like this should work:
    Code:
    FILE=path/to/file
    ISO="ISO 9660 CD-ROM filesystem data 'CDROM                          ' (bootable)"
    IMG="output of the file command for an img file"
    TYPE="$(file -b "$FILE")"
    
    if [ "$TYPE"=="$ISO" ]; then 
      ...
    elif [ "$TYPE"=="$IMG"]; then 
      ...
    else 
      ...
    fi

  5. #5
    Join Date
    Sep 2007
    Beans
    471
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Check file type command?

    I tried using ls -F /path/to/file but it only return the same. file -b works better it return following for a img file. I think it can work if it's possible to remove the 'Title' part.
    Code:
    # UDF filesystem data (version 1.5) 'Title'

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Beans
    1,805

    Re: Check file type command?

    Quote Originally Posted by SpinningAround View Post
    I tried using ls -F /path/to/file but it only return the same. file -b works better it return following for a img file. I think it can work if it's possible to remove the 'Title' part.
    Code:
    # UDF filesystem data (version 1.5) 'Title'
    You can extract the first (say) three characters with cut:
    Code:
    file -b path/to/file | cut -c1-3
    This should then give you either 'UDF' or 'ISO'.
    ...

  7. #7
    Join Date
    Sep 2007
    Beans
    471
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Check file type command?

    Thanks for all the answers, I'm almost got the script working but there is some problem regarding '$*'. It return the absolute path to the file instead of only the file name.

    PHP Code:
    #!/bin/bash
    #
    # nautilus-mount-iso

    gksudo -u root -/bin/echo "Enter your password for root terminal access"
    zenity --info --text "TEST /media/$*/"
    TYPE "$(file "$*" | cut -c 3-6)"
    if [-/media/"$*" ]; then
        
    if [ zenity --question --title "ISO Mounter" --text "Unmount $*?" ]
            
    sudo umount "$*" 
            
    zenity --info --text "Successfully unmounted /media/$*/"
            
    sudo rmdir "/media/$*/"
            
    exit 0
        
    else
            exit 
    1
        fi
    else
        
    sudo mkdir /media/"$*"
        
    if [ "$TYPE"==ISO ]; then
            
    if [ sudo mount -o loop -t iso9660 "$*" /media/"$*" ]; then
                zenity 
    --info --title "ISO Mounter" --text "$* Successfully Mounted."
                
    exit 0
            
    else
                
    sudo rmdir /media/"$*"
                
    zenity --error --title "ISO Mounter" --text "Cannot mount $*!"
                
    exit 1
            fi
        elif 
    "$TYPE"==UDF ]; then
            
    if [ sudo mount -t udf -o loop $/media/"$*" ]
                
    zenity --info --title "ISO Mounter" --text "$* Successfully Mounted."
                
    exit 0
            
    else
                
    sudo rmdir /media/"$*"
                
    zenity --error --title "ISO Mounter" --text "Cannot mount $*!"
                
    exit 1
            fi
        
    else
            
    sudo rmdir /media/"$*"
            
    zenity --error --title "ISO Mounter" --text "Cannot mount $*!"
            
    exit 1
        fi
    fi 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •