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

Need help with C, C++, perl, python, etc?

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

Postby thamarok » 2007-01-24 20:00

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

Postby Grifter » 2007-01-24 20:41

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...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby thamarok » 2007-01-25 14:46

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

Postby plugwash » 2007-01-25 20:48

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.
plugwash
 
Posts: 2508
Joined: 2006-09-17 01:10

Postby thamarok » 2007-01-25 21:52

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??
thamarok
 

Postby runningwithscissors » 2007-01-26 14:25

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)
User avatar
runningwithscissors
 
Posts: 3
Joined: 2007-01-26 14:18

Postby Grifter » 2007-01-26 16:54

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...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby thamarok » 2007-01-27 15:47

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)
thamarok
 

Postby Grifter » 2007-01-27 16:34

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...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby thamarok » 2007-01-27 16:39

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!
thamarok
 

Postby Grifter » 2007-01-27 16:40

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...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby thamarok » 2007-01-27 17:07

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!
thamarok
 

Postby Grifter » 2007-01-28 17:50

rename 's/\ /_/g' *
rename 's/\ /_/g' */*
rename 's/\ /_/g' */*/*
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby thamarok » 2007-01-28 18:24

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


Thanks!
thamarok
 


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable