This is not specifically Pinguy-related, so apologies if this is posted in the wrong place.
I know pretty much nothing about Bash scripting, and I'm trying to get a script working. I need to extract all .zip files in a given directory into new directories based on the filename of the .zip. That part isn't a problem. Then I need to enter each directory and rename each file in it to the original file with the directory name prefixed.
Example:
Directory /foo contains three zip files (abc.zip, def.zip, and ghi.zip). Extract abc.zip to /foo/abc, extract def.zip to /foo/def, etc. That part is done. Now exists /foo/abc, /foo/def, and /foo/ghi, each has 1.jpg and 2.jpg. I need to rename each file in these directories to abc1.jpg, abc2.jpg, def1.jpg, def2.jpg, ghi1.jpg, ghi2.jpg based on which folder each jpg is in. The jpgs will have spaces and occasionally special characters, if that matters.
This is what I have right now:
Code:
#!/bin/bash
for f in *.zip; do
unzip -d "${f%*.zip}" "$f"; done
for directory in *; do
cd "$directory"
for filename in *; do
mv "$filename" "$directory - $filename";
done
done
This extracts the zip files properly, but only renames the files in the first directory, and does those incorrectly (it prefixes the names of all the zip files, not the directory name).
Like I said, I really don't know Bash scripting (or any language) and so I'm looking for advice.
Thanks all!
--chriscrutch