将大量文件分发到较小组的脚本

发布于 2024-10-15 07:27:44 字数 310 浏览 5 评论 0原文

我的文件夹包含大量不同大小的文件(例如 1000 个以上),我想将它们移至较小的组中,例如每个文件夹 100 个文件。

我编写了一个 Apple 脚本,该脚本对文件进行计数,创建一个编号的子文件夹,然后将 100 个文件移动到新文件夹(可以指定文件数量),该脚本循环直到移动到的文件数量少于指定数量它创建的最后一个文件夹。

问题是它运行速度慢得可怕。我正在寻找可以在我的 MacBook 和/或 Linux 机器上运行的 Apple 脚本或 shell 脚本,这将有效地将文件移动到更小的组中。

文件如何分组并不是特别重要,我只是希望每个文件夹中的文件更少。

I have folders containing large numbers of files (e.g. 1000+) of various sizes which I want to move in to smaller groups of, say, 100 files per folder.

I wrote an Apple Script which counted the files, created a numbered subfolder, and then moved 100 files in to the new folder (the number of files could be specified) which looped until there were less than specified number of files which it moved in to the last folder it created.

The problem was that it ran horrendously slowly. I'm looking for either an Apple Script or shell script I can run on my MacBook and/or Linux box which will efficiently move the files in to smaller groups.

How the files are grouped is not particularly significant, I just want fewer files in each folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め可乐爱微笑 2024-10-22 07:27:44

这应该可以帮助您开始:

DIR=$1
BATCH_SIZE=$2
SUBFOLDER_NAME=$3
COUNTER=1

while [ `find $DIR -maxdepth 1 -type f| wc -l` -gt $BATCH_SIZE ] ; do
  NEW_DIR=$DIR/${SUBFOLDER_NAME}${COUNTER}
  mkdir $NEW_DIR
  find $DIR -maxdepth 1 -type f | head -n $BATCH_SIZE | xargs -I {} mv {} $NEW_DIR
  let COUNTER++
if [ `find $DIR -maxdepth 1 -type f| wc -l` -le $BATCH_SIZE ] ; then
  mkdir $NEW_DIR
  find $DIR -maxdepth 1 -type f | head -n $BATCH_SIZE | xargs -I {} mv {} $NEW_DIR
fi
done

嵌套的 if 语句获取最后剩余的文件。修改供您使用后,您可以根据需要添加一些额外的检查。

This should get you started:

DIR=$1
BATCH_SIZE=$2
SUBFOLDER_NAME=$3
COUNTER=1

while [ `find $DIR -maxdepth 1 -type f| wc -l` -gt $BATCH_SIZE ] ; do
  NEW_DIR=$DIR/${SUBFOLDER_NAME}${COUNTER}
  mkdir $NEW_DIR
  find $DIR -maxdepth 1 -type f | head -n $BATCH_SIZE | xargs -I {} mv {} $NEW_DIR
  let COUNTER++
if [ `find $DIR -maxdepth 1 -type f| wc -l` -le $BATCH_SIZE ] ; then
  mkdir $NEW_DIR
  find $DIR -maxdepth 1 -type f | head -n $BATCH_SIZE | xargs -I {} mv {} $NEW_DIR
fi
done

The nested if statement gets the last remaining files. You can add some additional checks as you see needed after you modify for your use.

待天淡蓝洁白时 2024-10-22 07:27:44

这是一个巨大的混乱,但它不应该太慢:

rm /tmp/counter*
touch /tmp/counter1
find /source/dir -type f -print0 | 
    xargs -0 -n 100 \
        sh -c 'n=$(echo /tmp/counter*); \
               n=${n#/tmp/counter}; \
               counter="/tmp/counter$n"; \
               mv "$counter" "/tmp/counter$((n+1))"; \
               mkdir "/dest/dir/$n"; \
               mv "$@" "/dest/dir/$n"' _

它完全不区分哪些文件去哪里。

This is a tremendous kludge, but it shouldn't be too terribly slow:

rm /tmp/counter*
touch /tmp/counter1
find /source/dir -type f -print0 | 
    xargs -0 -n 100 \
        sh -c 'n=$(echo /tmp/counter*); \
               n=${n#/tmp/counter}; \
               counter="/tmp/counter$n"; \
               mv "$counter" "/tmp/counter$((n+1))"; \
               mkdir "/dest/dir/$n"; \
               mv "$@" "/dest/dir/$n"' _

It's completely indiscriminate as to which files go where.

-小熊_ 2024-10-22 07:27:44

解决目录中文件过多问题的最常见方法是按名称的前几个字符进行细分。例如:

之前:

aardvark
apple
architect
...
zebra
zork

之后:

a/aardvark
a/apple
a/architect
b/...
...
z/zebra
z/zork

如果细分得不够好,则更进一步:

a/aa/aardvark
a/ap/apple
a/ar/architect
...
z/ze/zebra
z/zo/zork

这应该可以很快完成,因为脚本执行的移动命令可以使用简单的 glob 扩展来选择要移动的所有文件,ala mv aa* a/aa,而不是必须在每个文件上单独运行移动命令(这将是我对原始脚本为何缓慢的第一个猜测)

The most common way to solve the problem of directories with too many files in them is to subdivide by the the first couple characters of the name. For example:

Before:

aardvark
apple
architect
...
zebra
zork

After:

a/aardvark
a/apple
a/architect
b/...
...
z/zebra
z/zork

If that isn't subdividing well enough, then go one step further:

a/aa/aardvark
a/ap/apple
a/ar/architect
...
z/ze/zebra
z/zo/zork

This should work quite quickly, because the move command that your script executes can use simple glob expansion to select all the files to move, ala mv aa* a/aa, as opposed to having to individually run a move command on each file (which would be my first guess as to why the original script was slow)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文