The following python script should do what you want. Just copy the contents in a file (named sc1.py
, say) in the folder which contains all the subfolders you want to merge. Then run the command chmod +x sc1.py
and then run this file in that folder ./sc1.py
and you should get the result. What this script will do is the following:
Say you are working in the folder /home/bob/foo
and you have hundreds of folders bar1
, bar2
,....,bar99
inside the folder foo
.
Run the script in /home/bob/foo/
and it will create a folder /home/bob/foo/Merged
and it will transfer the contents of all the folders bar1
,bar2
etc... inside the folder Merged
. The directory stuctures inside the folders bar1
,bar2
etc will remain intact
You may want to test this somewhere to see that this is what you asked for.
#!/usr/bin/env python
import subprocess as sbp
import os
path=os.getcwd()
fol = os.listdir(path)
p2 = os.path.join(path,'Merged')
sbp.Popen(['mkdir','Merged'])
for i in fol:
if os.path.isdir(i)==True:
if i!='Merged':
p1 = os.path.join(path,i)
p3 = 'cp -r "' + p1 +'"/* ' + p2
sbp.Popen(p3,shell=True)
Note: I am an absolute beginner at scripting and just learned most of these commands. If there are suggestions to improve from the folks around - greatly appreciated.