Ubuntu – How to combine hundreds of folders using command line

command linedirectoryfilesystemmerge

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:

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)
Related Question