Shweta
Shweta

Reputation: 1161

Split a folder which has 100s of subfolders with each having few files into one more level of subfolder using shell

I have a following data dir:

root/A/1
root/A/2
root/B/1
root/B/2
root/B/3
root/C/1
root/C/2

And I want to convert it into following file structure:

root2/I/A/1
root2/I/A/2
root2/I/B/1
root2/I/B/2
root2/I/B/3
root2/II/C/1
root2/II/C/2

Purpose of doing it is I want to run some script which takes home folder (root here) and runs on it. And I want to run it in parallel on many folders(I, II) to speed up the process.

Simple assumption about file and folder name is that all are alphanumeric, even no period or underscore.

Edit: I tried following:

for i in `seq 1 30`; do mkdir -p "root2/folder$i"; find root -type f | head -n 4000 | xargs -i cp "{}" "root2/folder$i"; done

Problem is that it creates something like following, which is not what i wanted.

root2/I/1
root2/I/2
root2/I/1
root2/I/2
root2/I/3
root2/II/1
root2/II/2

Upvotes: 0

Views: 354

Answers (1)

Мона_Сах
Мона_Сах

Reputation: 322

You may wish to use a lesser known command called dirsplit, the usual application of which is to split a directory into multiple directories for burning purposes.

Use it like below :

dirsplit -m -s 300M /root/ -p /backup/folder1

Options implies below stuff :
-m|--move     Move files to target dirs
-e 2 special exploration mode, 2 means files in directory are put together
-p prefix to be attached to each directory created, in you case I, II etc
-s Maximum size allowed for each new folder created.

For more information see :

dirsplit -H

Upvotes: 1

Related Questions