Quickly archive multiple directories into separate archive files
Posted by Hodge on Aug 24, 2009 in Ubuntu • 4 comments •
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.
|
|
… |
|
|
… |
|
|
… |
|
|
Alldaymall(TM) 7-inch Capacitive Touch Screen Android 4.0 Tablet PC with Allwinner A13 1.0GHz 512MB/4GB WiFi Front-camera (Black) $52.99 Alldaymall owns this trademark exclusively Basic Configuration Operating System: Android 4.0 CPU: Allwinner A13 1.0GHz GPU: Mali-400 RAM: 512MB DDR3 Internal Memory: 4GB Expansion Memory: Support micro SD/TF card up to 32GB Keyboard Type: Virtual keyboard Input Mode: Handwritten and keyboard input Display Screen Screen Size: 7-inch Screen Type: Capacitive 5-point touch screen Screen Resolution:… |
|
|
7” A13 Google Android 4.0 AllWinner Tablet Boxchip Cortex A8 1.2Ghz MID Capacitive Touch Screen G-sensor WIFI, Camera, Skype Video Calling, Netflix, Flash Supported Dragon Touch(TM) MID7134B [By TabletExpress] (4GB Black) $67.98 TabletExpress is ONLY one authorized distributor by Dragon Touch(TM) in North America.Buy genius Dragon Touch (TM) tablet from TabletExpress only. Specification: Operation system: Android 4.0 Support HD Flash 11 Main IC: Allwinner A13 (CPU+ GPU+ VPU) Cortex A9 1.2G Screen: 7 inches 5 Points Capacitive Touch 16:9 .800 * 480 Storage: 4GB Memory: DDR3 512MB Storage Extend: 128MB-32GB SD/TF Sup… |





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
man, i googled for a script, ran across your code.
complete time saver man, props to you!
thanks so much.
[...] Quickly archive multiple directories into separate archive files | 64 Bit Jungle – [...]
?????????)