Script to rename zip-files with name of file

Hi,
I'm desperately in search for a solution of the following problem:
I have a directory full of zip-files. All these zip-files contain a single file with a name that should be used for the name of the zip-container.

Anybody a good idea. I'm an absolute beginner in shell scripting - so please bear with me.

Thanks
Mark

the below two commands will do your task.

example :

one.zip contains abc file, then it will create a abc.zip and abc.zip file contains abc file

for i in `ls *.zip`; do unzip $i; done
for i in `ls | grep -v ".zip"`; do zip $i $i;done

You can do something like that :

$ cat mark.sh
for zip in *.zip
do
   file="$(unzip -l -qq "${zip}" | sed 's/.*:[0-9]\+[[:space:]]*//')"
   newzip=$(echo "${file}" | sed 's/ /_/g').zip
   echo "Zip '${zip}' contains file  '${file}', renamed to '${newzip}'"
   mv ${zip} ${newzip}
done
exit

Intial zip files

$ ls *zip
m1.zip  m2.zip  m3.zip
$ unzip -l -qq m1.zip
    582  05-10-10  16:37   mark_1
$ unzip -l -qq m2.zip
   4249  05-10-10  16:37   mark_2.txt
$ unzip -l -qq m3.zip
    631  05-10-10  16:43   mark with spaces
$

Script execution:

$ ./mark.sh
Zip 'm1.zip' contains file  'mark_1', renamed to 'mark_1.zip'
Zip 'm2.zip' contains file  'mark_2.txt', renamed to 'mark_2.txt.zip'
Zip 'm3.zip' contains file  'mark with spaces', renamed to 'mark_with_spaces.zip'
$

New zip files :

$ ls *.zip
mark_1.zip  mark_2.txt.zip  mark_with_spaces.zip
$ unzip -l -qq mark_1.zip
    582  05-10-10  16:37   mark_1
$ unzip -l -qq mark_2.txt.zip
   4249  05-10-10  16:37   mark_2.txt
$ unzip -l -qq mark_with_spaces.zip
    631  05-10-10  16:43   mark with spaces
$

Jean-Pierre.

Sorry,
my description was not entirely correct. Alle zip-files contain a large amount of files but always a file with the name ########.jdf,
where ######## is always a 8-digit number. That filename should be used as the zipfiles name. I suppose that makes it a little bit more complicated.
Is 'sed' still the way to go or is 'grep' more suitable?

Thanks for your kind help

Mark

New version of the mark.sh script :

for zip in *.zip
do
   jdf="$( unzip -l -qq "${zip}" |
           sed -n 's/.*:[0-9]\+[[:space:]]*\([0-9]\{8\}\)\.jdf$/\1/p')"
   if [ -z "${jdf}" ]
   then
      echo "Invalid zip file '${zip}', jdf file not found"
      continue
   fi
   newzip=${jdf}.zip
   echo "Zip '${zip}' contains file  '${jdf}.fdf', renamed to '${newzip}'"
   mv ${zip} ${newzip}
done
exit

Zip files:

$ ls *.zip
m1.zip  m2.zip  m3.zip
$ unzip -l -qq m1.zip
      172  05-10-2010 21:27   mark_1a.txt
     1285  05-10-2010 21:27   mark_1b.txt
        0  05-10-2010 21:27   12345678.jdf
$ unzip -l -qq m2.zip
      216  05-10-2010 21:30   mark_2a.txt
     1562  05-10-2010 21:31   mark_2b.txt
        0  05-10-2010 21:27   87654321.jdf
$ unzip -l -qq m3.zip
      268  05-10-2010 21:50   mark_3.txt
$

Script execution :

$ ./mark.sh
Zip 'm1.zip' contains file  '12345678.fdf', renamed to '12345678.zip'
Zip 'm2.zip' contains file  '87654321.fdf', renamed to '87654321.zip'
Invalid zip file 'm3.zip', jdf file not found
$

New zip files :

$ ls *.zip
12345678.zip  87654321.zip  m3.zip
$ unzip -l -qq 12345678.zip
      172  05-10-2010 21:27   mark_1a.txt
     1285  05-10-2010 21:27   mark_1b.txt
        0  05-10-2010 21:27   12345678.jdf
