bash script - unzip to preformated filename & counternumber

Hello,

how do I get preformated filenames with increasing counternumber out of my zipfiles on linux shell?
I have a zip file containig e.g. 5 files:
wewesd.txt
sajsk.txt
sad3asd.txt
hdsa.txt
sdad.txt

I want to use the systems standard unzip command in a batch routine to get preformated filenames with counternumber:

The result should look like this:
text01.txt
text02.txt
text03.txt
text04.txt
text05.txt

How can this be done the most easy way?
Thanks for your help.
LVL 1
mfuerlingerAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
But expr and ls are not needed.  You can use the shell builtin functionality, rather than forking external processes.

Perhaps some coffee/sleep will make it all clearer :-)
0
 
sunnycoderCommented:
unzip to a directory.

i=1
for filename in `ls *.txt`
do
        fn=`echo $filename | sed 's/.*\.txt//'`
        mv $filename text$i.txt
         i=`expr $i + 1`
done
0
 
TintinCommented:
sunnycoder.

No need for forking three additional processes for the job.

#!/bin/bash
i=1

for file in *.txt
do
  mv $file text$i.txt
   let i++
done

And if you really want the digits to be zero padded, change the mv line to:

mv  $file text`printf "%02d.txt" $i`
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
sunnycoderCommented:
you are right tintin ...
>        fn=`echo $filename | sed 's/.*\.txt//'`
was unrequired ... May be I can blame it to lack of coffee/sleep at 6:30 AM ;-)
0
 
TintinCommented:
Also, the 'ls' and the 'expr' are not required :-)

0
 
sunnycoderCommented:
I do tend to use expr often, but taking a second look, I am wondering why I needed to extract fn from filename? It does not help even remotely ...

atleast expr and ls contribute to a solution
0
 
sunnycoderCommented:
I do agree but fn is far more amazing to me .... What was I thinking?
0
 
mfuerlingerAuthor Commented:
Here is the code I'm using so far:
#!/bin/bash
i=1
pathname=temp
newfilename=mynewfilename
unzip -j test.zip -d $pathname/

for file in $pathname/*.*
do
  mv $file $pathname/$newfilename$i.txt
   let i++
done


works fine but one little thing I'm sure you can help me easily:

The above bash routine will be later on be invoked by a php script (via php system-command).
How can I post/pipe a variable (the temporarily path name -$pathname) to the bash script from php?

thanks
0
 
mfuerlingerAuthor Commented:
in php
e.g. system("/usr/local/tmp/test.sh '$myvar'");
?
0
 
sunnycoderCommented:
First argument to the script will be accessible as $1 in the script
e.g.
system("/usr/local/tmp/test.sh $myvar");
and $myvar is your path, then in the test.sh script, $pathname can be safely replaced with $1 or you can add something like
pathname=$1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.