Quickly archive multiple directories into separate archive files

Posted by on Aug 24, 2009 in Ubuntu4 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.





















ProntoTec 7 Android 4.2 Tablet PC, Cortex A8 1.2 Ghz Dual Core Processor,512MB / 4GB,Dual Camera,HDMI,G-Sensor (Black)


ProntoTec 7 Android 4.2 Tablet PC, Cortex A8 1.2 Ghz Dual Core Processor,512MB / 4GB,Dual Camera,HDMI,G-Sensor (Black)


$50.98


Product Name

- ProntoTec Q8 7 inch Dual Core Tablet PC

CPU

- Cortex A8 Dual Core 1.2 GHz

Operating System

- Android 4.2.2 OS

Touch Screen

- 7-Inch Capacitive Display
- 800 x 480 Pixels

Internal Storage

- 4GB GB

RAM

- 512MB SDRM DDR3

Sensors

- G-Sensor

Wireless and Cellular

- Wi-Fi (802.11 BGN)
- Support for 3G Dongles and Ethernet with Adapter

Dual Cameras

- Front 0.3M/Rear 2M …


Dragon Touch® 7'' Black Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024x600, 4GB, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13)[By TabletExpress]


Dragon Touch® 7” Black Dual Core Y88 Google Android 4.1 Tablet PC, Dual Camera, HD 1024×600, 4GB, Google Play Pre-load, HDMI, 3D Game Supported (enhanced version of A13)[By TabletExpress]


$57.99


Trademark Dragon Touch products are marketed exclusively by TabletExpress. Specification:
Operating System: Google Android 4.1 (Jelly Bean)
Display: 7 Inch Multi-touch screen (1024×600)
CPU: iMAP X15 Dual Core, up to 1.2 GHz processor;
GPU: Quad core ARM Mali 400 MP GPU
Internal Memory: 4 GB Memory Expansion: MicroSD card (up to 32
GB in size)
RAM: 512MB DDR3
Camera: Front facing plus rear camer…