New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
How to zip compress each directory and set filename same as directory name
Hi, can someone help me. I want to zip each directory recursively and set each filename (.zip) same as directory name.
/home/alice
/home/bob
/home/someuser
so the result will be alice.zip bob.zip .. someuser.zip
I dont want to do it manually, any idea how to do it? bash script maybe
*sorry my bad english.
Comments
My experience with this site is, you might have a better chance of getting answers elsewhere instead.
You are welcome - http://stackoverflow.com/questions/15936003/for-each-dir-create-a-tar-file
I'm not clear on what you mean by recursive as in your example you only wanted the top directories.
There's most likely better ways to do it but here's on way:
for i in /home/*; do cd "$i"; zip ../$(basename $i).zip -r .;done
This will cd into each directory, and zip all files. Since zip creates a folder structure based on the current directory you must cd into it.
Have fun!
Fun to know I'm not the first person who thought "Oh fun, an opportunity to write a for loop!"
I love bash.
Who does not?
I ruby
You're hired.
thanks for helping.
I additionally love Python, but bash is great anyway.
****> @cosmicgate said:
I am glad to see that members of LET responded with working solutions, kudos to you guys
Windows users.
Be careful with this, if the /home directory has a file in it then you'll end up making a zipfile with that name containing everything in the /home directory
You probably want to use find as @numin suggested to return only directories.