?
Solved

bash script - unzip to preformated filename & counternumber

Posted on 2005-05-09
10
Medium Priority
?
805 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:mfuerlinger
  • 5
  • 3
  • 2
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13964735
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
 
LVL 48

Expert Comment

by:Tintin
ID: 13965333
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13965413
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 48

Expert Comment

by:Tintin
ID: 13965497
Also, the 'ls' and the 'expr' are not required :-)

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13965509
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
 
LVL 48

Accepted Solution

by:
Tintin earned 375 total points
ID: 13965556
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13965567
I do agree but fn is far more amazing to me .... What was I thinking?
0
 
LVL 1

Author Comment

by:mfuerlinger
ID: 13966903
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
 
LVL 1

Author Comment

by:mfuerlinger
ID: 13967235
in php
e.g. system("/usr/local/tmp/test.sh '$myvar'");
?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13968161
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question