Aero
Aero

Reputation: 311

Bash: Move multiple files with same name into same directory by renaming

I currently have a bunch of .txt files in separate folders and want to move them all into the same folder, except all the files have the same name. I would like to preserve all the files by adding some sort of number so that each one isn't overwritten, like FolderA/file.txt becomes NewFolder/file_1.txt, and FolderB/file.txt becomes NewFolder/file_2.txt, etc. Is there a clean way to do this using bash? Thanks in advance for your help!

Upvotes: 1

Views: 2005

Answers (2)

Fehmi Yasin
Fehmi Yasin

Reputation: 71

Following the previous answer, you can add two lines of code in bash to achieve your desired output:

declare -i n=1;
for i in A B C D E 
do
    mv Folder$i/file.txt NewFolder/file_$n.txt
    n+=1
done

Upvotes: 0

Pete Mitchell
Pete Mitchell

Reputation: 91

You could do something like this (either in a script or right on the command line):

for i in A B C D E  
do  
  mv Folder$i/file.txt NewFolder/file_$i.txt  
done  

It won't convert letters to numbers, but it does the basics of what you're looking for in a fairly simple fashion.

Hope this helps.

Upvotes: 3

Related Questions