[CentOS] tar-ing subdirectories separately

Tue Jan 25 13:10:07 UTC 2011
Leonard den Ottolander <leonard at den.ottolander.nl>

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.

-- 
mount -t life -o ro /dev/dna /genetic/research