Quickly archive multiple directories into separate archive files

Posted by on Aug 24, 2009 in Ubuntu21 commentsPrint This Post

I recently needed to quickly archive several hundred directories, and their contents into separate, compressed files named after the directories. The horribly slow way to do this would be to either right-click each directory and select “create archive” via the File Manager, or, via the command line, to manually run tar several hundred times – once for each directory. Neither option is pleasant, or viable. However, with a simple shell for loop, combined with ls and the relevant archiver command, the task becomes quick and painless:

for dir in `ls`;do cd $dir; tar -cvzf $dir.tar.gz *;mv $dir.tar.gz ..; cd ..;done

This will run an ls command, and temporarily save the output for use in the For loop. The code loops through each part of the ls output, moves into the current directory (assuming it is a directory), creates the archive and moves the archive file to the parent directory, before moving itself to the parent directory to begin again with the next part of the ls output.

So, for example, if you have the following directory structure:

/home
--username
----stuff_to_archive
------dir-to-archive1
------dir-to-archive2
------dir-to-archive3
------dir-to-archive4
------dir-to-archive5

and you wish to archive dir-to-archive1 through dir-to-archive5, and all the contents therein, you would run:

cd /home/username/stuff_to_archive
for dir in `ls`;do cd $dir; tar -cvzf $dir.tar.gz *;mv $dir.tar.gz ..; cd ..;done

The code will loop through each directory and archive its contents into a file named after the directory (e.g. dir-to-archive1.tar.gz), leaving you with:

/home
--username
----stuff_to_archive
------dir-to-archive1
------dir-to-archive1.tar.gz
------dir-to-archive2
------dir-to-archive2.tar.gz
------dir-to-archive3
------dir-to-archive3.tar.gz
------dir-to-archive4
------dir-to-archive4.tar.gz
------dir-to-archive5
------dir-to-archive5.tar.gz

If you want to use ZIP instead, then run:

for dir in `ls`;do cd $dir; zip $dir *;mv $dir.zip ..; cd ..;done

or replace the archiver command with whatever you want to use to compress the directory contents.


Something not quite right? Inaccuracies or invalid code? Didn’t work for you? Don’t like me using Ss instead of Zs? Add a comment below! All comments are welcome. Except spam, because spam is a bit crap.





















BearTab® Unlocked 7 Inch Android Tablet Phone (16GB) - Quad Core 1.5ghz - Best IPS LCD Screen - Bluetooth & GPS Support - 5MP Camera with Auto Focus - DDR RAM 1GB Memory - Phone Phablet - Front and Back Camera with Flash - Wifi + 3G - Cell Phones & Smartphones, Mobile w/ Dual Sim Card Slot - Android 4.4.2 KitKat - Tmobile, AT&T and GSM Sim Cards - 1 YEAR WARRANTY


BearTab® Unlocked 7 Inch Android Tablet Phone (16GB) – Quad Core 1.5ghz – Best IPS LCD Screen – Bluetooth & GPS Support – 5MP Camera with Auto Focus – DDR RAM 1GB Memory – Phone Phablet – Front and Back Camera with Flash – Wifi + 3G – Cell Phones & Smartphones, Mobile w/ Dual Sim Card Slot – Android 4.4.2 KitKat – Tmobile, AT&T and GSM Sim Cards – 1 YEAR WARRANTY


$114.99


The BearTab 16 GB Tab tablet works great and features Android 4.4.2. Its perfect for those on the road trips or simply to keep your kids entertained. Comes with Micro USB Cable, AC Adapter (USA), & Manual. Features – Google Play Pre-Installed – Huge 16 GB Storage & blazing fast 1GB DDR memory speed – Bluetooth & GPS Support – Unlocked Phone with 3G support 850/1900 – Includes a Micro…

Dragon Touch A1X 10.1 Quad Core Google Android 4.4 KitKat Tablet PC, 1GB RAM, 16GB Nand Flash, Bluetooth, Dual Camera, HDMI, Google Play Pre-installed, 3D Game Supported, 2014 Newest Model [ by TabletExpress ]


Dragon Touch A1X 10.1 Quad Core Google Android 4.4 KitKat Tablet PC, 1GB RAM, 16GB Nand Flash, Bluetooth, Dual Camera, HDMI, Google Play Pre-installed, 3D Game Supported, 2014 Newest Model [ by TabletExpress ]


$109.99


Need a keyboard case ? Please search “B00EZAX2LA” on Amazon Brilliant PerformanceEquipped with a Quad-Core CPU and Octa Core GPU, this tablet ensures everything running smoothly. Enjoy yourself with stream videos, smooth-flowing gaming, web surfing and more!Newest Android 4.4 KitKatThe newly released system enables faster multitasking, optimizes user interface with more convenience and simplity a…