I have a directory with 52 subdirectories, and I'd like to split them in 11 folders with 5 subdirectories each of them. Can anyone suggest me a way to achieve this?
Asked
Active
Viewed 712 times
4
-
1You could use a python script. – Alex Lowe Dec 13 '15 at 02:29
-
This may be useful as well.. just make sure your new $file names are not at the end of ls when tail is run: http://unix.stackexchange.com/questions/12976/how-to-move-100-files-from-a-folder-containing-thousands – cutrightjm Dec 13 '15 at 02:31
2 Answers
2
In the first place, you're asking for a mathematical impossibility, but I'll overlook it.
The basic thing you ask is very simply done:
[ ! -e split ] &&
set ./*/ &&
while mkdir split && [ 4 -lt "$#" ]
do mv "$1" "$2" "$3" "$4" "$5" split
mv split "${1%/}"
shift 5
done&& mv "$@" split && mv split "${1%/}"
Because you don't specify any kind of names or similar that takes some care to avoid overwriting anything, and winds up just moving every 5 directories as sorted lexicographically into a directory named for every 5th. That is, it does so if there is no file or directory in the current directory named split

mikeserv
- 58,310
-
That works pretty well, though I wonder if it is possible to put a sequential name like folder01, or something similar? – Rodrigo Morales Alvarado Dec 15 '15 at 18:41
-
@RodrigoMoralesAlvarado -- of course it is possible, but you didn't ask for it, and you didn't provide any list of names. it would have been irresponsible of me to suggest you do so if there had been a folder01 in the directory already. – mikeserv Dec 15 '15 at 21:23
-
Yeah, well there is any folder with that name, neither split. Thanks. – Rodrigo Morales Alvarado Dec 17 '15 at 03:47
0
I dont know if i get it wrong but you could try something like this:
ls -1 | awk '{tmp=int($NR/5);system("mkdir Folder"$tmp);system("mv "$1" Folder"$tmp)}'
-
This works well, except in the case that there is files in the main folder, because it also creates a folder for it and copy it. Is there anyway to exclude files? – Rodrigo Morales Alvarado Dec 14 '15 at 04:22
-
-
When I'm run a test with 6 directories, it move each directory to a new one with the prefix Folder, but do not split them in 5 folders for each new folder. – Rodrigo Morales Alvarado Dec 15 '15 at 18:18