Re: OT: spaces in pathnames
Re: OT: spaces in pathnames
- Subject: Re: OT: spaces in pathnames
- From: Michael Abbott <email@hidden>
- Date: Thu, 9 Jun 2005 18:39:19 +0000 (GMT)
Thanks to all. In fact, I did find a tutorial, here:
http://www.hillside.co.uk/articles/C2/SE.C2.OCT.00.pdf
I'm not convinced that that tutorial gives you good advice at all. In
particular his policy of renaming all spaces to underscores is an attempt
to evade the problem rather than solve it.
In fact with careful use of quotes and care with using find it should be
possible to treat filenames with spaces without any problems.
The basic rule is, as you've already seen, if the file name is in a
variable then quote it:
"$1" or "$VAR"
If you're passing a list of command line arguments down to another process
then the form
"$@"
is needed to ensure all the arguments are quoted properly. There is a
similar form for quoting the context of a bash array:
"${ARRAY[@]}"
Of course, this rule about quoting also applies to anything generated
using $(...) unless you *know* it has no spaces, so always write like this
(here I make a stab at undoing the damage done to filenames in the
article!)
"$(echo "$1" | sed 's/_/ /g')"
When you're dealing with a list of filenames generated by another program
then you need some cooperation from the generator. Fortunately find is
quite obliging, and depending on what you're doing, you can write either
find . -name '*.c' -exec grep "$PATTERN" {} \;
or even
find . -name '*.c' -print0 | xargs -0 grep "$PATTERN"
to search for all c files containing the pattern.
Finally, what if you're given list of files in another file? Actually,
this is also easy (so long as noone is unkind enough to put \n in a
filename, in which case we're doomed):
cat file | while read -r; do whatever "$REPLY"; done
The moral is this: with a little care filenames with spaces can be handled
without any real trouble.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
X11-users mailing list (email@hidden)
This email sent to email@hidden