Script to rename zip-files with name of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to rename zip-files with name of file
# 1  
Old 05-10-2010
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
# 2  
Old 05-10-2010
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

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

# 3  
Old 05-10-2010
You can do something like that :
Code:
$ 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
Code:
$ 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:
Code:
$ ./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 :
Code:
$ 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.
# 4  
Old 05-10-2010
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
# 5  
Old 05-10-2010
New version of the mark.sh script :
Code:
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:
Code:
$ 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 :
Code:
$ ./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 :
Code:
$ 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.
# 6  
Old 05-11-2010
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:

Code:
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:

Code:
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

Last edited by mark_a17; 05-11-2010 at 02:50 AM..
# 7  
Old 05-11-2010
The folllowing new version of the script handles file names with directory specification. The output of your unzip command isn't a problem.
Code:
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 :
Code:
   jdf=$( unzip -l -qq "${zip}" |
          awk -F'[ /]' '{ if (sub(/\.jdf$/, "", $NF)) print $NF}' )

Zip files :
Code:
$ 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 :
Code:
$ ./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 :
Code:
$ 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.
This User Gave Thanks to aigles For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (1 Reply)
Discussion started by: b.saipriyanka
1 Replies

2. UNIX for Beginners Questions & Answers

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (2 Replies)
Discussion started by: b.saipriyanka
2 Replies

3. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

4. Shell Programming and Scripting

Compressing old files as zip file through script

I have below files in foler one/archive> one. txt 6/21/2013 two txt 7/23/2013 three.txt 6/20/2013 I wanted to move all the old files (>30 days) compressing single .zip file into one/archive/ as below two txt 7/23/2013 oldfiles.zip 6/21/2013 Please provide... (6 Replies)
Discussion started by: Ganesh L
6 Replies

5. Shell Programming and Scripting

Zip Multiple files to One .zip file in AIX system

Hi I have a requirement in unix shell where I need to zip multiple files on server to one single .zip file. I dont see zip command in AIX and gzip command not doing completely what I want. One I do .zip file, I should be able to unzip in my local Computer. Here is example what I want... (9 Replies)
Discussion started by: RAMA PULI
9 Replies

6. Shell Programming and Scripting

Rename files in ZIP file

Hi guys, I want to change the name of zip file and the files in zip. eg. there are two zip files 100_ABC_20101020.zip 101_ABC_20101020.zip 100_ABC_20101020.zip | --100_A_20101020.txt --100_B_20101020.txt --100_C_20101020.txt 101_ABC_20101021.zip | ... (0 Replies)
Discussion started by: ambious
0 Replies

7. Shell Programming and Scripting

Rename files that are inside zip file

Hello to all, I have a zip file with any name like FileName.zip, within the zip file there are more than 30 files with different extensions in the following format. FileName_BMN_ROSJ.txt FileName_THEUS.jpg . . . FileName_KWPWP.shx I would like to unzip the file and rename each file... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

8. AIX

ZIP multiple files and also specify size of zip file

I have to zip many pdf files and the size of zip file must not exceed 200 MB. When size is more than 200 MB then multiple zip files needs to be created. How we can achieve this in UNIX? I have tried ZIP utility but it takes a lot of time when we add individual pdfs by looping through a... (1 Reply)
Discussion started by: tom007
1 Replies

9. Shell Programming and Scripting

Hello - new here - bash script - need to rename and zip files.

I'm working on a project that basically unzips three zip files. When these unzip they create about 70+ directories with subdirectories of year/month with about 3 to 9 pdf files in each directory. Basically, I'm needing to figure out a way to zip these pdf files up. for instance the script... (1 Reply)
Discussion started by: Aixia
1 Replies

10. UNIX for Dummies Questions & Answers

unzip .zip file and list the files included in the .zip archive

Hello, I am trying to return the name of the resulting file from a .zip archive file using unix unzip command. unzip c07212007.cef7081.zip Archive: c07212007.cef7081.zip SecureZIP for z/OS by PKWARE inflating: CEP/CEM7080/PPVBILL/PASS/G0063V00 I used the following command to unzip in... (5 Replies)
Discussion started by: oracledev
5 Replies
Login or Register to Ask a Question