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
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.
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 subdir3
Use find(1) for such cases.
$ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;
Regards, Leonard.
On Mon, Jan 24, 2011 at 8:00 AM, Leonard den Ottolander leonard@den.ottolander.nl wrote:
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 subdir3
Use find(1) for such cases.
$ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;
Regards, Leonard.
Ahh-ahh-ahh! You forgot some subdirectories, especially generated from projects served to Windows systems, may have spaces in their names, and you'll want parentheses around those "{}" bits. Without those, chaos can ensue.
And don't get me *started* on what happens if some smart aleck starts slipping "$" into directory names. This is particularly common in Samba mounts of CIFS shares where the share creators cleverly left "$" on the names of the shares to keep them from being browsed.
Hello Nico,
On Mon, 2011-01-24 at 19:21 -0500, Nico Kadel-Garcia wrote:
On Mon, Jan 24, 2011 at 8:00 AM, Leonard den Ottolander
$ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;
Ahh-ahh-ahh! You forgot some subdirectories, especially generated from projects served to Windows systems, may have spaces in their names, and you'll want parentheses around those "{}" bits. Without those, chaos can ensue.
And don't get me *started* on what happens if some smart aleck starts slipping "$" into directory names.
I don't know what implementation of find you use, but the stock find on CentOS 4 and 5 does not do any shell expansions, despite what the man page might suggest. And why should it, find should be perfectly capable to quote its results before injecting them back into a shell.
See for yourself: $ mkdir testdir; cd testdir $ mkdir foo; touch foo/bar; mkdir foo\ bar; mkdir $PATH; mkdir \ .;rm\ -rf\ foo;; ls -1 $ find . -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ; $ ls foo
This is not a perl or shell script, it's a command that substitutes its own results back into a shell. Afacit it does this safely (compare printquoted.c). No user interaction in the form of the quoting of "{}" required. If your version of find does I would consider that a bug ;) .
Regards, Leonard.