Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Needing help to make a fully functional Bash script [SOLVED]

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
thamarok

Needing help to make a fully functional Bash script [SOLVED]

#1 Post by thamarok »

Hello!

I can't seem to make a working Bash script to do the following:

1. I have a folder where multiple ZIP files are stored, all these ZIP files contain many subdirectories, so using this command will just make a big mess:
for i in `find . -name "*.zip"`; do unzip -d `dirname $i` $i;done;
What I want would be to unzip each folder into a directory created with mkdir with the ZIP filename as the directory name (without the ending .zip extension!)

2. Convert all PNG images into JPG in a directory with lots lots of subdirectories containing PNG images and finally removing all PNG images.

3. List all directories in a folder and adding "_old" to the end of the directory name. (I need this to separate older and newer projects)

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

I have tried many times, but with no success. I know that someone might think that I'm a Pr0-Linux user, but I'm just a normal user with nearly no knowledge about basic commands used in Linux.

I appreciate it if someone could help me to make a Bash script to make my life easier; I am sure others will also benefit from it. Thanks!
Last edited by thamarok on 2007-01-28 18:24, edited 1 time in total.

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#2 Post by Grifter »

rename 'y/A-Z/a-z/' *; for i in $(find -iname '*.zip'); do mkdir `basename $i .zip`; unzip -d `baseename $i .zip` $i; done

EDIT:
forgot about the png and old stuff, but I'm going to eat now, I'm sure you can add those things yourself, and if not you're just a forum away from help (:
Eagles may soar, but weasels don't get sucked into jet engines...

thamarok

#3 Post by thamarok »

Thanks Grifter!
Your script works well, but after editing a small typo:
rename 'y/A-Z/a-z/' *; for i in $(find -iname '*.zip'); do mkdir `basename $i .zip`; unzip -d `basename $i .zip` $i; done
You had "basename" written as "baseename" :P

Now if someone could help me to have a script for the 2nd and 3rd operations ..

plugwash
Posts: 2507
Joined: 2006-09-17 01:10
Contact:

#4 Post by plugwash »

for the latter things look into the looping capabilities of bash script

for example if we look at http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html we find the example

#!/bin/bash
for i in $( ls ); do
echo item: $i
done

should be pretty easy to tweak that to only list directries and rename each one.

thamarok

#5 Post by thamarok »

plugwash wrote:for the latter things look into the looping capabilities of bash script

for example if we look at http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html we find the example

#!/bin/bash
for i in $( ls ); do
echo item: $i
done

should be pretty easy to tweak that to only list directries and rename each one.
How can I tweak ls to list only directories??

User avatar
runningwithscissors
Posts: 3
Joined: 2007-01-26 14:18

#6 Post by runningwithscissors »

thamarok wrote:How can I tweak ls to list only directories??

Code: Select all

$ for i in `ls`; do if [ -d ${i} ]; then echo ${i}; fi; done;
Happiness, is not being afraid. -- Roy Keane (supposedly)

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#7 Post by Grifter »

even simpler

for i in $( ls -d */); do echo $i; done

or

for i in $( find -type d); do echo $i; done
Eagles may soar, but weasels don't get sucked into jet engines...

thamarok

#8 Post by thamarok »

Thanks Grifter!
Once again you proved that you are a Bash hacker (:

Now just to make a small script to convert all PNGs to JPG and remove all PNGs (remember that the PNG files are located in many directories under subdirectories)

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#9 Post by Grifter »

for i in $( find -iname '*jpg'); do mogrify -format png $i; done

now if you wanted to remove it, after the mogrify one you could simply do ; rm $i; aswell, however I tend to be careful about automated removals, unless I know for sure that everything has been accomplished flawlessly I wouldn't remove anything
Eagles may soar, but weasels don't get sucked into jet engines...

thamarok

#10 Post by thamarok »

Thanks Grifter!
One can really learn from you :)

I actually have a question for you: Is it possible to emulate Windows XP as a Guest OS, but make it possible for Windows XP to see my REAL hardware (Graphic Card, Sound Card and such)? Because I just finished installing Windows XP in QEMU and when I wanted to install the drivers for my Graphic Card, it complained about not finding any cards on my system and I was really pissed-off... I need something like QEMU but which doesn't emulate the whole PC, just the Guest OS.

Thanks in advance!

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#11 Post by Grifter »

I don't know, xp is the reason I switched to linux, I have never run it and don't intend to
Eagles may soar, but weasels don't get sucked into jet engines...

thamarok

#12 Post by thamarok »

Grifter wrote:I don't know, xp is the reason I switched to linux, I have never run it and don't intend to
Ok, btw, I ran your script and it worked fine, for the most part. It doesn't want to mogrify files inside directories which have a space in their names.. I tried to fix it but with no success.. Thanks in advance!

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#13 Post by Grifter »

rename 's/\ /_/g' *
rename 's/\ /_/g' */*
rename 's/\ /_/g' */*/*
Eagles may soar, but weasels don't get sucked into jet engines...

thamarok

#14 Post by thamarok »

Grifter wrote:rename 's/\ /_/g' *
rename 's/\ /_/g' */*
rename 's/\ /_/g' */*/*
Thanks!

Post Reply