Date: Tue, 13 Apr 2004 13:42:19 +0100 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: Peter Risdon <peter@circlesquared.com> Cc: FreeBSD Question List <freebsd-questions@freebsd.org> Subject: Re: Problems with filename with spaces (was Pipes and commands thatrequire two arguments) Message-ID: <20040413124219.GA92953@happy-idiot-talk.infracaninophile.co.uk> In-Reply-To: <407BC1F9.4050305@circlesquared.com> References: <40757879.1010307@circlesquared.com> <20040408165511.GD8182@dan.emsphone.com> <407BC1F9.4050305@circlesquared.com>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
On Tue, Apr 13, 2004 at 11:33:29AM +0100, Peter Risdon wrote:
> Thanks very much for the previous help - I missed the example in man xargs.
>
> The files I am trying to manipulate include spaces in the file names,
> and I cannot seem to escape them effectively. If I try something like:
>
> #find /usr/home -name *.rtf.exe -print0 | perl -p -e 's/\ /\\\ /g;' |
> xargs -0 -J % mv % /usr/newplace/
>
> Then although all spaces are preceded by backslashes I get errors every
> time a space is encountered. So for a file at /usr/home/user/this is a
> file.rtf.exe I get file does not exist errors from mv at:
> /usr/home/user/this\
> and at:
> is\
> and at:
> a\
> and at:
> file.rtf.exe (yes, a windows virus on a network share has been busy).
>
> I have also tried
>
> #find /usr/home -name *.rtf.exe -print0 | perl -p -e 's/\ /\\\ /g;' >
> listofdodgyfiles
>
> and the list is fine. If I paste a line into mv on the command line, it
> works. If I use a simple script to read the file, same errors as
> mentioned above.
>
> I'm sure I'm missing something obvious... Any nudges in the right
> direction would be gratefully received.
Errr... how about:
# find /usr/home -name '*.rtf.exe' -print0 | \
xargs -0 -J % mv % /usr/newplace/
or
# find /usr/home -name '*.rtf.exe' -print0 | \
perl -n0e '($x = $_) =~ s,^.*/,/usr/newplace/,; rename $_, $x;'
Note: this puts all of those files into a single directory and doesn't
do anything to avoid overwriting one file with another. I assume
that's what you want.
This sort of thing is the whole point of '-print0' -- it sidesteps all
of the things the shell does with significant characters when it turns
a command line into an argument list. ie. no escaping needed.
Cheers,
Matthew
--
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
[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (FreeBSD)
iD8DBQFAe+ArdtESqEQa7a0RAip5AJ0eZKQjm1gV7Q8p5xBz+kdfoNHeqQCglWpp
iV/A/Ke4GYY96fFnh8AqJIU=
=ajiz
-----END PGP SIGNATURE-----
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040413124219.GA92953>
