<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: BASH Shell: For Loop File Names With Spaces</title> <atom:link href="http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html/feed" rel="self" type="application/rss+xml" /><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html</link> <description>This is a Linux sys admin journal by Vivek about sys admin work, Linux tips &#38; tricks, hacks, news and more.</description> <lastBuildDate>Fri, 10 Feb 2012 20:37:43 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Tom Thurman</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-178918</link> <dc:creator>Tom Thurman</dc:creator> <pubDate>Mon, 23 Jan 2012 20:14:38 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-178918</guid> <description>Thanks for the very clear explanation!</description> <content:encoded><![CDATA[<p>Thanks for the very clear explanation!</p> ]]></content:encoded> </item> <item><title>By: Chris F.A. Johnson</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-174244</link> <dc:creator>Chris F.A. Johnson</dc:creator> <pubDate>Thu, 22 Sep 2011 17:10:32 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-174244</guid> <description>&lt;pre&gt;
array=( &quot;${array[@]// /}&quot; )
&lt;/pre&gt;</description> <content:encoded><![CDATA[<pre>
array=( "${array[@]// /}" )
</pre>]]></content:encoded> </item> <item><title>By: zerocool</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-174148</link> <dc:creator>zerocool</dc:creator> <pubDate>Tue, 20 Sep 2011 00:59:10 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-174148</guid> <description>array=($(mysql --host=192.168.1.2 --user=XXXX --password=XXXX -s -N -e &#039;use xbmc_video; SELECT c22 FROM movie, files WHERE files.idFile=movie.idFile ORDER BY idMovie DESC LIMIT 10;&#039;))
how can i get rid of spaces in this array</description> <content:encoded><![CDATA[<p>array=($(mysql &#8211;host=192.168.1.2 &#8211;user=XXXX &#8211;password=XXXX -s -N -e &#8216;use xbmc_video; SELECT c22 FROM movie, files WHERE files.idFile=movie.idFile ORDER BY idMovie DESC LIMIT 10;&#8217;))</p><p>how can i get rid of spaces in this array</p> ]]></content:encoded> </item> <item><title>By: Rud Holmgren</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-173265</link> <dc:creator>Rud Holmgren</dc:creator> <pubDate>Sun, 14 Aug 2011 06:07:54 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-173265</guid> <description>For simple operations on multiple files you can use the -exec option on find:
&lt;blockquote&gt;
find . -iname &#039;*.avi&#039; -exec mv {} /Some/Where/Else/ \;
&lt;/blockquote&gt;
This command recursively finds all .avi files in the current directory and moves them to folder /Some/Where/Else. The curly brackets {} are substituted with file name. Consult the man page for more details.</description> <content:encoded><![CDATA[<p>For simple operations on multiple files you can use the -exec option on find:</p><blockquote><p> find . -iname &#8216;*.avi&#8217; -exec mv {} /Some/Where/Else/ \;</p></blockquote><p>This command recursively finds all .avi files in the current directory and moves them to folder /Some/Where/Else. The curly brackets {} are substituted with file name. Consult the man page for more details.</p> ]]></content:encoded> </item> <item><title>By: 8ohmh</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-172691</link> <dc:creator>8ohmh</dc:creator> <pubDate>Sat, 23 Jul 2011 14:02:28 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-172691</guid> <description>Thanks for your tip. I was not thinking anymore about IFS. Here&#039;s a tip to remove unwanted characters in filenames:
&lt;pre&gt;
SAVEIFS=$IFS
IFS=$(echo -en &quot;\n\b&quot;)
FILES=$1/*
for f in $FILES
do
#echo	&quot;$f&quot;
FILENAME=&quot;${f//[\?%\+]/_}&quot;;
mv -b --strip-trailing-slashes &quot;$f&quot; &quot;$FILENAME&quot;
done
IFS=$SAVEIFS
&lt;/pre&gt;
replaces ?, % and + with _ (Useful if you copy FAT32 files to linux and back)</description> <content:encoded><![CDATA[<p>Thanks for your tip. I was not thinking anymore about IFS. Here&#8217;s a tip to remove unwanted characters in filenames:</p><pre>
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
FILES=$1/*
for f in $FILES
do
	#echo	"$f"
	FILENAME="${f//[\?%\+]/_}";
	mv -b --strip-trailing-slashes "$f" "$FILENAME"
done
IFS=$SAVEIFS
</pre><p>replaces ?, % and + with _ (Useful if you copy FAT32 files to linux and back)</p> ]]></content:encoded> </item> <item><title>By: Stefan Mueller</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-171746</link> <dc:creator>Stefan Mueller</dc:creator> <pubDate>Sat, 04 Jun 2011 19:45:56 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-171746</guid> <description>Thanks to all contributors of this outstanding discussion!</description> <content:encoded><![CDATA[<p>Thanks to all contributors of this outstanding discussion!</p> ]]></content:encoded> </item> <item><title>By: Chris F.A. Johnson</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-169622</link> <dc:creator>Chris F.A. Johnson</dc:creator> <pubDate>Mon, 07 Mar 2011 17:50:35 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-169622</guid> <description>They do not do the same thing.
The first assigns a newline and a backspace to IFS; the second assigns an empty string.
The first assignment can be done more efficiently with:
&lt;pre&gt;
IFS=$&#039;\n\b&#039;
&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>They do not do the same thing.</p><p>The first assigns a newline and a backspace to IFS; the second assigns an empty string.</p><p>The first assignment can be done more efficiently with:</p><pre>
IFS=$'\n\b'
</pre>]]></content:encoded> </item> <item><title>By: jcubic</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-169620</link> <dc:creator>jcubic</dc:creator> <pubDate>Mon, 07 Mar 2011 17:03:03 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-169620</guid> <description>What is the difference between
&lt;pre&gt;
IFS=$(echo -en &quot;\n\b&quot;)
&lt;/pre&gt;
and
&lt;pre&gt;
IFS=&#039;&#039;
&lt;/pre&gt;
Which do exactly the same (but don&#039;t call sub shell)</description> <content:encoded><![CDATA[<p>What is the difference between</p><pre>
IFS=$(echo -en "\n\b")
</pre><p>and</p><pre>
IFS=''
</pre><p>Which do exactly the same (but don&#8217;t call sub shell)</p> ]]></content:encoded> </item> <item><title>By: Paul Grevink</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-168640</link> <dc:creator>Paul Grevink</dc:creator> <pubDate>Tue, 08 Feb 2011 12:08:10 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-168640</guid> <description>Thank you very much, this is very helpful.
I work with Linux for many year, but until today, I did not know this.</description> <content:encoded><![CDATA[<p>Thank you very much, this is very helpful.<br
/> I work with Linux for many year, but until today, I did not know this.</p> ]]></content:encoded> </item> <item><title>By: Vivek Gite</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-159555</link> <dc:creator>Vivek Gite</dc:creator> <pubDate>Sat, 11 Sep 2010 08:28:25 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-159555</guid> <description>How about:
&lt;pre&gt;find /home/gearond -maxdepth 1 -type f -print0 &#124; xargs -O -I file your-command-on file&lt;/pre&gt;
In your case:
&lt;pre&gt;find . -maxdepth 1 -type f -print0 &#124; xargs -0 -I {} tar -rjvf /tmp/directoryFiles.tar.bz2 {}
tar -tjvf /tmp/directoryFiles.tar.bz2&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>How about:</p><pre>find /home/gearond -maxdepth 1 -type f -print0 | xargs -O -I file your-command-on file</pre><p>In your case:</p><pre>find . -maxdepth 1 -type f -print0 | xargs -0 -I {} tar -rjvf /tmp/directoryFiles.tar.bz2 {}
tar -tjvf /tmp/directoryFiles.tar.bz2</pre>]]></content:encoded> </item> <item><title>By: Dennis Gearon</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-159553</link> <dc:creator>Dennis Gearon</dc:creator> <pubDate>Sat, 11 Sep 2010 06:24:05 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-159553</guid> <description>Trying to get the following to work. If I cut and paste the eventual contents of $directoryFiles EITHER in the bash script OR on the command line, it works perfectly. If I try to get it to do it directly in the script, it fails with:
tar --all the file names-- : Cannot stat: No such file or directory
I&quot;ve tried about every expansion or quoting that I can think of for the $direcotryFiles variable.
&lt;pre&gt;
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en &quot;\n\b&quot;)
directoryFiles=&quot;&quot;
for file in $( find /home/gearond -maxdepth 1 -type f)
do
directoryFiles=&quot;$directoryFiles\&quot;$file\&quot; &quot;
#echo $file
done
tar -cf /tmp/directoryFiles.tar.bz2  $directoryFiles
IFS=$SAVEIFS
&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>Trying to get the following to work. If I cut and paste the eventual contents of $directoryFiles EITHER in the bash script OR on the command line, it works perfectly. If I try to get it to do it directly in the script, it fails with:</p><p>tar &#8211;all the file names&#8211; : Cannot stat: No such file or directory</p><p>I&#8221;ve tried about every expansion or quoting that I can think of for the $direcotryFiles variable.</p><pre>
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
directoryFiles=""
for file in $( find /home/gearond -maxdepth 1 -type f)
do
  directoryFiles="$directoryFiles\"$file\" "
  #echo $file
done
tar -cf /tmp/directoryFiles.tar.bz2  $directoryFiles
IFS=$SAVEIFS
</pre>]]></content:encoded> </item> <item><title>By: Kelvin Nicholson</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-159178</link> <dc:creator>Kelvin Nicholson</dc:creator> <pubDate>Wed, 25 Aug 2010 21:40:22 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-159178</guid> <description>Fixed my little script! Cheers for posting this - always nice to learn something new.</description> <content:encoded><![CDATA[<p>Fixed my little script! Cheers for posting this &#8211; always nice to learn something new.</p> ]]></content:encoded> </item> <item><title>By: Matthias</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-158360</link> <dc:creator>Matthias</dc:creator> <pubDate>Mon, 26 Jul 2010 18:31:33 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-158360</guid> <description>File names can contain newline characters.
Gnome for example displays them correctly. Couldn&#039;t manage to create such a file in Gnome. I tested creation in C and Python iirc.</description> <content:encoded><![CDATA[<p>File names can contain newline characters.</p><p>Gnome for example displays them correctly. Couldn&#8217;t manage to create such a file in Gnome. I tested creation in C and Python iirc.</p> ]]></content:encoded> </item> <item><title>By: Rob Leach</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-157749</link> <dc:creator>Rob Leach</dc:creator> <pubDate>Mon, 12 Jul 2010 13:40:14 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-157749</guid> <description>This doesn&#039;t seem to work for me. I&#039;m wondering what I&#039;m doing wrong....
If I do this:
&lt;blockquote&gt;
$ for file in $(ls &#124; awk -F. &#039;{print $1}&#039;); do sox -S &quot;${file}.m4a&quot; &quot;${file}.ogg&quot;; done
&lt;/blockquote&gt;
it doesn&#039;t work as I would have expected.</description> <content:encoded><![CDATA[<p>This doesn&#8217;t seem to work for me. I&#8217;m wondering what I&#8217;m doing wrong&#8230;.</p><p>If I do this:</p><blockquote><p> $ for file in $(ls | awk -F. &#8216;{print $1}&#8217;); do sox -S &#8220;${file}.m4a&#8221; &#8220;${file}.ogg&#8221;; done</p></blockquote><p>it doesn&#8217;t work as I would have expected.</p> ]]></content:encoded> </item> <item><title>By: gringo guy</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-155100</link> <dc:creator>gringo guy</dc:creator> <pubDate>Mon, 19 Apr 2010 04:18:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-155100</guid> <description>Hey, thanks for this! I never did much bash scripting and was trying to figure out how to parse an array from a bash RC file into a Perl variable. I started out writing a long parser hack, but trying to support array entries with spaces was a big headache. Then thought maybe bash should do the work instead, and your examples helped a lot. Here&#039;s a one-liner bash script that&#039;s run using the Perl `backtick` operator.
$path is the RC file, and $var is the array variable. BASH variables must be escaped, but PERL
variables must not, and new-line chars have to be added so bash is happy with the string.
First step is to source the RC file, and next get the length of the array.
$list = `. $path; L=\${#${var}[@]}\n for (( i=0; i&lt;\${L}; i++ ));\n do\n echo -n \${${var}[\$i]}\,\n done`;
chop( $list );   # strip the trailing comma
foreach $entry (split(&quot;,&quot;, $list)) {    # Prove that it works
print &quot;entry: &#039;$entry&#039;\n&quot;;
}
Here&#039;s an example of the bash array that this will parse. Cool. Thanks again for your examples.
ARRAY_VAR=( .bashrc .vimrc &quot;foo bar&quot;  .java
.mozilla
.Xdefaults &quot;other file name&quot; )</description> <content:encoded><![CDATA[<p>Hey, thanks for this! I never did much bash scripting and was trying to figure out how to parse an array from a bash RC file into a Perl variable. I started out writing a long parser hack, but trying to support array entries with spaces was a big headache. Then thought maybe bash should do the work instead, and your examples helped a lot. Here&#8217;s a one-liner bash script that&#8217;s run using the Perl `backtick` operator.</p><p> $path is the RC file, and $var is the array variable. BASH variables must be escaped, but PERL<br
/> variables must not, and new-line chars have to be added so bash is happy with the string.<br
/> First step is to source the RC file, and next get the length of the array.</p><p> $list = `. $path; L=\${#${var}[@]}\n for (( i=0; i&lt;\${L}; i++ ));\n do\n echo -n \${${var}[\$i]}\,\n done`;<br
/> chop( $list );   # strip the trailing comma</p><p> foreach $entry (split(&quot;,&quot;, $list)) {    # Prove that it works<br
/> print &quot;entry: &#039;$entry&#039;\n&quot;;<br
/> }</p><p>Here&#039;s an example of the bash array that this will parse. Cool. Thanks again for your examples.</p><p> ARRAY_VAR=( .bashrc .vimrc &quot;foo bar&quot;  .java<br
/> .mozilla<br
/> .Xdefaults &quot;other file name&quot; )</p> ]]></content:encoded> </item> <item><title>By: piponazo</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-154216</link> <dc:creator>piponazo</dc:creator> <pubDate>Thu, 11 Mar 2010 15:01:14 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-154216</guid> <description>Great post! It has been very useful for me :D</description> <content:encoded><![CDATA[<p>Great post! It has been very useful for me :D</p> ]]></content:encoded> </item> <item><title>By: cfajohnson</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-153420</link> <dc:creator>cfajohnson</dc:creator> <pubDate>Tue, 09 Feb 2010 08:07:06 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-153420</guid> <description>&quot;grep hello $(ls &#124; sed -e “s/ /?/g”)&quot;
You don&#039;t need sed or ls; use filename expansion:
grep hello *
&quot;for i in $(find . &#124; sed -e “s/ /?/g) do; ls -l “$i”; done&quot;
That will fail if any filenames contain whitespace.
It is also extremely inefficient to call ls in a loop.</description> <content:encoded><![CDATA[<p>&#8220;grep hello $(ls | sed -e “s/ /?/g”)&#8221;</p><p>You don&#8217;t need sed or ls; use filename expansion:</p><p>grep hello *</p><p>&#8220;for i in $(find . | sed -e “s/ /?/g) do; ls -l “$i”; done&#8221;</p><p>That will fail if any filenames contain whitespace.</p><p>It is also extremely inefficient to call ls in a loop.</p> ]]></content:encoded> </item> <item><title>By: Chris Thiessen</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-153411</link> <dc:creator>Chris Thiessen</dc:creator> <pubDate>Mon, 08 Feb 2010 20:09:04 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-153411</guid> <description>My favorite solution, though it can theoretically overmatch:
find . &#124; sed -e &quot;s/ /?/g&quot;
ls &#124; sed -e &quot;s/ /?/g&quot;
Then you can do things like greps, for loops, whatever equally well:
grep hello $(ls &#124; sed -e &quot;s/ /?/g&quot;)
for i in $(find . &#124; sed -e &quot;s/ /?/g) do; ls -l &quot;$i&quot;; done</description> <content:encoded><![CDATA[<p>My favorite solution, though it can theoretically overmatch:</p><p>find . | sed -e &#8220;s/ /?/g&#8221;<br
/> ls | sed -e &#8220;s/ /?/g&#8221;</p><p>Then you can do things like greps, for loops, whatever equally well:<br
/> grep hello $(ls | sed -e &#8220;s/ /?/g&#8221;)<br
/> for i in $(find . | sed -e &#8220;s/ /?/g) do; ls -l &#8220;$i&#8221;; done</p> ]]></content:encoded> </item> <item><title>By: Shreyas Kulkarni</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-152375</link> <dc:creator>Shreyas Kulkarni</dc:creator> <pubDate>Sat, 19 Dec 2009 17:52:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-152375</guid> <description>excellent post. exactly what i was looking for. i had been facing this space-field-seperator problem for quite a sometime today. had tried many options to circumvent it, but in vain. will try this $IFS approach now.
thanks a lot for sharing.
shreyas</description> <content:encoded><![CDATA[<p>excellent post. exactly what i was looking for. i had been facing this space-field-seperator problem for quite a sometime today. had tried many options to circumvent it, but in vain. will try this $IFS approach now.</p><p>thanks a lot for sharing.</p><p>shreyas</p> ]]></content:encoded> </item> <item><title>By: cfajohnson</title><link>http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html#comment-152001</link> <dc:creator>cfajohnson</dc:creator> <pubDate>Wed, 02 Dec 2009 22:36:22 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/?p=3878#comment-152001</guid> <description>&lt;code&gt;for m in &quot;`ls`&quot;; do echo &quot;$m&quot; ; done&lt;/code&gt;
Why?
The &lt;code&gt;for&lt;/code&gt; command will only see one argument.
Try this:
&lt;code&gt;
n=1
for m in &quot;`ls`&quot;
do
echo &quot;$n&quot;
echo &quot;$m&quot;
n=$(( $n + 1 ))
done
&lt;/code&gt;
You&#039;ll see that it only goes through the loop once.
You don&#039;t need a loop for that, and you don&#039;t need &lt;code&gt;ls&lt;/code&gt;:
&lt;code&gt;printf &quot;%s\n&quot; *&lt;/code&gt;</description> <content:encoded><![CDATA[<p><code>for m in "`ls`"; do echo "$m" ; done</code></p><p>Why?</p><p>The <code>for</code> command will only see one argument.</p><p>Try this:</p><p><code><br
/> n=1<br
/> for m in "`ls`"<br
/> do<br
/> echo "$n"<br
/> echo "$m"<br
/> n=$(( $n + 1 ))<br
/> done<br
/> </code></p><p>You&#8217;ll see that it only goes through the loop once.</p><p>You don&#8217;t need a loop for that, and you don&#8217;t need <code>ls</code>:</p><p><code>printf "%s\n" *</code></p> ]]></content:encoded> </item> </channel> </rss>
