Penguin
Blame: SpacesInPathNames
EditPageHistoryDiffInfoLikePages
Annotated edit history of SpacesInPathNames version 4, including all changes. View license author blame.
Rev Author # Line
4 LawrenceDoliveiro 1 Unix/Linux allows any character in a file/directory name except __/__ or __[NUL]__. However, embedded spaces, newlines and the like can cause problems for carelessly-written [Shell] scripts (or scripts for [carelessly-written shells|CshProgrammingConsideredHarmful]), and cause some command [PipeLine]s to fail completely.
1 LawrenceDoliveiro 2
2 LawrenceDoliveiro 3 To get around this, the [GNU] versions of find(1) and xargs(1) incorporate special extensions that work together. The __-print0__ option to __find__ causes it to output matching pathnames terminated by __NUL__ characters rather than newlines, thereby avoiding confusion with any embedded newlines that might occur in the pathnames. Correspondingly, the __xargs__ command takes the option __--null__ (shorter synonym __-0__) to indicate that its input is delimited in this way; though on my [SUSE] system I also have to specify __--max-lines=1__ (shorter synonym __-L1__).
1 LawrenceDoliveiro 4
5 For instance, suppose I have the following files:
6
7 <pre>
8 Events/Weddings/Freda's Wedding/data.txt
9 Events/Car Race/data.txt
10 Events/New Year Fireworks/data.txt
11 </pre>
12
13 and I want to count up the number of lines in each "data.txt" file. I can do this as follows:
14
15 <pre>
16 find Events -name data.txt -print0 | xargs -0 -L1 wc -l
17 </pre>
3 JohnMcPherson 18
19 ----
20 As discussed on the BashNotes page, scripts can cope with arguments that contain spaces if you are careful. <tt>bash</tt> will expand *$@* as all of its arguments, but importantly will expand *"$@"* as each argument surrounded by "quote marks". Eg, if a script is called with arguments
21 <verbatim>
22 ./myscript "argument 1" "argument 2"
23 </verbatim>
24 then a script that did
25 <verbatim>
26 perl -e 'foreach $a (@ARGV) {print ".$a.\n"}' $@
27 </verbatim>
28 would output
29 <verbatim>
30 .argument.
31 .1.
32 .argument.
33 .2.
34 </verbatim>
35 while a script that did
36 <verbatim>
37 perl -e 'foreach $a (@ARGV) {print ".$a.\n"}' "$@"
38 </verbatim>
39 would output
40 <verbatim>
41 .argument 1.
42 .argument 2.
43 </verbatim>