From owner-freebsd-questions@FreeBSD.ORG  Thu Oct 23 07:24:55 2003
Return-Path: <owner-freebsd-questions@FreeBSD.ORG>
Delivered-To: freebsd-questions@freebsd.org
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id C0D6D16A4B3
	for <freebsd-questions@freebsd.org>;
	Thu, 23 Oct 2003 07:24:55 -0700 (PDT)
Received: from enmu.edu (EM01.enmu.edu [192.94.216.103])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 8171043F3F
	for <freebsd-questions@freebsd.org>;
	Thu, 23 Oct 2003 07:24:54 -0700 (PDT)	(envelope-from groups@xscd.com)
Received: from TSEH071.enmu.edu (TSEH071.enmu.edu [198.59.107.107])
	by enmu.edu (Postfix) with ESMTP id EA158BC161
	for <freebsd-questions@freebsd.org>;
	Thu, 23 Oct 2003 08:24:52 -0600 (MDT)
From: Steve D <groups@xscd.com>
To: FreeBSD <freebsd-questions@freebsd.org>
Date: Thu, 23 Oct 2003 08:25:20 -0600
User-Agent: KMail/1.5.2
References: <7F19C442-0513-11D8-8F40-000393801C60@g-it.ca>
	<20031023113439.GC39601@happy-idiot-talk.infracaninophile.co.uk>
In-Reply-To: <20031023113439.GC39601@happy-idiot-talk.infracaninophile.co.uk>
MIME-Version: 1.0
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Message-Id: <200310230825.20546.groups@xscd.com>
Subject: Re: Fwd: Help: tar & find
X-BeenThere: freebsd-questions@freebsd.org
X-Mailman-Version: 2.1.1
Precedence: list
List-Id: User questions <freebsd-questions.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-questions>,
	<mailto:freebsd-questions-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-questions>
List-Post: <mailto:freebsd-questions@freebsd.org>
List-Help: <mailto:freebsd-questions-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-questions>,
	<mailto:freebsd-questions-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Thu, 23 Oct 2003 14:24:55 -0000

On Wed, Oct 22, 2003 at 10:43:50PM -0600, Scott Gerhardt wrote:
> I am having trouble combining the tar and find command.  I want to
> tar and
> delete all .bak,.Bak,.BAK files.
>
> I am using the following command but keep receiving errors.
[...]
> The script is as follows
> =========================================
> #! /bin/bash
> set +x
> TAR_DIR=/home/tarbackups;
> FILES_DIR=/home/common;
> tar --remove-files -cvzpf $TAR_DIR/bak_files_`date +%F`.tar.gz\
>   `find $FILES_DIR -xdev -type f -iname "*.bak"`;
> ==========================================
[...]
> Here is some error output returned:
>
> tar: jobs/ROOF: Cannot stat: No such file or directory
> tar: LAYOUTS/RESIDENTIAL/FRASER/219: Cannot stat: No such file or
> directory
> tar: LEWIS: Cannot stat: No such file or directory
> tar: CRES.bak: Cannot stat: No such file or directory
[...]

--- --- ---

Matthew Seaman <m.seaman@infracaninophile.co.uk> replied:
> The problem is that you have file/directory names like 'ROOF LAYOUTS'
> which contain spaces and possibly other filenames containing
> characters with syntactic significance to the shell.
>
> Try:
>
>     find $FILES_DIR -xdev -type f -iname "*.bak -print0 | \
>         xargs -0 tar --remove-files -cvzpf $TAR_DIR/bak_files_`date
> +%F`.tar.gz
>

--- --- ---

Would the following approach also work? (Have sed surround each item
returned by the find command with single quotes?)
---
#! /bin/bash
set +x
TAR_DIR=/home/tarbackups;
FILES_DIR=/home/common;
tar --remove-files -cvzpf $TAR_DIR/bak_files_`date +%F`.tar.gz\
    `find $FILES_DIR -xdev -type f -iname "*.bak" | sed "s/\(^.*$\)/'\1'/"`;
---

or the backticks in the last line replaced with the newer alternative "$()":

    "$( find $FILES_DIR -xdev -type f -iname "*.bak" | sed "s/\(^.*$\)/'\1'/" )" ;

 
Do the characters \ * $ in sed's argument need to be quoted further,
to protect them from interpretation by the shell? The "find" portion
of the command works correctly, as written above, on my FreeBSD machine
using /bin/sh or /usr/local/bin/bash, but I don't know why those 
characters in sed's argument don't need to be further escaped.

--- --- ---

Steve D
Portales, NM US