0
for j in * .* ; do 
    cp $j ../$name-S$i.gid/${j%%. * }$i.${j#*.}
done

Could someone tell me what this for loop does? What do *, % and # mean? ($name is a path of a directory in which there are -S1.gid ... -Sn.gid directory)

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
user277585
  • 163
  • 1
  • 1
  • 4
  • It is generally better to quote `$j` (i.e. `"$j"`) and the rest. If a file name contains whitespace then the unquoted command breaks. – Hauke Laging May 04 '14 at 23:16

1 Answers1

3
  • It loops (for foo in bar; do something; done) over the files matching the * and .* globs and
    • copies (cp)
      • each file ($j) to
      • a path composed of
        • the parent directory (../)
        • followed by a directory path
          • starting with the value of $name,
          • followed by -S,
          • some other unknown variable ($i)
          • and finally .gid/,
        • then a file name starting with
          • the string remaining after removing from the end of the file (${j...}) the longest string (%%) matching the glob . *, meaning
            • a dot
            • followed by a space,
            • followed by any number of characters
            • followed by a space
          • followed by the value of $i,
          • followed by a dot
          • and finally the string remaining after removing from the start of the file (${j...}) the shortest string matching the glob *., meaning
            • any number of characters
            • followed by a dot.

All this is explained in man bash. This code should be simplified to use quoted variables for each of those expansions to explain what they are. It's not maintainable as is IMO.

l0b0
  • 50,672
  • 41
  • 197
  • 360