[CentOS] tar-ing subdirectories separately

Sun Jan 23 21:23:39 UTC 2011
Michel van Deventer <michel at van.deventer.cx>


> I want to backup a directory using tar, but want separate tarballs for
> each subdirectory. For example: 
> # ls dir1
> subdir1 subdir2 subdir3
> Will it possible to do it using only tar command? Or will I need
> another separate piece of logic/control? I thought of writing a shell
> script with three tar commands for each subdirectory, but that's not
> elegant way of doing it. Also, it may not scale as number of
> subdirectories change. Any other solutions or tips for doing this will
> be really helpful. 
You can try something like :
find /dir1 -type d -print -maxdepth 0 | while read DIR ; do tar cfv
$DIR.tar $DIR/; done

not tested, just off the top of my head and it's late, so if it breaks,
you can keep and be happy with all the pieces ;)