<?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 Loop Over Set of Files</title> <atom:link href="http://www.cyberciti.biz/faq/bash-loop-over-file/feed/" rel="self" type="application/rss+xml" /><link>http://www.cyberciti.biz/faq/bash-loop-over-file/</link> <description>Every answer asks a more beautiful question.</description> <lastBuildDate>Fri, 10 Feb 2012 19:55:56 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Vivek Gite</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-66628</link> <dc:creator>Vivek Gite</dc:creator> <pubDate>Wed, 11 Jan 2012 08:27:20 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-66628</guid> <description>Pure bash code and nothing else :)
&lt;pre&gt;
filecount=(*.pdf)
echo &quot;Total files in $PWD: ${#filecount[@]}&quot;
&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>Pure bash code and nothing else :)</p><pre>
 filecount=(*.pdf)
 echo "Total files in $PWD: ${#filecount[@]}"
</pre>]]></content:encoded> </item> <item><title>By: Potato</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-66619</link> <dc:creator>Potato</dc:creator> <pubDate>Wed, 11 Jan 2012 00:26:53 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-66619</guid> <description>try  COUNT=`ls -1 *.pdf &#124; wc -l`</description> <content:encoded><![CDATA[<p>try  COUNT=`ls -1 *.pdf | wc -l`</p> ]]></content:encoded> </item> <item><title>By: RDuke</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-66603</link> <dc:creator>RDuke</dc:creator> <pubDate>Tue, 10 Jan 2012 15:32:56 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-66603</guid> <description>@saad: maybe something like
count=$( ls -1 *.pdf &#124; wc -l )</description> <content:encoded><![CDATA[<p>@saad: maybe something like</p><p> count=$( ls -1 *.pdf | wc -l )</p> ]]></content:encoded> </item> <item><title>By: Saad</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-66581</link> <dc:creator>Saad</dc:creator> <pubDate>Tue, 10 Jan 2012 06:48:39 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-66581</guid> <description>Is it possible to know how many files will this loop run on,
e.g. in the following loop
for f in *.pdf
do
done;
can i know how many files are present in *.pdf glob, and can i use that to know how many files have been processed out of the total number</description> <content:encoded><![CDATA[<p>Is it possible to know how many files will this loop run on,</p><p>e.g. in the following loop</p><p>for f in *.pdf<br
/> do</p><p>done;</p><p>can i know how many files are present in *.pdf glob, and can i use that to know how many files have been processed out of the total number</p> ]]></content:encoded> </item> <item><title>By: Hari</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-65268</link> <dc:creator>Hari</dc:creator> <pubDate>Wed, 07 Dec 2011 12:21:58 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-65268</guid> <description>Hello I have a problem .i want to run a program N times by give N number of different .txt files from a directory. i want to give all the files in single time by using loops can any one help me to script .</description> <content:encoded><![CDATA[<p>Hello I have a problem .i want to run a program N times by give N number of different .txt files from a directory. i want to give all the files in single time by using loops can any one help me to script .</p> ]]></content:encoded> </item> <item><title>By: monr4</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-60779</link> <dc:creator>monr4</dc:creator> <pubDate>Fri, 15 Jul 2011 21:09:39 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-60779</guid> <description>hello, a have a problem, i need to do a script por a rutine something like
for i in $(cat list1) but with 2 list
for i in $(cat list1) AND b in $(cat list2);do mknod /dev/$i/group c64[$b];done
somebody helpme with that please</description> <content:encoded><![CDATA[<p>hello, a have a problem, i need to do a script por a rutine something like</p><p>for i in $(cat list1) but with 2 list<br
/> for i in $(cat list1) AND b in $(cat list2);do mknod /dev/$i/group c64[$b];done</p><p>somebody helpme with that please</p> ]]></content:encoded> </item> <item><title>By: Vivek Gite</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-60226</link> <dc:creator>Vivek Gite</dc:creator> <pubDate>Sat, 25 Jun 2011 15:29:34 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-60226</guid> <description>Also, see &lt;a href=&quot;http://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html&quot; rel=&quot;nofollow&quot;&gt;HowTo: Use Bash Parameter Substitution Like A Pro&lt;/a&gt;</description> <content:encoded><![CDATA[<p>Also, see <a
href="http://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html" rel="nofollow">HowTo: Use Bash Parameter Substitution Like A Pro</a></p> ]]></content:encoded> </item> <item><title>By: TheBonsai</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-60225</link> <dc:creator>TheBonsai</dc:creator> <pubDate>Sat, 25 Jun 2011 14:46:24 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-60225</guid> <description>http://wiki.bash-hackers.org/syntax/pe#common_use</description> <content:encoded><![CDATA[<p><a
href="http://wiki.bash-hackers.org/syntax/pe#common_use" rel="nofollow">http://wiki.bash-hackers.org/syntax/pe#common_use</a></p> ]]></content:encoded> </item> <item><title>By: Benjamin</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-60217</link> <dc:creator>Benjamin</dc:creator> <pubDate>Fri, 24 Jun 2011 20:12:48 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-60217</guid> <description>Nice compilation, thanks! One suggestion, though: I think many people would be looking for still another thing: how to get file extensions and how to get filenames without extensions.</description> <content:encoded><![CDATA[<p>Nice compilation, thanks! One suggestion, though: I think many people would be looking for still another thing: how to get file extensions and how to get filenames without extensions.</p> ]]></content:encoded> </item> <item><title>By: she</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-56174</link> <dc:creator>she</dc:creator> <pubDate>Mon, 07 Mar 2011 16:21:56 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-56174</guid> <description>can somebody help me develop a program that has the option whether to overwrite a document or not. when making a file, it will ask the user if he would want to create a new file on the same file name or overwrite the said filename.. please i need this badly.</description> <content:encoded><![CDATA[<p>can somebody help me develop a program that has the option whether to overwrite a document or not. when making a file, it will ask the user if he would want to create a new file on the same file name or overwrite the said filename.. please i need this badly.</p> ]]></content:encoded> </item> <item><title>By: Xa</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-52886</link> <dc:creator>Xa</dc:creator> <pubDate>Sun, 19 Dec 2010 04:14:30 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-52886</guid> <description>for i in &#039;ls&#039;
did NOT work for me. What does work is this:
for i in $(ls)</description> <content:encoded><![CDATA[<p>for i in &#8216;ls&#8217;<br
/> did NOT work for me. What does work is this:<br
/> for i in $(ls)</p> ]]></content:encoded> </item> <item><title>By: Vivek Gite</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-45206</link> <dc:creator>Vivek Gite</dc:creator> <pubDate>Thu, 17 Dec 2009 06:29:17 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-45206</guid> <description>@gurpur
Again, offtopic, go to our forum at nixcraft.com to post all your shell scripting related queries.</description> <content:encoded><![CDATA[<p>@gurpur</p><p>Again, offtopic, go to our forum at nixcraft.com to post all your shell scripting related queries.</p> ]]></content:encoded> </item> <item><title>By: gurpur</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-45203</link> <dc:creator>gurpur</dc:creator> <pubDate>Thu, 17 Dec 2009 03:29:35 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-45203</guid> <description>I need some in writing a script that will delete a specific line from a bunch of file in a directory.
your help is appreciated</description> <content:encoded><![CDATA[<p>I need some in writing a script that will delete a specific line from a bunch of file in a directory.<br
/> your help is appreciated</p> ]]></content:encoded> </item> <item><title>By: TheBonsai</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-41473</link> <dc:creator>TheBonsai</dc:creator> <pubDate>Tue, 05 May 2009 04:57:18 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-41473</guid> <description>@Jeff Schroeder:
&lt;cite&gt;
The for is easier to read, but it is really annoying when your scripts fail with the dreaded “argument list too long” errors.
&lt;/cite&gt;
This won&#039;t happen on a for-loop statement, since no exec() is done for the loop itself.</description> <content:encoded><![CDATA[<p>@Jeff Schroeder:</p><p><cite><br
/> The for is easier to read, but it is really annoying when your scripts fail with the dreaded “argument list too long” errors.<br
/></cite></p><p>This won&#8217;t happen on a for-loop statement, since no exec() is done for the loop itself.</p> ]]></content:encoded> </item> <item><title>By: rduke15</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-41163</link> <dc:creator>rduke15</dc:creator> <pubDate>Sun, 12 Apr 2009 14:53:57 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-41163</guid> <description>Found the problem with my previous example. The nullglob shell option needs to be set:
&lt;code&gt;shopt -s nullglob; for file in *.jpg; do echo &quot; the file variable is now &#039;$file&#039; &quot; ; done&lt;/code&gt;
produces no output, as expected. As opposed to:
&lt;code&gt;shopt -u nullglob; for file in *.jpg; do echo &quot; the file variable is now &#039;$file&#039; &quot; ; done
the file variable is now &#039;*.jpg&#039;&lt;/code&gt;</description> <content:encoded><![CDATA[<p>Found the problem with my previous example. The nullglob shell option needs to be set:<br
/> <code>shopt -s nullglob; for file in *.jpg; do echo " the file variable is now '$file' " ; done</code></p><p>produces no output, as expected. As opposed to:</p><p><code>shopt -u nullglob; for file in *.jpg; do echo " the file variable is now '$file' " ; done<br
/> the file variable is now '*.jpg'</code></p> ]]></content:encoded> </item> <item><title>By: rduke15</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-41162</link> <dc:creator>rduke15</dc:creator> <pubDate>Sun, 12 Apr 2009 14:40:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-41162</guid> <description>One curious problem. If there are NO files matching the glob, you get this:
&lt;code&gt;
$ for file in *.jpg; do echo &quot; the file variable is now &#039;$file&#039; &quot; ; done&lt;/code&gt;
the file variable is now &#039;*.jpg&#039;
&lt;/code&gt;
I would have expected that the contents of the for loop would not be executed at all, since there was no jpg file.</description> <content:encoded><![CDATA[<p>One curious problem. If there are NO files matching the glob, you get this:<br
/> <code><br
/> $ for file in *.jpg; do echo " the file variable is now '$file' " ; done</code><br
/> the file variable is now &#8216;*.jpg&#8217;</p><p>I would have expected that the contents of the for loop would not be executed at all, since there was no jpg file.</p> ]]></content:encoded> </item> <item><title>By: Michael Smith</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-40242</link> <dc:creator>Michael Smith</dc:creator> <pubDate>Wed, 11 Feb 2009 22:00:25 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-40242</guid> <description>I was initially a little confused by the thread. It&#039;s not useful to assign * to a variable if you only intend to use it for a loop. Furthermore, as others have stated, putting quotes around the variable name prevent the glob, in this case *, from expanding.
* Vivek is not correct that $FILES should be quoted.
* DAY&#039;s initial response that $FILES should be unquoted is not wrong, but using the variable at all is not useful.
* DAY&#039;s second idea of looping over the output of `ls` is a very common mistake. It&#039;s wrong because of &lt;a href=&quot;http://bash-hackers.org/wiki/doku.php/syntax/words&quot; title=&quot;Wordsplitting&quot; rel=&quot;nofollow&quot;&gt;wordsplitting&lt;/a&gt;.
* Brock Noland&#039;s instinct to use &lt;code&gt;for file in *.c...&lt;/code&gt; is spot-on.
* Jeff Schroeder is right to avoid ARG_MAX in general, but it only applies when you call exec*() via the kernel. Since for is a shell builtin, ARG_MAX doesn&#039;t apply here.
* David Thompson and Baz&#039;s comments are OK, but to Baz I would reiterate to avoid using the &lt;code&gt;ls&lt;/code&gt; command for anything except human-readable output.
* As for Chris&#039; comment: &lt;code&gt;FILES=&quot;*&quot;&lt;/code&gt; and &lt;code&gt;FILES=*&lt;/code&gt; are equivalent since sh-compliant shells don&#039;t expand globs during variable assignment.</description> <content:encoded><![CDATA[<p>I was initially a little confused by the thread. It&#8217;s not useful to assign * to a variable if you only intend to use it for a loop. Furthermore, as others have stated, putting quotes around the variable name prevent the glob, in this case *, from expanding.<br
/> * Vivek is not correct that $FILES should be quoted.<br
/> * DAY&#8217;s initial response that $FILES should be unquoted is not wrong, but using the variable at all is not useful.<br
/> * DAY&#8217;s second idea of looping over the output of `ls` is a very common mistake. It&#8217;s wrong because of <a
href="http://bash-hackers.org/wiki/doku.php/syntax/words" title="Wordsplitting" rel="nofollow">wordsplitting</a>.<br
/> * Brock Noland&#8217;s instinct to use <code>for file in *.c...</code> is spot-on.<br
/> * Jeff Schroeder is right to avoid ARG_MAX in general, but it only applies when you call exec*() via the kernel. Since for is a shell builtin, ARG_MAX doesn&#8217;t apply here.<br
/> * David Thompson and Baz&#8217;s comments are OK, but to Baz I would reiterate to avoid using the <code>ls</code> command for anything except human-readable output.<br
/> * As for Chris&#8217; comment: <code>FILES="*"</code> and <code>FILES=*</code> are equivalent since sh-compliant shells don&#8217;t expand globs during variable assignment.</p> ]]></content:encoded> </item> <item><title>By: Foo</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-40241</link> <dc:creator>Foo</dc:creator> <pubDate>Wed, 11 Feb 2009 21:47:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-40241</guid> <description>FILES=*; for f  in $FILES; do... is WRONG.
for f in `ls`; do... is even WORSE.
Both break with filenames including whitespaces, newlines etc.
Since this is about bash use array if you want files in variables:
files=(*.c)
for f in &quot;${files[@]}&quot;; do cmd &quot;$f&quot;; done
Or just use glob:
for f in *.c; do cmd &quot;$f&quot;; done</description> <content:encoded><![CDATA[<p>FILES=*; for f  in $FILES; do&#8230; is WRONG.<br
/> for f in `ls`; do&#8230; is even WORSE.<br
/> Both break with filenames including whitespaces, newlines etc.</p><p>Since this is about bash use array if you want files in variables:<br
/> files=(*.c)<br
/> for f in &#8220;${files[@]}&#8221;; do cmd &#8220;$f&#8221;; done</p><p>Or just use glob:<br
/> for f in *.c; do cmd &#8220;$f&#8221;; done</p> ]]></content:encoded> </item> <item><title>By: Chris</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-39426</link> <dc:creator>Chris</dc:creator> <pubDate>Thu, 11 Dec 2008 01:00:45 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-39426</guid> <description>&gt; FILES=”*” is wrong unless you want the value of $FILES to be *. The same is true of “*.c”. Lose the quotes to get what you want.
I dont think his wrong. He just gave those of us who are new to Bash scripting a placeholder for other commands or values. In fairness, the script did what it said it would do. Thanks for explaining the difference with using quotes and doing away with them.
For others reading this. Dont just take our words for it. Use the script - test it for yourself.  Play with it. Thanks and congratulations to nixcraft for sharing.
Peace!</description> <content:encoded><![CDATA[<p>&gt; FILES=”*” is wrong unless you want the value of $FILES to be *. The same is true of “*.c”. Lose the quotes to get what you want.</p><p>I dont think his wrong. He just gave those of us who are new to Bash scripting a placeholder for other commands or values. In fairness, the script did what it said it would do. Thanks for explaining the difference with using quotes and doing away with them.</p><p>For others reading this. Dont just take our words for it. Use the script &#8211; test it for yourself.  Play with it. Thanks and congratulations to nixcraft for sharing.</p><p>Peace!</p> ]]></content:encoded> </item> <item><title>By: Baz</title><link>http://www.cyberciti.biz/faq/bash-loop-over-file/#comment-38060</link> <dc:creator>Baz</dc:creator> <pubDate>Sun, 08 Jun 2008 00:12:52 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/?p=1061#comment-38060</guid> <description>Double quotes disable the special meaning of most enclosed characters.  They do not disable the interpretation of variables with a leading $.  To do this yopu need single quotes.
FILES=&quot;*&quot; is wrong unless you want the value of $FILES to be *.  The same is true of &quot;*.c&quot;.  Lose the quotes to get what you want.
I have always just used -
for F in *
do
... etc
for F in `ls`
is OK except that
for F in ls -1 (one)
is better, but both are more cumbersome and less elegant that
for F in * (or *.c and so on)</description> <content:encoded><![CDATA[<p>Double quotes disable the special meaning of most enclosed characters.  They do not disable the interpretation of variables with a leading $.  To do this yopu need single quotes.</p><p>FILES=&#8221;*&#8221; is wrong unless you want the value of $FILES to be *.  The same is true of &#8220;*.c&#8221;.  Lose the quotes to get what you want.</p><p>I have always just used -</p><p>for F in *<br
/> do<br
/> &#8230; etc</p><p>for F in `ls`<br
/> is OK except that<br
/> for F in ls -1 (one)<br
/> is better, but both are more cumbersome and less elegant that<br
/> for F in * (or *.c and so on)</p> ]]></content:encoded> </item> </channel> </rss>
