The script below moves all files from one directory, containing your 535 folders, (recursively) into another (single) directory, keeping their original filename.
In case of duplicates
(Only) in case of duplicate names, files will be renamed to duplicate_1_[filename]
, duplicate_2_[filename]
etc.
How to use
Copy the script below into an empty file, save it as rearrange.py
, set the correct paths to the source and destination (directories) and run it by:
python rearrange.py
The script:
#!/usr/bin/env python
import os
import shutil
# --------------------------------------------------------
reorg_dir = "/path/to/sourcedirectory"
target_dir = "/path/to/destination"
# ---------------------------------------------------------
for root, dirs, files in os.walk(reorg_dir):
for name in files:
subject = root+"/"+name
n = 1; name_orig = name
while os.path.exists(target_dir+"/"+name):
name = "duplicate_"+str(n)+"_"+name_orig; n = n+1
newfile = target_dir+"/"+name; shutil.move(subject, newfile)
For (gnome-) terminal- "drag and drop" functionality:
Use below version, save it as described above (but don't change anything) and make it executable. To use it, open a terminal window, drag the script over the terminal window, then the source directory, last the destination. The command you'll then see in your terminal:
rearrange.py /path/to/source /path/to/destination
Press return and it is done.
The script:
#!/usr/bin/env python
import os
import shutil
import sys
# --------------------------------------------------------
reorg_dir = sys.argv[1]
target_dir = sys.argv[2]
# ---------------------------------------------------------
for root, dirs, files in os.walk(reorg_dir):
for name in files:
subject = root+"/"+name
n = 1; name_orig = name
while os.path.exists(target_dir+"/"+name):
name = "duplicate_"+str(n)+"_"+name_orig; n = n+1
newfile = target_dir+"/"+name; shutil.move(subject, newfile)
Copy instead of move
If you'd like to keep your current directory untouched and only copy the files into a new directory, simply replace the last (section of) line:
replace:
shutil.move(subject, newfile)
by:
shutil.copy(subject, newfile)
cp
command with the--backup
option. The backup option can specify various types of backup file naming, including numbered.