r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

How To: Compress Each Folder/Directory to Separate Archives in Linux

r3dux | June 27, 2009

Lets say you’ve got a bunch of folders taking up a large swathe of space which you never really use but want to keep, just not taking up stacks of your NAS… How can you easily compress them all up to individual archives of each folder? Dead easy:

1
2
3
4
5
#!/bin/bash
for folder in */
do
  7z a -mx9 -mmt "${folder%/}.7z" "$folder"
done

Save that to a file, chmod +x it and run in the location you want to compress the folders. Every folder (and all contents within) will be compressed to its own foldername.7z archive.

With 7z, -mx9 is the flag for maximum compression, and -mmt says to use multiple CPUs to speed up compression, so omit that part if you’re on a single core machine.

Related posts:

  1. How To: Compress a Directory of Files into Individual Archives
  2. How To: Swap between Soundcards in Linux
  3. How To: Delete Stuff Stuck in your Linux Trashbin
Categories
How-To, Linux
Tags
7z, Compress, Compression, Directory, Folder, How-To
Comments rss
Comments rss
Trackback
Trackback
Print This Post Print This Post

« I Have No Blog And I Must Scream Best. News. Evah! »

4 Responses to “How To: Compress Each Folder/Directory to Separate Archives in Linux”

  1. Daniel says:
    January 19, 2011 at 10:34 am

    Great example and very simple. However I have one question. Im trying to create the folder archives in a different folder e.g.

    All folders in
    /backup/live/*

    are archived and sent to:

    /backup/archive

    im sure this would be quite simple but im not sure how to do this.

    Thanks :-D

    Reply
    • r3dux says:
      January 19, 2011 at 3:41 pm

      try:

      #!/bin/bash
      for folder in */
      do
        7z a -mx9 -mmt "/backup/archive/${folder%/}.7z" "$folder"
      done

      =D

      Reply
      • Daniel says:
        January 20, 2011 at 8:04 am

        Thanks that seems so much simpler than what i came up with.

        for folder in $(find /backup/live/ -maxdepth 1 -mindepth 1 -name '[!.]*' -exec basename  {} \;)
        do
        7z a -mmt /backup/archive/${folder}/${folder}-backup-$(date +%d-%m-%Y).7z /backup/live/${folder} >> /tmp/backup-7z
        done
        Reply
        • r3dux says:
          January 20, 2011 at 8:26 am

          Well, yours seems pretty comprehensive with the date and all that – good job, and thanks for sharing.

          Bash scripting is such fiddly stuff :)

          Reply

Leave a Reply

Click here to cancel reply.

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash FPS GLFW Glitch GLSL Hack How-To install Java Kinect Linux Live Mash-Up Microsoft Motion mount OpenGL Particle Problem PS3 Remix Retro script Slides Sound Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“Auntie Em. Hate you, hate Kansas, taking the dog. Dorothy.”

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox