From owner-freebsd-questions@FreeBSD.ORG Thu Oct 23 08:32:00 2003 Return-Path: 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 8995416A4B3 for ; Thu, 23 Oct 2003 08:32:00 -0700 (PDT) Received: from smtp.infracaninophile.co.uk (smtp.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id F03A843F93 for ; Thu, 23 Oct 2003 08:31:55 -0700 (PDT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [127.0.0.1]) h9NFVIDK063040 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 23 Oct 2003 16:31:48 +0100 (BST) (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost)id h9NFVIot063039; Thu, 23 Oct 2003 16:31:18 +0100 (BST) (envelope-from matthew) From: Matthew Seaman To: Steve D Message-ID: <20031023153118.GF39601@happy-idiot-talk.infracaninophile.co.uk> Mail-Followup-To: Matthew Seaman , Steve D , FreeBSD References: <7F19C442-0513-11D8-8F40-000393801C60@g-it.ca> <20031023113439.GC39601@happy-idiot-talk.infracaninophile.co.uk> <200310230825.20546.groups@xscd.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="pE2VAHO2njSJCslu" Content-Disposition: inline In-Reply-To: <200310230825.20546.groups@xscd.com> User-Agent: Mutt/1.5.4i X-Spam-Status: No, hits=-4.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=2.60 X-Spam-Checker-Version: SpamAssassin 2.60 (1.212-2003-09-23-exp) on happy-idiot-talk.infracaninophile.co.uk cc: FreeBSD Subject: Re: Fwd: Help: tar & find X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Thu, 23 Oct 2003 15:32:00 -0000 X-Original-Date: Thu, 23 Oct 2003 16:31:18 +0100 X-List-Received-Date: Thu, 23 Oct 2003 15:32:00 -0000 --pE2VAHO2njSJCslu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 23, 2003 at 08:25:20AM -0600, Steve D wrote: > 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 > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > #! /bin/bash > > set +x > > TAR_DIR=3D/home/tarbackups; > > FILES_DIR=3D/home/common; > > tar --remove-files -cvzpf $TAR_DIR/bak_files_`date +%F`.tar.gz\ > > `find $FILES_DIR -xdev -type f -iname "*.bak"`; > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > [...] > > 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 > [...] >=20 > --- --- --- >=20 > Matthew Seaman 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 > > >=20 > --- --- --- >=20 > 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=3D/home/tarbackups; > FILES_DIR=3D/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'/= "`; > --- >=20 > or the backticks in the last line replaced with the newer alternative "$(= )": >=20 > "$( find $FILES_DIR -xdev -type f -iname "*.bak" | sed "s/\(^.*$\)/'\= 1'/" )" ; >=20 > =20 > 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=20 > characters in sed's argument don't need to be further escaped. You've apparently got double quotes inside a double quoted string. That doesn't work. Trying to enclose the output of find in quote marks will sort of work, but it's generally found to be flaky. Especially when the filenames you're dealing with also contain quotation marks of various types or return characters. This is exactly why the '-print0' primitive for find(1) was invented: it puts out a list of file names separated by ascii NULL characters, which is one of the two ascii characters you can't get in a filename. (The other is '/' -- the directory separator). The other great advantage of xargs(1) is that it knows how many command line arguments it can supply to it's dependent command, and it will chop up a long argument list into managable chunks. Unfortunately the particular tar(1) command you're using can't be restarted in that way so it doesn't help here. You'ld only run into this problem if you were trying to backup many thousands of files, in which case your quoting the output of find trick would suffer in exactly the same way. There are many ways to solve that problem, should you encounter it. But I'll leave you to discover those for yourself if you need to. Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way PGP: http://www.infracaninophile.co.uk/pgpkey Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK --pE2VAHO2njSJCslu Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3 (FreeBSD) iD8DBQE/l/RGdtESqEQa7a0RAnPkAJ4z/eW0dZRxuNYYhDzcLVHiVBCxZACgizRK uG0t7lj1B93veAoVgCp8fxg= =0q9C -----END PGP SIGNATURE----- --pE2VAHO2njSJCslu--