[Techtalk] Looping with numbers in filenames

Wim De Smet kromagg at gmail.com
Wed Feb 16 08:45:28 UTC 2011


On Tue, Feb 15, 2011 at 5:59 PM, Kathryn Hogg <kjh at flyballdogs.com> wrote:
>
> David Sumbler wrote:
>> I have some sets of files which have names such as file1.txt,
>> file2.txt ... file25.txt.
>>
>> I want to perform an operation on all the files:in other words, I need
>> to be able to do something like
>>
>> for integer = str(1 to 25)
>>      do something to file$integer.txt
>> next
>>
>> How can I do this at the bash input?
>
> num=1
> while expr $num '<' 26 >/dev/null
> do
>  echo $file${num}.txt
>  num=$(expr $num + 1)
> done

yet another way, similar to the above:
num=1
while [ $num -lt 1 ]
do
  echo "$file$num.txt"
  num=$(($num + 1))
done

Apparently all that is posix compliant too, though I don't know which
version of the standard.

cheers,
Wim


More information about the Techtalk mailing list