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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through the dir and Rename zip files and their underlying text file.
# 1  
Old 09-10-2014
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 [datepart - 1 day].

For eg: in the case of ABC_20140910.zip, the zip file should be renamed to ABC_20140909.zip and the text file should be renamed to ABC_20140910.txt.

Below is what I have so far. I am sure that there is a more efficient way to accomplish this. Any input is appreciated. Thanks.


Code:
for file in `ls -v $FileNamePrefix*.zip`
do

DATE_PART=$(ls "$file" | grep $FileNamePrefix | grep -oE [[:digit:]]+ )  #strip date part from zip file name.

NEW_DATE_PART=$(date +'%Y%m%d' -d "$DATE_PART - 1 day")  #subtract 1 day from date part above.

unzip -o "$file" -d /tmp   #unzip to /tmp dir 

mv /tmp/"$FileNamePrefix"_"$DATE_PART".txt /tmp/"$FileNamePrefix"_"$NEW_DATE_PART".txt  #rename the txt file.

 zip -d "$FileNamePrefix"_"$DATE_PART".zip "$FileNamePrefix"_"$DATE_PART".txt  #remove the original txt file from the zip file.

zip -j "$FileNamePrefix"_"$DATE_PART".zip /tmp/"$FileNamePrefix"_"$NEW_DATE_PART".txt  #zip back the renamed txt file.
 
mv "$file" $FileNamePrefix"_"$NEW_DATE_PART".zip  #rename the zip file.

done


Last edited by Don Cragun; 09-10-2014 at 09:04 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 09-11-2014
So, do you mean that you want to edit the zip file and change the file information? If this was a simple unix compressed single file it would not matter, but there is data written within the zip file like a mini-directory holding all the file details. It's the same with tar where multiple files can be collected.

I fear that you may need to extract the file, rename it and re-zip as your script does, however we can tidy up your script a little so that we don't call unnecessary external programs (grep, ls, etc.)

To get your loop started, you can simply:-
Code:
for file in ${FileNamePrefix}*.zip
do

To slice a string with nice clear delimiters, you can use variable substitution. This looks like ksh or bash so:-
Code:
tempvar="${file#*_}"                   # Strip off everything up to the first underscore
DATE_PART="${tempvar%.zip}"           # Strip off the trailing .zip

Date calculations form many threads on this forum. If what you have works for you, then it is probably the neatest you can get already.

When you are zip -d 'removing' the .txt file from the .zip, what will be left in the .zip? If it's nothing, then just remove the .zip file and create a new one of the required name for the extracted .txt



I hope that this helps a little.
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

2. Shell Programming and Scripting

Finding a text file from a group of zip files without unzipping

HI , There are more than 100 zip files in a directory and i wanted to see if there is a max1157.txt file in any of the zip files without actually unzipping them. Could you please help. Thanks in Advance. Karthik. (6 Replies)
Discussion started by: karthikk0508
6 Replies

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

For Loop To Rename Multiple Files Finds One Non-existant File

Okay so here's something that's confusing me: I have a script that's designed to remove the words "new_" from the front of any file except two exceptions and it looks something like this... for i in new_* do if ] && ]; then j=`echo "$i"|cut -c5-` mv $i $j fi done ... (5 Replies)
Discussion started by: Korn0474
5 Replies

6. UNIX for Dummies Questions & Answers

get all files under specified dir and zip them, then transfer to another machine

Hi guys, now, I have the following requirement: 1. Get all files under specified directory 2. the files modify date should be specified date 3. zip these files 4. transfer these files to another machine, assume ip, user and password are given. could you pls provide good solution for... (1 Reply)
Discussion started by: crest.boy
1 Replies

7. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: mark_a17
7 Replies

8. 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

9. Shell Programming and Scripting

Loop through files in dir, omit file with latest date

I want to loop through files in a directory but omit the file with the latest date in my list of files. How would I accomplish this? Thanks (2 Replies)
Discussion started by: stringzz
2 Replies

10. Shell Programming and Scripting

trying to rename the files in dir

I have bunch of files in win xp machine with 123456 E15 filename 112333.E20 filename 123412.E11 filename you get the pic, I mount that xp machine's share into linux and try to do a mass rename to something simpler E15 filename E20 filename and so on.. I wrote below thinking that it... (8 Replies)
Discussion started by: hankooknara
8 Replies
Login or Register to Ask a Question