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.



Seis De Mayo


Seis De Mayo


$4.29


It wasn’t long after the recording of this, Trey Anastasio’s third–and most warmly idiosyncratic–solo album, that the Phish frontman/guitarist announced that his improvisation-fueled, cult-fave band would record no more. Longtime Phish-heads (or admirers of Anastasio’s previous free-form jazz excursions with Les Claypool and Stewart Copeland and more conventional solo work) may find this ambitio…

Sapphire Video Card 100314-3l Hd 6870 1gb Ddr5 256bit Pci Express D-Dvi/Hdmi/Display Port Retail


Sapphire Video Card 100314-3l Hd 6870 1gb Ddr5 256bit Pci Express D-Dvi/Hdmi/Display Port Retail


$222.69


PCI Express based PC is required with one X16 lane graphics slot available on the motherboard.4X75 Watt 6-pin PCI Express power connector is required for CrossFireX system.2X75 Watt 6-pin PCI Express power connector is required.1024MBMinimum of system memory.Installation software requires CD-ROM drive.DVD playback requires DVD drive.Blu-ray/HD DVD playback requires Blu-ray/HD DVD drive.Blu-ray/HD …

ASUS Eee Pad Transformer TF101-A1 10.1-Inch Tablet (Dock Sold Separately)


ASUS Eee Pad Transformer TF101-A1 10.1-Inch Tablet (Dock Sold Separately)


$388.99


Some things are meant to go together Transforming the Conventional into the Unconventional With a slim, lightweight design and a 10.1-inch WSVGA IPS capacitive display made from durable and scratch-resistant glass that is viewable at angles up to 178°, the versatile Eee Pad Transformer is the perfect tablet for anyone who wants to enjoy multimedia on the move, but still wishes to have easy ac…

Advanced System CD for Laptop -File Restore, Diagnostics & More for DELL, Compaq, HP, Acer, Lenovo, IBM, Toshiba


Advanced System CD for Laptop -File Restore, Diagnostics & More for DELL, Compaq, HP, Acer, Lenovo, IBM, Toshiba


$15.95


Looking for a disc that actually works, a disc that will boot your laptop and give you access to advanced tools and features when Windows is not booting correctly?The Advanced System Recovery disc has a suite of tools designed to work with any laptop including those from Dell, Compaq, HP, Lenovo, IBM, Acer, Nokia, Toshiba, and more. Simply insert the disc into your computer and restart it to acces…

Gateway ID49C07u - Laptop, Intel Core i3 Processor, 14 Display, 4GB Memory, 500GB Hard Drive, Windows 7 Home Premium, Silver


Gateway ID49C07u – Laptop, Intel Core i3 Processor, 14 Display, 4GB Memory, 500GB Hard Drive, Windows 7 Home Premium, Silver


$799.99


Digital media card reader
Supports Secure Digital, MultiMediaCard, Memory Stick, Memory Stick PRO and xD-Picture Card formats. 4 high-speed USB 2.0 ports
For fast digital video, audio and data transfer. Built-in high-speed wireless LAN (802.11b/g/n)
Wirelessly connect to the Internet. Built-in 10/100/1000 Gigabit Ethernet LAN
With RJ-45 connector for quick and easy wired Web connection. Weighs onl…

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

4 comments

» Comments RSS Feed
  1. Hi buddy.

    By chance I’ve had some similar needs lately. I wanted to create an archive of the deepest directory inside a structure with different depths (my music folder actually)
    But first, you shouldn’t parse ls output.
    find $PWD -type d
    would be much better. Also, there’s no need to cd in a command or a script.
    However, here’s what I did
    This nice piece of bit finds the deepest subdirs in a given stack, returning the full path for each one:

    find $PWD -type d -exec bash -c ‘shopt -s nullglob; d=(“$1″/*/); ((!${#d[@]}))’ _ {} \; -print > /tmp/dirlist

    and then run a nice smooth and safe

    for i in $(sed ’1,$!d’ /tmp/dirlist); do tar cvfj “${i##*/}”.tar.bz2 “$i” ; done

    :)

    I just rolled a blog too, join me if you feel like :)

    blog.loomsen.org

  2. man, i googled for a script, ran across your code.

    complete time saver man, props to you!

    thanks so much.

  3. [...] Quickly archive multiple directories into separate archive files | 64 Bit Jungle – [...]

  4. ?????????)

Leave a comment