4

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?

muru
  • 72,889
  • 1
    You 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 Answers2

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
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)}'