Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to zip compress each directory and set filename same as directory name
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.

How to zip compress each directory and set filename same as directory name

Ryan22Ryan22 Member
edited January 2015 in Help

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.

  • nunimnunim Member
    edited January 2015

    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:

    In this example your top directory is /home/stuff
    
    cd /home/stuff |find . -maxdepth 1 -mindepth 1 -type d  | sed 's:^\./::' | while read i; do  zip -qr9 $i.zip $i; done
    Thanked by 1Amitz
  • 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!

    Thanked by 2jar Amitz
  • jarjar Patron Provider, Top Host, Veteran

    @pcfreak30 said:

    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.

    Thanked by 1Amitz
  • @Jar said:
    I love bash.

    Who does not? <3

    Thanked by 1jar
  • @Amitz said:
    Who does not? <3

    I <3 ruby

  • jarjar Patron Provider, Top Host, Veteran

    @msg7086 said:
    I <3 ruby

    You're hired.

  • thanks for helping.

  • @msg7086 said:
    I <3 ruby

    I additionally love Python, but bash is great anyway. <3 <3

  • risharderisharde Patron Provider, Veteran
    edited January 2015

    ****> @cosmicgate said:

    My experience with this site is, you might have a better chance of getting answers elsewhere instead.

    I am glad to see that members of LET responded with working solutions, kudos to you guys

  • Amitz said: Who does not? <3

    Windows users.

  • pcfreak30 said: for i in /home/*; do cd "$i"; zip ../$(basename $i).zip -r .;done

    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.

Sign In or Register to comment.