2

I wrote a small shell script to iterate through folder with names having numbers in them. The script is as below.

#!/bin/bash
for (( i = 100; i < 1000; i++))
do
    cp test0$i/test.out executables/test0$i.out
done

here he script traverses through test0100 .. to test0999. I want to enhance this script to traverse from test0000 to test1100 folders. I am not able to do that.

I am new to shell scripting. Any help is appreciated.

shx2
  • 61,779
  • 13
  • 130
  • 153
ajay bidari
  • 693
  • 2
  • 10
  • 22

3 Answers3

3

Using seq:

for i in $(seq -w 0 1100); do
    cp test$i/test.out executables/test$i.out
done

with the -w flag seq pads generated numbers with leading zeros such that all numbers have equal length.

perreal
  • 94,503
  • 21
  • 155
  • 181
2

How about this -

#!/bin/bash
for (( i = 0; i < 1100; i++))
do
    cp test$(printf "%04d" $i)/test.out executables/test$(printf "%04d" $i).out
done
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
0

With a recent bash

#!/bin/bash
for i in {0000..1100}; do
do
    cp test$i/test.out executables/test$i.out
done

Note that brace expansion occurs before variable expansion (see the manual) so if you want to do

start=0000
stop=1100
for i in {$start..$stop}

that won't work. In that case, use seq

glenn jackman
  • 238,783
  • 38
  • 220
  • 352