How do I recursively move and rename multiple files in multiple folders?

s_kennedy
s_kennedy used Ask the Experts™
on
I would prefer a method of doing this in Linux, but Windows would be acceptable.

Suppose I have one folder "documents", containing three folders, A, B,C, each containing three files, 1.txt, 2.txt, 3.txt.

I want to move and rename all the files into "documents", such that the names are A-1.txt, A-2.txt, A-3.txt, B-1.txt, B-2.txt, etc.

How can I do this?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007
Commented:
try this

for dir in A B C
do
   cd $dir
   for file in *.txt
   do
       mv $file ../$dir-$file
   done
   cd ..
done

Author

Commented:
I had to replace "A B C" with "*" for the general case, but it worked great, thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial