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. Thank you jM -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.centos.org/pipermail/centos/attachments/20110123/96e61476/attachment-0002.html>
Hi,> 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 ;) Regards, Michel
On 1/23/11 2:51 PM, Johan Martinez wrote:> > 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?No, tar only generates a single output stream.> 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.What's not elegant about a script that does exactly what you want?> Also, it may not scale as number of subdirectories change. Any other solutions > or tips for doing this will be really helpful.Decide how you want to specify the list (in the script, in something the script can read, or a wildcard name expansion) and use a 'for' shell loop with the list that also expands the name into the output filename. -- Les Mikesell lesmikesell at gmail.com
Hello Johan, On Sun, 2011-01-23 at 14:51 -0600, Johan Martinez wrote:> I want to backup a directory using tar, but want separate tarballs for > each subdirectory. For example: # ls dir1 > subdir1 subdir2 subdir3Use find(1) for such cases. $ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz \; Regards, Leonard. -- mount -t life -o ro /dev/dna /genetic/research