4

I have a lot of folders that I need to merge. I know that if I drag the content from all of them over another one I can merge them. But with so many folders it will take me hours.

So, I need a way of merging all the folders inside a directory, with a bash script, or a program that does that.

EDIT As a plus, I would like it if the folder structure was preserved and inside all the folders there will be folders with the same names. These folders need to be merged.

5
  • 1
    That depends on how you have your folders organized. Do you want to merge ALL subfolders of a certain directory into one?
    – mbaitoff
    Sep 17, 2011 at 9:15
  • 1
    I don't understand the organization of the folders and files even after your edit. Do you need to sweep through the directory branch and copy all the files into one big pile< leaving the original directory structure intact, but empty?
    – mbaitoff
    Sep 17, 2011 at 13:01
  • You should explain better what you want, perhaps with the aid of an example.
    – enzotib
    Sep 17, 2011 at 18:34
  • I have folder "fol1" with many subdirectories. I have "fol2" with more subdirectories. I have "fol3" with many subdirectories too. I need fol1, fol2 and fol3 to be merged, and that all subdirectories inside it are also merged. Any duplicate files should be ignored. Thanks
    – user25119
    Sep 18, 2011 at 10:26
  • Should the merge destination contain only files with no directories after the desired operation? Folders fol1 fol2 fol3 resides in the same source directory?
    – mbaitoff
    Sep 18, 2011 at 16:00

3 Answers 3

2

I would suggest doing a search in the parent folder for . and include all subfolders. This will let you select all the files and then move them into another folder. This will merge all the contents into the new folder.

1

If this is your folder structure ...

fol1
| `-subfolA
| | `-one.txt
| `-subfolB
|   `-two.txt
fol2
 `-subfolA
  | `-een.txt
  `-subfolB
    `-twee.txt

... and you want ...

fol1
| `-subfolA
| | `-een.txt
| | `-one.txt
| `-subfolB
|   `-twee.txt
|   `-two.txt
fol2

... then you can:

  1. Go into fol1;
  2. hit Ctrl-A to select all subfolders;
  3. drag them into folB. Nautilus will see that there already exists a subfolA etc, and will ask whether you want to merge [this one/all of them] automatically.

Or you could open the terminal and

cd path/to/fol1
cp -r ../fol2/* ./

That automerges, too. Elaboratish explanation, just in case a bash novice encounters this answer:

  • cd changes directory.
  • cp copies; the first folder is the source, the second the destination.
  • -r means to descend into subfolders ('recurse').
  • ../fol2/* means one directory up / into fol2 / all files and folders you can find there.
  • ./ is the current directory.
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .