Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Oct 2003 08:25:20 -0600
From:      Steve D <groups@xscd.com>
To:        FreeBSD <freebsd-questions@freebsd.org>
Subject:   Re: Fwd: Help: tar & find
Message-ID:  <200310230825.20546.groups@xscd.com>
In-Reply-To: <20031023113439.GC39601@happy-idiot-talk.infracaninophile.co.uk>
References:  <7F19C442-0513-11D8-8F40-000393801C60@g-it.ca> <20031023113439.GC39601@happy-idiot-talk.infracaninophile.co.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200310230825.20546.groups>