Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: compress multiple files/folders to separate zip files

  1. #1
    Join Date
    May 2008
    Beans
    75

    Angry compress multiple files/folders to separate zip files

    Hi

    I have a folder that contains multiple folder and multiple files,
    to be exact 1862 folder and file.

    I would want to, with one command, compress this so that ever folder becomes a separate zip file and ever file thats not in a folder becomes a separate zip file.
    Is this possible in ubuntu?

    I have searched on google and i can only find solutions for windows and mac and I have searched this forum and found nothing.

    So if there is same one out there that have a tip please help.

    MVH Sean

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: compress multiple files/folders to separate zip files

    test this, maybe on some smaller set first
    Code:
    for f in *; do zip -9r "$f.zip" "$f"; done

  3. #3
    Join Date
    Oct 2006
    Location
    New York
    Beans
    1,118
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: compress multiple files/folders to separate zip files

    Here's another possibility:

    Code:
    find . -mindepth 1 -maxdepth 1 -exec tar -czf {}.tar.gz {} \;
    I'm not sure if by zip you mean in general a compressed file format, or specifically zip file format. Some variation could probably be used for zip, I'm just not familiar with the zip syntax.
    xubuntu minimal, extensive experience, lshw: http://goo.gl/qCCtn
    blog: http://goo.gl/yLg78
    Linux viruses: http://goo.gl/6OCKA

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: compress multiple files/folders to separate zip files

    Hi Arunomi.

    You could use the command 'find' to get both the files on the directory and the list of directories in it.

    This will get the files:
    Code:
    find -maxdepth 1 - type f
    and this the subdirectories:
    Code:
    find -maxdepth 1 -type d -not -name .
    where:
    '-maxdepth 1' means to not descent looking on the subdirectories.
    '-type f' means files.
    '-type d' means directories.
    ''-not -name .' means do not include the directory itself (represented by .).
    You can execute a command every time 'find' finds a directory by using the exec option:
    Code:
    find -maxdepth 1 -type d -not -name . -exec zip -r '{}'.zip '{}' \;
    where '{}' makes reference to the directory just found. For example when it finds the dir 'Music', it will run:
    Code:
    zip -r Music.zip Music
    thus recursively scanning 'Music' and compress it into Muzic.zip

    For the files you need a different approach. You don't want each file compress by it self, but all of them together in one zip.

    You can also use the exec option but this time the command will be executed at the end using all files found as parameters:
    Code:
    find -maxdepth 1 -type f -exec zip localfiles.zip '{}' +
    This time all files directly under the folder will be compress into 'localfiles.zip'.

    I hope that helps, and tell us how it goes.
    Regards.

    BTW, you may need to install zip:
    Code:
    sudo apt-get install zip

  5. #5
    Join Date
    May 2008
    Beans
    75

    Re: compress multiple files/folders to separate zip files

    Code:
    find -maxdepth 1 -type d -not -name . -exec zip -r '{}'.zip '{}' \;
    Workt like a sharm but it includes the folder to.

    Is there a way to not include the folder?

    And I want the files in the main dir to be compresst to separat zip files and not to one big
    Last edited by Arunomi; April 19th, 2012 at 09:22 PM.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: compress multiple files/folders to separate zip files

    use for loop i mentioned earlier

    Code:
    $ ls *
    1.txt  2.txt
    xxx:
    1.txt  2.txt
    
    $ for f in *; do zip -9r "$f.zip" "$f"; done
      adding: 1.txt (stored 0%)
      adding: 2.txt (stored 0%)
      adding: xxx/ (stored 0%)
      adding: xxx/2.txt (stored 0%)
      adding: xxx/1.txt (stored 0%)
    $ ls
    1.txt  1.txt.zip  2.txt  2.txt.zip  xxx  xxx.zip

  7. #7
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: compress multiple files/folders to separate zip files

    Quote Originally Posted by Arunomi View Post
    Is there a way to not include the folder?
    Let me see if I understand correctly. Let's say one of the subdirectories directly under the main folder is 'Music'.

    I believe you want to zip Music's files as a plain file list with no reference to ever belonging to 'Music'. Is that so?

    I see 2 options depending if Music has just files, or more subdirectories and files:

    (1) Just files, and no subdirectories: just use the -j option (remove paths):
    Code:
    find -maxdepth 1 -type d -not -name . -exec zip -jr '{}'.zip '{}' \;
    (2) There are subdirectories under it ('Music' in my example): Here you "may" use the last command, but if they are repeated filenames, zip won't work properly.

    I can't think of anything simpler that this to compress all the subtree but excluding 'Music':
    Code:
    find -maxdepth 1 -type d -not -name . -exec bash -c 'cd "$1";  zip -r '../{}'.zip .' _ '{}' \;

    Quote Originally Posted by Arunomi View Post
    And I want the files in the main dir to be compresst to separat zip files and not to one big
    Find files, but use the exec part as in the directories example (but without recursion):
    Code:
    find -maxdepth 1 -type f -exec  zip '{}'.zip '{}' \;
    Regards.
    Last edited by papibe; April 19th, 2012 at 10:34 PM.

  8. #8
    Join Date
    May 2008
    Beans
    75

    Re: compress multiple files/folders to separate zip files

    It works wonderfully.
    Tanks

    Now just one more thing how do you know how to make this commands?

  9. #9
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: compress multiple files/folders to separate zip files

    Quote Originally Posted by Arunomi View Post
    It works wonderfully.
    Tanks
    Great! Please mark the thread solved when you have the chance.

    Quote Originally Posted by Arunomi View Post
    Now just one more thing how do you know how to make this commands?
    I started like you , asking questions, and then with time I've learned a few tricks.

    This is a very good place to learn cool stuff! Have fun and come back often!

    Regards.

  10. #10
    Join Date
    Oct 2006
    Location
    New York
    Beans
    1,118
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: compress multiple files/folders to separate zip files

    A good place to start for something like that is the man page for the find command, just type "man find" at the terminal and you'll get a bunch of documentation for the find command.

    The loop is kind of a bash-ism, and reading the documentation for bash is a little harder...

    There are also a bunch of clever bash-ism's, like back-ticks, and loops, and apparently zsh is even more clever, but I've never learned it through and through.
    xubuntu minimal, extensive experience, lshw: http://goo.gl/qCCtn
    blog: http://goo.gl/yLg78
    Linux viruses: http://goo.gl/6OCKA

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •