2

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?

  • 1
    are they all .jpg? – Jacob Vlijm Sep 6 '14 at 18:58
  • no, they have all kinds of extensions. – Totally Not Julius Sep 6 '14 at 19:02
  • I have never tried this, but it seems like you should be able to copy the files using the cp command with the --backup option. The backup option can specify various types of backup file naming, including numbered. – Marty Fried Sep 6 '14 at 19:20
4

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)
  • I ended up using your solution. I had to run script with sudo and paths had to be absolute, but it worked... yeay :D Now I have another problem - the destination folder now should contain ~267k files. For the last 30min I've been waiting for it to load, but it just keeps loading... – Totally Not Julius Sep 7 '14 at 8:29
3

sudo cp -r --backup=t recup_dir.*/* recup_dir

this command will copy the contents of all the 535 folder to recup_dir and rename existing files to file.~1~ file.~2~ ..

the 535 folder and their contents will still exist , if you want to delete them after copying just add && sudo rm -rf recup_dir.* after the first command , so it will look like :

sudo cp -r --backup=t recup_dir.*/* recup_dir && sudo rm -rf recup_dir.

  • 3
    why would you use sudo here? – jfs Sep 6 '14 at 19:21
  • That's somewhat what I mentioned in my comment about the same time as your post. I think it should work, although I would do the deletion after making sure the results were correct. sudo is probably not needed if he has permissions for the files – Marty Fried Sep 6 '14 at 19:22
  • in case the directory is protected , or one of the folders contains a protected file , – younes zeboudj Sep 6 '14 at 19:25
  • because if it is the case , all file will be copied except the protected file and you will not be noticed at unless you use q verbose mode and search for errors , – younes zeboudj Sep 6 '14 at 19:27
  • 1
    yes this occur due the big number of argument to be executed in just one command , to avoid this issue you can paste this in your terminal : for folder in $(ls recup_dir.*); do sudo cp -r --backup=t $folder/* recup_dir; done this will not erase copied folder , to erase them in the same time when copying them use this one : for folder in $(ls recup_dir.*); do sudo cp -r --backup=t $folder/* recup_dir; sudo rm -rf $folder done ,this may take a while due the big number of files you that you have!! – younes zeboudj Sep 6 '14 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.