<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>r3dux.org &#187; Compress</title>
	<atom:link href="http://r3dux.org/tag/compress/feed/" rel="self" type="application/rss+xml" />
	<link>http://r3dux.org</link>
	<description>A number-pimping side project from the valleys in *NEW* upside-down flavour.</description>
	<lastBuildDate>Fri, 10 Sep 2010 08:00:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How To: Compress Each Folder/Directory to Separate Archives in Linux</title>
		<link>http://r3dux.org/2009/06/how-to-compress-each-folderdirectory-to-separate-archives-in-linux/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=how-to-compress-each-folderdirectory-to-separate-archives-in-linux</link>
		<comments>http://r3dux.org/2009/06/how-to-compress-each-folderdirectory-to-separate-archives-in-linux/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 01:05:58 +0000</pubDate>
		<dc:creator>r3dux</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[7z]]></category>
		<category><![CDATA[Compress]]></category>
		<category><![CDATA[Compression]]></category>
		<category><![CDATA[Directory]]></category>
		<category><![CDATA[Folder]]></category>

		<guid isPermaLink="false">http://r3dux.org/?p=719</guid>
		<description><![CDATA[Lets say you&#8217;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&#8230; How can you easily compress them all up to individual archives of each folder? Dead easy: 1 2 3 4 5 #!/bin/bash for [...]


Related posts:<ol><li><a href='http://r3dux.org/2009/04/how-to-compress-a-directory-of-files-into-individual-archives/' rel='bookmark' title='Permanent Link: How To: Compress a Directory of Files into Individual Archives'>How To: Compress a Directory of Files into Individual Archives</a></li>
<li><a href='http://r3dux.org/2010/09/how-to-extract-ace-archives-in-linux/' rel='bookmark' title='Permanent Link: How To: Extract .ace Archives in Linux'>How To: Extract .ace Archives in Linux</a></li>
<li><a href='http://r3dux.org/2010/01/how-to-install-thunderbird-3-in-linux/' rel='bookmark' title='Permanent Link: How To: Install Thunderbird 3 in Linux'>How To: Install Thunderbird 3 in Linux</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Lets say you&#8217;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&#8230;  How can you easily compress them all up to individual archives of each folder? Dead easy:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">for</span> folder <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*/</span>
<span style="color: #000000; font-weight: bold;">do</span>
  7z a <span style="color: #660033;">-mx9</span> <span style="color: #660033;">-mmt</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${folder%/}</span>.7z&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$folder</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Save that to a file, <b>chmod +x</b> 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.</p>
<p>With 7z, <b>-mx9</b> is the flag for maximum compression, and <b>-mmt</b> says to use multiple CPUs to speed up compression, so omit that part if you&#8217;re on a single core machine.</p>


<p>Related posts:<ol><li><a href='http://r3dux.org/2009/04/how-to-compress-a-directory-of-files-into-individual-archives/' rel='bookmark' title='Permanent Link: How To: Compress a Directory of Files into Individual Archives'>How To: Compress a Directory of Files into Individual Archives</a></li>
<li><a href='http://r3dux.org/2010/09/how-to-extract-ace-archives-in-linux/' rel='bookmark' title='Permanent Link: How To: Extract .ace Archives in Linux'>How To: Extract .ace Archives in Linux</a></li>
<li><a href='http://r3dux.org/2010/01/how-to-install-thunderbird-3-in-linux/' rel='bookmark' title='Permanent Link: How To: Install Thunderbird 3 in Linux'>How To: Install Thunderbird 3 in Linux</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://r3dux.org/2009/06/how-to-compress-each-folderdirectory-to-separate-archives-in-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To: Compress a Directory of Files into Individual Archives</title>
		<link>http://r3dux.org/2009/04/how-to-compress-a-directory-of-files-into-individual-archives/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=how-to-compress-a-directory-of-files-into-individual-archives</link>
		<comments>http://r3dux.org/2009/04/how-to-compress-a-directory-of-files-into-individual-archives/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 07:33:48 +0000</pubDate>
		<dc:creator>r3dux</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[7z]]></category>
		<category><![CDATA[Archives]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Compress]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[Individual]]></category>
		<category><![CDATA[zipeach]]></category>

		<guid isPermaLink="false">http://r3dux.org/?p=439</guid>
		<description><![CDATA[I&#8217;ve got a stack of files all thrown together in the same directory, and I wanted them compressed &#8211; simple enough, eh? Only thing is I wanted each file compressed to its own archive, so I can see at a glance what&#8217;s there, and if for some reason an archive gets corrupted, it&#8217;s just one [...]


Related posts:<ol><li><a href='http://r3dux.org/2009/06/how-to-compress-each-folderdirectory-to-separate-archives-in-linux/' rel='bookmark' title='Permanent Link: How To: Compress Each Folder/Directory to Separate Archives in Linux'>How To: Compress Each Folder/Directory to Separate Archives in Linux</a></li>
<li><a href='http://r3dux.org/2009/11/how-to-convert-a-directory-of-mp3s-to-wavs-in-linux/' rel='bookmark' title='Permanent Link: How To: Convert a Directory of MP3s to WAVs in Linux'>How To: Convert a Directory of MP3s to WAVs in Linux</a></li>
<li><a href='http://r3dux.org/2009/09/how-to-easily-convert-flac-audio-to-mp3s-in-linux/' rel='bookmark' title='Permanent Link: How To: Easily Convert FLAC audio to MP3s in Linux'>How To: Easily Convert FLAC audio to MP3s in Linux</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got a stack of files all thrown together in the same directory, and I wanted them compressed &#8211; simple enough, eh? Only thing is I wanted each file compressed to its own archive, so I can see at a glance what&#8217;s there, and if for some reason an archive gets corrupted, it&#8217;s just one file lost and I can replace it instead of having to dick around repairing corrupted &#8220;blob&#8221; archives that contain the entire bunch of files. And I want to be able to specify all files with a given file extension to compress.</p>
<p>Although I wouldn&#8217;t be surprised if you could do this in 4 lines of <a href="http://en.wikipedia.org/wiki/Perl">Perl</a>, I don&#8217;t know flippin&#8217; Perl (yet), so I wrote a bash script to do the job.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Purpose: Script to compress all files of given extension to individual archives using 7z</span>
<span style="color: #666666; font-style: italic;"># Usage  : zipeach.sh &lt;extension-without-prefix-dot&gt; i.e zipeach.sh n64</span>
<span style="color: #666666; font-style: italic;"># Author : r3dux</span>
<span style="color: #666666; font-style: italic;"># Date   : 16/04/2009</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">count</span>=<span style="color: #000000;">0</span>           <span style="color: #666666; font-style: italic;"># File counter</span>
<span style="color: #007800;">got7z</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> 7z<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># Use &quot;which&quot; to check if there's a copy of 7z on the system</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># If there's no copy of 7z - we're not going to be doing much compressing... Exit stage left.</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$got7z</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;No copy of 7z found on system! Try running: sudo apt-get install p7zip-full&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># If we have 7z, and have been given a file extension parameter...</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #ff0000;">&quot;$1&quot;</span>; <span style="color: #000000; font-weight: bold;">then</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># Stop the script from entering an infinite loop should user mistakenly enter 7z as filetype to compress...</span>
 	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> $<span style="color: #000000;">1</span> = <span style="color: #ff0000;">&quot;7z&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
 	    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Recursion neatly sidestepped - no 7z filetypes ya scurvy seadog! =P&quot;</span>
 	    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
 	<span style="color: #000000; font-weight: bold;">fi</span>	 	
&nbsp;
 	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Starting Zipeach...&quot;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;"># For each file with the given extension in the current directory...</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">in</span> .<span style="color: #000000; font-weight: bold;">/*</span>.$<span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">do</span>
&nbsp;
		<span style="color: #666666; font-style: italic;"># If a file exists with given extension...</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
&nbsp;
			<span style="color: #666666; font-style: italic;"># Compress the file with maximum compression (-mx9) and use multiple threads for multiCPU machines (-mmt)</span>
			<span style="color: #666666; font-style: italic;"># NOTE: Remove -mmt flag to run this on a single CPU box...</span>
			7z a <span style="color: #660033;">-mx9</span> <span style="color: #660033;">-mmt</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span>.7z <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;"># Increment our file counter</span>
			<span style="color: #7a0874; font-weight: bold;">let</span> count+=<span style="color: #000000;">1</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #666666; font-style: italic;"># If no file of required extension has been found then notify user and quit</span>
			<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;No files of extension .$1 found in current directory!&quot;</span>
			<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">fi</span> <span style="color: #666666; font-style: italic;"># End of if file exists condition				</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #666666; font-style: italic;"># End of for each file loop</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># Exit when all files compressed</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #666666; font-style: italic;"># Cheap blank line =P</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Zipeach completed. <span style="color: #007800;">$count</span> files of extension .$1 found and compressed.&quot;</span>
  	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #666666; font-style: italic;"># No extension parameter given? No worky...</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'Please run the script with an extension to compress i.e. &quot;zipeach.sh n64&quot; to compress each .n64 file into its own 7z archive.'</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><a href="http://en.wikipedia.org/wiki/Bash">Bash</a>, as it turns out, is a fiddly, finicky beast in that you really have to think about what the command-line will see under different circumstances and enclose variables in inverted commas or not in very precise ways (see this <a href="http://linuxcommand.org/wss0100.php">article</a> to understand what I mean). All that <b><i>if</i></b> / <b><i>fi</i></b> stuff too&#8230; very odd.</p>
<p>To use the script, copy and paste it into a text file (in my case I&#8217;ve called it <b>zipeach.sh</b>), save it, make the file executable using <b>chmod +x zipeach.sh</b> and move it to <i>/usr/bin</i> or something so it&#8217;s in your path using <b>sudo mv ./zipeach.sh /usr/bin/</b> &#8211; then run it inside any directory you want to zip files to individual archives by calling it with <b>zipeach nds</b> (for example) to compress all the .nds (Nintendo DS roms) in a folder into individual archives. Or use this <a href='http://r3dux.org/wp-content/uploads/2009/04/zipeach.sh'>link</a> <img src='http://r3dux.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Anyway, job done &#8211; suggestions? improvements? props? death-threats? Let me know below! </p>
<p>Oh, and cheers to <a href="http://jamesmcdonald.id.au/">James McDonald</a> for his <a href="http://jamesmcdonald.id.au/open-source-apps/wordpress/wp-syntax-plugin">WP-Syntax hack</a> to stop all the embedded code appearing on a single (incredibly long) line!</p>
<div class="diggbutton"><script type="text/javascript">digg_url = 'http://r3dux.org/2009/04/how-to-compress-a-directory-of-files-into-individual-archives/';digg_title = 'How To: Compress a Directory of Files into Individual Archives';</script><script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div>


<p>Related posts:<ol><li><a href='http://r3dux.org/2009/06/how-to-compress-each-folderdirectory-to-separate-archives-in-linux/' rel='bookmark' title='Permanent Link: How To: Compress Each Folder/Directory to Separate Archives in Linux'>How To: Compress Each Folder/Directory to Separate Archives in Linux</a></li>
<li><a href='http://r3dux.org/2009/11/how-to-convert-a-directory-of-mp3s-to-wavs-in-linux/' rel='bookmark' title='Permanent Link: How To: Convert a Directory of MP3s to WAVs in Linux'>How To: Convert a Directory of MP3s to WAVs in Linux</a></li>
<li><a href='http://r3dux.org/2009/09/how-to-easily-convert-flac-audio-to-mp3s-in-linux/' rel='bookmark' title='Permanent Link: How To: Easily Convert FLAC audio to MP3s in Linux'>How To: Easily Convert FLAC audio to MP3s in Linux</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://r3dux.org/2009/04/how-to-compress-a-directory-of-files-into-individual-archives/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