$ unzip -l -qq 87654321.zip
      216  05-10-2010 21:30   mark_2a.txt
     1562  05-10-2010 21:31   mark_2b.txt
        0  05-10-2010 21:27   87654321.jdf
$ unzip -l -qq m3.zip
      268  05-10-2010 21:50   mark_3.txt
$

Jean-Pierre.

Jean-Pierre,

wow, what a fast reaction. I really appreciate your efforts. I still try to figure out the sed command. Unfortunatley it doesn't work for me.

My output of unzip -l -qq is like this:

Archive:  ./test.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
    44145  12-29-09 11:33   01090230.jdf
 --------                   -------
    44145                   1 file

sed -n 's/.*:[0-9]\+[[:space:]]*\([0-9]\{8\}\)\.jdf$/\1/p'

comes up empty.: Invalid zip file 'test.zip', jdf file not found

BTW, what happens when the *.jdf is stored in a directory tree like this:

42262  04-16-10 11:13   System/Connector/JDF-Import/01100850.jdf
    0  04-16-10 11:47   System/Connector/JDF-Import/
    0  04-16-10 11:13   System/Connector/
    0  04-16-10 11:13   System/ContentHotfolder/Saved/
    0  04-16-10 11:13   System/ContentHotfolder/
    0  04-16-10 11:13   System/Documents/Current/

Mark

The folllowing new version of the script handles file names with directory specification. The output of your unzip command isn't a problem.

for zip in *.zip
do
   jdf=$( unzip -l -qq "${zip}" |
          sed -n 's=.*[/ ]\([0-9]\{8\}\)\.jdf[ ]*$=\1=p' )
   if [ -z "${jdf}" ]
   then
      echo "Invalid zip file '${zip}', jdf file not found"
      continue
   fi
   newzip=${jdf}.zip
   echo "Zip '${zip}' contains file  '${jdf}.jdf', renamed to '${newzip}'"
   mv ${zip} ${newzip}
done
exit

If the sed command doesn't work, try this modification :

   jdf=$( unzip -l -qq "${zip}" |
          awk -F'[ /]' '{ if (sub(/\.jdf$/, "", $NF)) print $NF}' )

Zip files :

$ ls *.zip
m1.zip  m2.zip  m3.zip
$ unzip -l -qq m1.zip
      0  05-11-10  09:02   dir1/
      4  05-11-10  09:02   dir1/12345678.jdf
    467  05-11-10  09:01   dir1/mark_1a.txt
     24  05-11-10  09:02   dir1/mark_1b.txt
$ unzip -l -qq m2.zip
      0  05-11-10  09:21   dir2/
     37  05-11-10  09:21   dir2/87654321.jdf
     29  05-11-10  09:21   dir2/mark_2a.txt
    770  05-11-10  09:21   dir2/mark_2b.txt
$ unzip -l -qq m3.zip
      0  05-11-10  09:28   dir3/
    125  05-11-10  09:28   dir3/mark_3a.txt
$

Script execution :

$ ./mark2.sh
Zip 'm1.zip' contains file  '12345678.jdf', renamed to '12345678.zip'
Zip 'm2.zip' contains file  '87654321.jdf', renamed to '87654321.zip'
Invalid zip file 'm3.zip', jdf file not found
$

New zip files :

$ ls *.zip
12345678.zip  87654321.zip  m3.zip
$ unzip -l -qq 12345678.zip
      0  05-11-10  09:02   dir1/
      4  05-11-10  09:02   dir1/12345678.jdf
    467  05-11-10  09:01   dir1/mark_1a.txt
     24  05-11-10  09:02   dir1/mark_1b.txt
$ unzip -l -qq 87654321.zip
      0  05-11-10  09:21   dir2/
     37  05-11-10  09:21   dir2/87654321.jdf
     29  05-11-10  09:21   dir2/mark_2a.txt
    770  05-11-10  09:21   dir2/mark_2b.txt
$ unzip -l -qq m3.zip
      0  05-11-10  09:28   dir3/
    125  05-11-10  09:28   dir3/mark_3a.txt
$

Jean-Pierre.

1 Like

Thank you Jean-Pierre,

that's the solution. I should have described the problem better in the first place.

Mark