I have 535 folders (recup_dir.1
, recup_dir.2
, …, recup_dir.535
) and I want to merge (combine?) the contents of those folders into a single folder (lets say a folder named recup_dir
). Some files might have identical names (like img.jpg
), they should't overwrite the existing ones (instead they should be renamed to something like img1.jpg
, img2.jpg
and so on…).
Is there a way to do such thing using the command line?
Best Answer
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:The script:
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:
Press return and it is done.
The script:
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:
by: