LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2013, 01:08 AM   #1
dhirendrs
Member
 
Registered: Nov 2009
Posts: 45

Rep: Reputation: 15
Required script zip file rename with the contain of file


Dear all,

we need below script.

we have run for loop and unzip and rename same but still require detail not coming.

e.g i have below two file

1139.20000625162706.zip
bingo.1139.20000625162706


need to rename file name with 1139 with 1200.

Most important is

inside 1139.20000625162706.zip file all file have below structure

inflating: 1139.20000625162706.disc_pro.iu
inflating: 1139.20000625162706.disc_gq.iu
inflating: 1139.20000625162706.disc_iq.iu
inflating: 1139.20000625162706.disc_iqd.iu
inflating: 1139.20000625162706.disc_rul.iu
inflating: 1139.20000625162706.disc_lin.iu
inflating: 1139.20000625162706.disc_rulld.iu

that also need to rename 1139 to 1200 and that file need to rename with

1200.20000625162706.zip


--------------

Script i wrote as below.

for file in *.zip
do
unzip -l $file | grep -q 1139
cp $file /home/tplinux/tmp/idoc/in
cd /home/tplinux/tmp/idoc/in/
unzip $file
rename "1139." "1200." *.*
ls -ltr | awk '{print $9}' >> 3.txt
ff=`tail -1 3.txt`
echo $ff
rm 3.txt *.zip
ss=`expr substr $ff 1 19`
zip $ff *.*
ls -ltr | grep -v "zip" | awk '{print "rm -rf " $9}' | sh
touch bingo.$ss
done

Last edited by dhirendrs; 06-28-2013 at 01:27 AM.
 
Old 06-28-2013, 12:03 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.


I'm not completely sure I understand what you need. You want to unzip the contents of the archive, replace a string in each name, then re-zip it?

What is the exact problem you're having with the script you posted? I can't understand half of what it's trying to do. What's all that stuff with awk, 3.txt, expr (a useless command since modern shells have built-ins that can do everything it does), and all that? parsing ls is not recommended in any case, so I doubt it's doing any good.


Couldn't you just do something like this?

Code:
#!/bin/bash

zippath=/location/of/zipfiles
outpath=/path/to/outputdir
tmpdir=/whatever/you/want

[[ -d $tempdir ]] && cd "$tempdir" || { echo 'No tempdir available' ; exit 1 ;}

for zipfile in "$zippath/"*.zip; do

    unzip "$zipfile"

    for fname in *; do
        mv "$fname" "${fname/1139/1200}"
    done

    outzip=$outpath/${zipfile##*/}
    outzip=${outzip/1139/1200}

    zip "$outzip" *

    rm -f *

done

exit 0
Please explain if you need something else.

Last edited by David the H.; 06-28-2013 at 12:05 PM. Reason: minor rewording
 
  


Reply

Tags
shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] HowTo: Find a file, rename and copy new file in one script! pietmomberg Linux - Newbie 17 06-02-2013 06:08 AM
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 02:49 AM
How to find out a file in a WAR which in a zip without unzip the zip-file? thomas2004ch Linux - Software 3 09-30-2011 03:06 PM
Script to rename a file saghir27 Linux - Newbie 1 04-29-2008 08:20 AM
File rename script jjwl89 Linux - General 1 03-14-2005 06:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration