0

I have 30 folders:

s-01
s-02
...
s-30

And, I have 30 files:

txtFile-001
txtFile-002
...
txtFile-030

How can I perform the following operation in one shot

mv txtFile-001 s-01/txtFile
mv txtFile-002 s-02/txtFile
...
mv txtFile-030 s-30/txtFile
3

With a for loop:

for i in $(seq -w 1 30); do mv -v "txtFile-0${i}" "s-${i}/txtFile"; done

This runs trough 01 to 30 and calls for every number the mv command, where $i contains the number.

Edit: without seq it would loop like:

for i in {01..30}; do mv -v "txtFile-0${i}" "s-${i}/txtFile"; done
5
  • Apologies, I updated the question, since the txtFiles have a different way of numbering than folders. It would help if you can update your answer accordingly. – rambalachandran Sep 8 '15 at 14:48
  • @WanderingMind Since it's only that leading 0 in the filename, just add it in the mv command or see my updated answer – chaos Sep 8 '15 at 14:51
  • It works for i>=10, but fails for i<10. I think it needs a conditional statement – rambalachandran Sep 8 '15 at 14:54
  • Does you seq accept the -w flag? You can also use the second statement in my updated answer – chaos Sep 8 '15 at 14:58
  • Yes my seq accepts -w command and it worked. Thank you – rambalachandran Sep 8 '15 at 15:48
1

In one shot?

prename 's!txtFile-0(..)$!s-$1/txtFile!' txtFile*

This uses a Regular Expression to split off the trailing two digits of txtFile-0nn and use it as the directory suffix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.