bash 如何将包含数千张图像的文件夹拆分为多个子文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17669176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I split a folder with thousands of images into multiple sub-folders?
提问by Todd Jenk
I have a directory with about 5000 images and I'd like to split/move it in to 50 folders (which will need to be created) with 100 images each.
我有一个包含大约 5000 个图像的目录,我想将它拆分/移动到 50 个文件夹(需要创建),每个文件夹包含 100 个图像。
Is there a way to do this using terminal?
有没有办法使用终端来做到这一点?
I'm running OS X.
我正在运行 OS X。
回答by Lri
i=0; for f in *; do d=dir_$(printf %03d $((i/100+1))); mkdir -p $d; mv "$f" $d; let i++; done
i=0; for f in *; do d=dir_$(printf %03d $((i/100+1))); mkdir -p $d; mv "$f" $d; let i++; done
回答by anubhava
awk one-liner can do that. Consider this awk command:
awk one-liner 可以做到这一点。考虑这个 awk 命令:
find . -name "*.JPG" | awk '!(++cnt%100) {"mkdir sub_" ++d|getline}'
Run it inside the folder with 5000 images. This will create 50 folders with the names sub_1, sub_2...sub_50.
在包含 5000 张图像的文件夹中运行它。这将创建 50 个名为 sub_1、sub_2...sub_50 的文件夹。
Also to move files into these newly created directories:
还要将文件移动到这些新创建的目录中:
find . -type f | awk '{
a[++cnt] = ##代码##
}
cnt==100 {
subd = "sub_" ++d;
system("mkdir " subd);
for (f in a)
system("mv " a[f] " " subd);
cnt=0
}'

