Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: unzip a file with a name containing a string

  1. #1
    Join Date
    Jul 2012
    Location
    Seattle
    Beans
    45
    Distro
    Ubuntu

    unzip a file with a name containing a string

    Hello, this isn't exactly a problem as something I don't know how to do.

    I want to be able to make a .sh script that will take out the file "worldedit.jar" from a ziped folder which is named worldedit-<versionnumber>.

    My problem is that I don't know how to do any of this.
    Basically there will be a zipped folder whose name will be worldedit with a few numbers after it in the /home/newlanders/server/update/ folder.
    I just want to get the file worldedit.jar out of the zipped folder, so I don't have to do it myself unzipping the whole thing and then deleting the files that I don't need that were in that folder. I am going to be including this in a crontab job script to be run automatically, right before it transfers all .jar files from the /home/newlanders/server/update/ folder. (most updates are just jars, only worldedit comes ina zip and I need to get the jar out of it.)
    I will be deleting the zip folder after I get worldedit.jar out of it.

    Thanks in advance for anyone who can/will help.

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: unzip a file with a name containing a string

    I'm not sure that I truly understand what you are asking for, but I'll take a guess.
    Code:
    cd /home/newlanders/server/update
    unzip worldedit1234.zip worldedit.jar
    Is that it?

  3. #3
    Join Date
    Jul 2012
    Location
    Seattle
    Beans
    45
    Distro
    Ubuntu

    Re: unzip a file with a name containing a string

    That would work if it was named worldedit1234 every time, but it won't have the same numbers after its name every time... I need a way to unzip any zip file who's name contains worldedit, whether it be worldedit1234 or worldedit7398.

  4. #4
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: unzip a file with a name containing a string

    if there is 1 file with a name worldedit????.zip, then you can use
    Code:
    unzip worldedit*.zip worldedit.jar
    bash will replace "worldedit*.zip" with a list of files that match the pattern.

    if there are several files with a name that matches, then you can filter out the first (or last), with something like
    Code:
    unzip `ls worldedit*.zip | head -n1` worldedit.jar
    the backticks (`) tell bash to run the command within them, and use their output as part of a command. "ls worldedit*.zip" gets all the files with a name that matches, and "| head -n1" acts as a filter that only allows the first answer though. use 'tail' instead to get the last.

  5. #5
    Join Date
    Jul 2012
    Location
    Seattle
    Beans
    45
    Distro
    Ubuntu

    Re: unzip a file with a name containing a string

    Quote Originally Posted by ssam View Post
    if there is 1 file with a name worldedit????.zip, then you can use
    Code:
    unzip worldedit*.zip worldedit.jar
    bash will replace "worldedit*.zip" with a list of files that match the pattern.

    if there are several files with a name that matches, then you can filter out the first (or last), with something like
    Code:
    unzip `ls worldedit*.zip | head -n1` worldedit.jar
    the backticks (`) tell bash to run the command within them, and use their output as part of a command. "ls worldedit*.zip" gets all the files with a name that matches, and "| head -n1" acts as a filter that only allows the first answer though. use 'tail' instead to get the last.
    Thanks so much for this, It looks like it will work. I haven't tried it yet but it seems exactly what I need. I thought that the * character would only do that when I was using it like something.*.something but it will work with something*something? That is pretty cool, thanks. I will try it in a little bit and edit this post with my results.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: unzip a file with a name containing a string

    I thought that the * character would only do that when I was using it like something.*.something but it will work with something*something?
    depends on the context. In fullblown regexes used by grep, sed, awk, perl, etc * is a 0+ modifier to the symbol before it, eg
    .* = <any char>(0 or more times)
    while in case of shell patterns (globs) * is a standalone <any sequence>

    Code:
    unzip `ls worldedit*.zip | head -n1` worldedit.jar
    unfortunately this line reinforces bad practices.
    ls is for human eyes not for scripts. It should not be parsed.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Jul 2012
    Location
    Seattle
    Beans
    45
    Distro
    Ubuntu

    Re: unzip a file with a name containing a string

    Ok, thanks. This works now. I have taken all that and written this:
    Code:
    PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    #!/bin/bash
    cd /home/newlander/server/update/
    if [ -a `ls worldedit*.zip | tail -n1` ]
    then
        unzip `ls worldedit*.zip | tail -n1` WorldEdit.jar
        rm worldedit*.zip
    fi
    That works great!

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: unzip a file with a name containing a string

    a bit messy, hashbang line (#!/bin/bash) should be the very first line.
    and i'd do that 'unzip the last found archive' like this to avoid parsing LS

    Code:
    for f in worldedit*.zip; do continue; done
    [ -f "$f" ] && unzip "$f" WorldEdit.jar
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Jul 2012
    Location
    Seattle
    Beans
    45
    Distro
    Ubuntu

    Re: unzip a file with a name containing a string

    Quote Originally Posted by Vaphell View Post
    a bit messy, hashbang line (#!/bin/bash) should be the very first line.
    and i'd do that 'unzip the last found archive' like this to avoid parsing LS

    Code:
    for f in worldedit*.zip; do continue; done
    [ -f "$f" ] && unzip "$f" WorldEdit.jar
    Code:
    #!/bin/bash
    PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    
    cd /home/newlander/server/update/
    for f in worldedit*.zip; do continue; done
    if [ -f "$f" ] 
    then
         unzip "$f" WorldEdit.jar
         rm "$f"
    fi
    Like that?
    I am pretty much a beginner in bash.

  10. #10
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: unzip a file with a name containing a string

    I am not sure that ls is any worse to parse than any other command, though globbing in the if statement could be faster.

    both methods are going to give problems if any of the filenames have spaces in, or even worse if they contain characters like '*' or '\n'. see http://unix.stackexchange.com/questi...s-in-the-names for examples of getting around this.

Page 1 of 2 12 LastLast

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
  •