自动分割文件列表
这是我在这里发表的第一篇文章,尽管我非常频繁地来这里寻求解决方案! 我找不到我的问题的答案。也许我在搜索中使用了错误的标签:/无论如何,如果之前有人问过这个问题,请提前原谅我。
我想创建一个小脚本来自动从文件夹上传照片和视频。我目前使用一个名为“flickr_upload”的 Perl 脚本,可以在 macports 目录中找到。
假设一个目录中有 12 个文件。我想将这 12 个文件分成 3 批,每批 4 个文件,并使用 xterm 并行启动 3 个命令行。
如何通过指定批次数量自动拆分列表?
问候
编辑:而不是写:
flickr_upload photo01.jpg photo02.jpg photo03.jpg
我想要:
flickr_upload photo01.jpg
flickr_upload photo02.jpg
flickr_upload photo03.jpg
通过写类似的东西:
split -n 3 ./*.jpg | xterm flickr_upload -
This is my first post here although I've come here for solutions very very frequently!
I couldn't find an answer to a question I have. Maybe I'm using the wrong tags in my search :/ Anyway, please, pardon me in advance if the question was asked before.
I want to create a small script to automatically upload photos and videos from a folder. I currently use a perl script called "flickr_upload", found in the macports directory.
Let's say I have 12 files in a directory. I want to split these 12 files in 3 batches of 4 files and launch 3 command-lines in parallel using xterm for example.
How can automatically split a list by just specifying the number of batches?
Regards
Edit: instead of writing:
flickr_upload photo01.jpg photo02.jpg photo03.jpg
I'd like to have:
flickr_upload photo01.jpg
flickr_upload photo02.jpg
flickr_upload photo03.jpg
by writing something like:
split -n 3 ./*.jpg | xterm flickr_upload -
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
xargs
可以为您做到这一点。例如,
seq 10 | xargs echo
一次回显所有 10 个“参数”。但是 seq 10 | xargs -n 3 echo 一次执行 3 个:我不知道有关 flickr_upload 或“macports”可能是什么的任何信息,但想必您可以执行以下
操作: ls *jpg | xargs -n 3 echo 一次执行 3 个: xargs -n 3 flickr_upload
xargs
can do this for you.for instance,
seq 10 | xargs echo
echos all 10 "arguments" at once. Butseq 10 | xargs -n 3 echo
does them 3 at a time:I don't know anything about flickr_upload or what "macports" might be, but presumably you can do this:
ls *jpg | xargs -n 3 flickr_upload
如果您安装了 GNU Parallel http://www.gnu.org/software/parallel/您不需要计算 modulo+1:
如果您只想并行上传 3 个(其中 3 个始终运行):
您只需通过以下方式安装 GNU Parallel:
观看 GNU Parallel 的介绍视频以了解更多信息:
https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you do not need to calculate the modulo+1:
If you just want to upload 3 in parallel (with 3 running all the time):
You can install GNU Parallel simply by:
Watch the intro videos for GNU Parallel to learn more:
https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
xargs 可以运行多个线程:
以下是分桶的完成方式:
xargs can run multiple threads:
And here is how bucketing isdone: