Find A Volume Using Terminal

Discussion in 'OS X' started by n-abounds, Apr 28, 2006.

  1. macrumors 6502a

    n-abounds

    Joined:
    Mar 6, 2006
    #1
    Is there any Terminal command that lets me find a volume with a certain keyword in it (say iPod), even if I don't know the full name of the volume. I'm doing this because I'd like to create a little app that locates another person's iPod, and can then change stuff inside the iPod. And how could I change directories into it?
    The "find" command doesn't seem to work.
     
  2. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #2
    Where were you running find from? All mounted volumes are under /Volumes. And how many volumes do you have mounted?
     
  3. thread starter macrumors 6502a

    n-abounds

    Joined:
    Mar 6, 2006
    #3
    I want to be able to find any volume with, say, "ipod" in the title of the volume. Then I want to change directory into that volume automatically with another command. Possible?

    I'm doing this because I want the Terminal command to be able to work on any computer, and still find an "iPod".
     
  4. tag
    macrumors 6502a

    tag

    Joined:
    Apr 29, 2005
    Location:
    PA, US
    #4
    Well you could approach this different ways, but the thing to remember is, not everyone is going to have the word iPod in the name of their actual iPod, so that can cause a problem. Because of this, just having a script navigate into /Volumes/ and look for the word iPod wouldn't cut it.

    To get around that the best way I can think of is by using 'system_profiler' to discover the device.
    Here is a quick shell script I wrote which locates the iPod, grabs it's name and path and then goes to it's directory. From there you can add on / modify it as you like to issue any other commands.

    This should at least give you an idea of one way it can be done.

    Code:
    #!/bin/ksh
    ipodcheck=`system_profiler SPUSBDataType SPFireWireDataType | grep  "iPod Unit:" -o -m 1 -c`
    getvolume=`system_profiler SPUSBDataType SPFireWireDataType | grep  "iPod Unit:" -A 20  | grep "/Volumes/"  | cut -f 2 -d ':'  | cut -c 2-`
    
    if [  "$ipodcheck" = "0" ]
       then print 'No iPod Connected.';exit
       else cd "$getvolume"
    fi
    
    #This below part is for testing purposes only
    #So now the script is currently in the attached iPods directory
    #The following executes 'ls' command to prove it
    ls -laTips
    #So now that you found and are in the iPods directory you can now add to
    #this script so it will cp/mv whatever files you want
    ###END OF SCRIPT###
     
  5. thread starter macrumors 6502a

    n-abounds

    Joined:
    Mar 6, 2006
    #5
    Hmm that last one went a bit over my head, and/or it doesn't seem to work for me. Any other suggestions?
     
  6. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    There are a couple of errors in that script. "print" should be "echo" and "exit" causes Terminal to exit.

    But this only works if the iPod is configured for disk use. If not, it is automatically unmounted after update, and even though the ipodcheck result is "1" (i.e. the Mac recognises that the iPod is still connected) there is no Volume.

    You need to check for "$ipodcheck" = "0" OR "$getvolume" = ""

    Try this:
    Code:
    #!/bin/ksh
    ipodcheck=`system_profiler SPUSBDataType SPFireWireDataType | grep  "iPod Unit:" -o -m 1 -c`
    getvolume=`system_profiler SPUSBDataType SPFireWireDataType | grep  "iPod Unit:" -A 20  | grep "/Volumes/"  | cut -f 2 -d ':'  | cut -c 2-`
    
    if [  "$ipodcheck" = "0" ] || [ "$getvolume" == "" ]
        then echo 'No iPod Connected.';
        else
            #So now that you found and are in the iPods directory you can now add to
            #this script so it will cp/mv whatever files you want
            cd "$getvolume";
            ls;
    fi
    
    ###END OF SCRIPT###
    
     
  7. tag
    macrumors 6502a

    tag

    Joined:
    Apr 29, 2005
    Location:
    PA, US
    #7
    Ok thats odd, the exit didn't cause a Terminal exit for me, just a script exit, not sure why (do you know why that did for you and not me? Just wondering, I mean the only reason I threw it in there was so it wouldn't execute the ls command if the check failed). Anyways I totally didn't think about my iPod being set up for disk use, that really does change things.

    So somehow you would need to have the script manually mount the iPod first (is that possible if it's not set up for disk use? I don't have my iPod with me right now to play around with it. I guess you'd have to find the mount points somehow?). Or state in the apps instructions that you have to enable disk mode for the app to work.
     
  8. thread starter macrumors 6502a

    n-abounds

    Joined:
    Mar 6, 2006
    #8
    Is there a simpler script if the other person does have "iPod" in the name of their iPod?
     
  9. tag
    macrumors 6502a

    tag

    Joined:
    Apr 29, 2005
    Location:
    PA, US
    #9
    Well yeah you could do this really simply, though I have to say if you want this to work with just a simple command line argument I don't think it's possible without creating at least one temporary file to store information so your best bet would be a script. Though maybe plinden can correct me if I'm wrong on that.

    So simpler as you want it.. with this following script which will find any variation of iPod (ipod IPOD ipOd etc.. excluding dot files) in /Volumes and then go to the directory.

    Like this for example..

    Code:
    #!/bin/ksh
    ipod=`ls /Volumes/ | grep -i iPod`
    
    if [ -n "$ipod" ];then
      cd /Volumes/"$ipod";
      ls;
     else echo 'Cannot find the iPod! OMG what now?! Buh bye.'
    fi
    NOTE: Some flaws can be found with this route..

    a)the users iPod must have the word iPod in the name

    b)if another drive is mounted with the word iPod in the name, conflicts arise (i.e. backup iPod music cd, etc) (though this could be remedied by checking to see if the directory has write access, though you would still have problems with CD/DVD-RW's and external drives)

    c)this wont work with non mounted ipods not in disk mode (of course neither does the prior script deal with this problem either)
     
  10. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #10
    Maybe, but the idea of the original script is good in that it's the simplest way of doing it effectively. Anything that makes the assumption of iPod in the name isn't going to be that much simpler and won't be as effective.

    The system_profiler check isn't that complicated. To learn about system_profiler, you can read its man pages (in Terminal type "man system_profiler").

    If you just want to list the names of all mounted Volumes containing "iPod" you can do this :
    Code:
    ls /Volumes | grep iPod
    Or you can use this to get information on the mounted volume:
    Code:
    disktool -l | grep iPod 
    But I don't know a way of mounting an iPod if it's not in disk mode.
     
  11. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #11
    /Volumes on my iBook has a habit of showing multiple copies of the same volume..
    Code:
    df | grep iPod
    
    works just fine for me :D

    Just as a heads up, neither of my iPods have 'iPod' in the name...so...that's a particularly bad way of looking for iPods.
     

Share This Page