New on LowEndTalk? Please Register and read our Community Rules.
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.
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.
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.
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?
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
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.