Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Hello, I have created a directory and populated it with a zip file from a ftp server. The issue im having is if i unzip the file the given name ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    8

    Unzip problem please help

    Hello,

    I have created a directory and populated it with a zip file from a ftp server.
    The issue im having is if i unzip the file the given name of the unzipped file is not the same a the zipped file.

    for example (AllData_20110221_163857.zip) becomes (AllData_TMacID_sTime_eTime.csv)

    How can i name to unzipped file the same as the zipped file, so that i would get AllData_20110221_163857.csv?

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    The zip name is independent of the file and directorynames it contains.

    I would look at the process that creates those zips -or probably even before that- and enforce a naming convention by code and/or policy.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Mar 2011
    Posts
    8
    The zip files are created on a daily basis by an external server. my initial idea was to change the name of the file before it is zipped, but this cant be done so i need to find a workround.

    Can the unzipped file be renamed with the zip file name and the original zip file deleted?

    ***I should say that this zip file is one of many that are generated on a daily basis and always contain only 1 csv file*** I can elaborate more if needed.
    Last edited by gussy82; 03-01-2011 at 09:22 PM.

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    Yes, e.g. with a wrapper script, that saves the zip filename in a variable and uses it for a mv.
    But this is just a hack/workaround and also requires handling of various errorconditions.

    What if the zip is named incorrectly,
    if it contains multiple files,
    if the zip is corrupt,
    etc, etc

    It might be possible to omit the zip by using a ftp server/client with compression.

    Or dont use ftp in the first place in favour of e.g. rsync or scp.
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Mar 2011
    Posts
    8
    How would i phrase the wrapper script just for one unzip-rename-delete original zip file sequence?

    I assume i can then create a repeat loop that can work through multple zipped files in the directory, unzipping, naming and deleting the original?

    thanks

  6. #6
    Just Joined!
    Join Date
    Mar 2011
    Location
    Wales
    Posts
    15
    Hi.

    Saw your post & rustled up the following:

    Code:
    #!/bin/sh
    
    for FILE in *.zip;                      # Do the following for all zip files
    do 
      mkdir .$FILE.tmp                      # Make a temporary hidden directory
      cd .$FILE.tmp                         # ...So that there are no mix-ups
      unzip ../$FILE                                # Unzip the file into it
      NEWFILE=`echo $FILE | sed s/.zip$/.csv/`      # Work out the new filename 
      mv *.csv ../$NEWFILE                          # Move & rename the file
      cd ..                                 # Leave the temporary directory 
      rmdir .$FILE.tmp                      # Remove the temporary directory
    done
    ...But make sure you thoroughly check it works on something non-crucial first --- it's late & my brain's probably not firing on all cylinders...
    Last edited by Ignotum; 03-02-2011 at 05:20 PM. Reason: Prettified comments in script.

  7. #7
    Just Joined!
    Join Date
    Mar 2011
    Location
    Wales
    Posts
    15
    ...Any joy with that script, Gussy, old chap?

    (Now I've had some sleep and looked over it again, I think it should indeed do what you want to do: just make sure you test it first in a temporary directory on some expendable copies of your zip files.)

  8. #8
    Just Joined!
    Join Date
    Mar 2011
    Posts
    8
    Hello Ignotum....

    The script worked perfectly. thankyou for your help! Its much appreciated.

  9. #9
    Just Joined!
    Join Date
    Mar 2011
    Location
    Wales
    Posts
    15
    Jolly good: glad to have helped!

  10. #10
    Just Joined!
    Join Date
    Mar 2011
    Posts
    8
    Hello,

    I might of found a bug in the script. There seems to be an issue with the unzip command. It seems to be partially unzipping some of the files + populating some with random data.
    I have tested this againt my standard front end unzip package and traced the fault to the script. I should say that im doing this on a intel mac... :-/.. i hope that doesnt upset anyone, since a lot of the developer guys i work with think im a moron for using one.

    These files unzip to 26 meg so there not massive, the script is pretty much as above.

    Any ideas? could i try gunzip instead?

    cd ./desktop/raptor
    for FILE in *.zip; # Do the following for all zip files
    do
    mkdir .$FILE.tmp # Make a temporary hidden directory
    cd .$FILE.tmp # ...So that there are no mix-ups
    unzip ../$FILE # Unzip the file into it
    NEWFILE=`echo $FILE | sed s/.zip$/.csv/` # Work out the new filename
    mv *.csv ../$NEWFILE # Move & rename the file
    cd .. # Leave the temporary directory
    rmdir .$FILE.tmp # Remove the temporary directory
    done
    rm *.zip

Page 1 of 2 1 2 LastLast

Posting Permissions

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