linux bash - 从每个文件夹中移动 x 百分比的文件

linux bash - move x percentage of files from each folder

我有以下文件夹结构:

maindir
|-sub1
|-sub2
|-sub...
|-sub1000

我想要实现的是从每个子目录移动,例如。将每个文件夹包含的文件的 10% 放入具有相同结构的新文件夹 maindir2 中:

maindir2
|-sub1
|-sub2
|-sub...
|-sub1000

假设 maindir 的子目录中有以下文件数量(手镯中的数字):

maindir
|-sub1 (10)
|-sub2 (100)
|-sub...
|-sub1000 (50)

我想在 maindir2 中输出以下内容:

maindir
|-sub1 (1)
|-sub2 (10)
|-sub...
|-sub1000 (5)

在主目录中:

maindir
|-sub1 (99)
|-sub2 (90)
|-sub...
|-sub1000 (45)

请告诉我最好使用 bash 命令如何做到这一点。

是的,这是可能的 ;)

它看起来很丑(一行)但有效, 运行 它来自 maindir 的内部:

find . -type f -exec dirname {} + | uniq -c | while read n d;do echo "Directory:$d Files:$n Moving first:$(($n / 10))";mkdir -p ../maindir2${d:1};find $d -type f | head -n $(($n / 10)) | while read file;do mv $file ../maindir2${d:1}/;done;done

此致,