21

我有几个文件夹,每个文件夹包含 15,000 到 40,000 张照片。我希望将其中的每一个都拆分为子文件夹 - 每个文件夹中有 2,000 个文件。

有什么快速的方法可以在旅途中创建我需要的每个文件夹并移动所有文件?

目前我只能找到如何将文件夹中的前 x 个项目移动到预先存在的目录中。为了在包含 20,000 个项目的文件夹上使用它...我需要手动创建 10 个文件夹,并运行该命令 10 次。

ls -1  |  sort -n | head -2000| xargs -i mv "{}" /folder/

我尝试将其放入 for 循环中,但无法使用 mkdir 正确创建文件夹。即使解决了这个问题,我也需要该程序只为每 20 个文件创建文件夹(新组的开始)。它想为每个文件创建一个新文件夹。

那么......我怎样才能轻松地将大量文件移动到每个文件夹中任意数量的文件中?

任何帮助都会非常……嗯……有帮助!

4

8 回答 8

32

尝试这样的事情:

for i in `seq 1 20`; do mkdir -p "folder$i"; find . -type f -maxdepth 1 | head -n 2000 | xargs -i mv "{}" "folder$i"; done

完整脚本版本:

#!/bin/bash

dir_size=2000
dir_name="folder"
n=$((`find . -maxdepth 1 -type f | wc -l`/$dir_size+1))
for i in `seq 1 $n`;
do
    mkdir -p "$dir_name$i";
    find . -maxdepth 1 -type f | head -n $dir_size | xargs -i mv "{}" "$dir_name$i"
done

对于假人:

  1. 创建一个新文件:vim split_files.sh
  2. 更新dir_sizedir_name值以匹配您的需求
    • 请注意,dir_name将附加一个数字
  3. 导航到所需的文件夹:cd my_folder
  4. 运行脚本:sh ../split_files.sh
于 2015-03-18T09:23:21.643 回答
14

这个解决方案在 MacOS 上对我有用:

i=0; for f in *; do d=dir_$(printf %03d $((i/100+1))); mkdir -p $d; mv "$f" $d; let i++; done

它创建每个包含 100 个元素的子文件夹。

于 2018-08-27T19:11:31.373 回答
5

此解决方案可以处理带有空格和通配符的名称,并且可以轻松扩展以支持不太直接的树结构。它将在工作目录的所有直接子目录中查找文件,并将它们分类到这些子目录的新子目录中。新目录将命名为0,1等:

#!/bin/bash

maxfilesperdir=20

# loop through all top level directories:
while IFS= read -r -d $'\0' topleveldir
do
        # enter top level subdirectory:
        cd "$topleveldir"

        declare -i filecount=0 # number of moved files per dir
        declare -i dircount=0  # number of subdirs created per top level dir

        # loop through all files in that directory and below
        while IFS= read -r -d $'\0' filename
        do
                # whenever file counter is 0, make a new dir:
                if [ "$filecount" -eq 0 ]
                then
                        mkdir "$dircount"
                fi

                # move the file into the current dir:
                mv "$filename" "${dircount}/"
                filecount+=1

                # whenever our file counter reaches its maximum, reset it, and
                # increase dir counter:
                if [ "$filecount" -ge "$maxfilesperdir" ]
                then
                        dircount+=1
                        filecount=0
                fi
        done < <(find -type f -print0)

        # go back to top level:
        cd ..
done < <(find -mindepth 1 -maxdepth 1 -type d -print0)

find -print0/与进程替换的read组合已从另一个问题中窃取。

需要注意的是,简单的 globbing 也可以处理各种奇怪的目录和文件名。然而,它不容易扩展到多级目录。

于 2015-03-18T09:48:14.490 回答
5

下面的代码假定文件名不包含换行符、空格、制表符、单引号、双引号或反斜杠,并且文件名不以破折号开头。它还假设IFS未更改,因为它使用while read而不是while IFS= read,并且因为未引用变量。添加setopt shwordsplitZsh。

i=1;while read l;do mkdir $i;mv $l $((i++));done< <(ls|xargs -n2000)

下面的代码假定文件名不包含换行符并且它们不以破折号开头。-n2000一次接受 2000 个参数,{#}是作业的序列号。替换{#}'{=$_=sprintf("%04d",$job->seq())=}'将数字填充到四位数。

ls|parallel -n2000 mkdir {#}\;mv {} {#}

下面的命令假定文件名不包含换行符。它使用renameAristotle Pagaltzis 的实现,这是renameHomebrew 中的公式,-p需要在哪里创建目录,--stdin需要在哪里从 STDIN 获取路径,以及在哪里$N是文件的编号。在其他实现中,您可以使用$.or++$::i代替$N.

ls|rename --stdin -p 's,^,1+int(($N-1)/2000)."/",e'
于 2015-12-25T14:51:57.543 回答
4

我会用这样的东西:

#!/bin/bash
# outnum generates the name of the output directory
outnum=1
# n is the number of files we have moved
n=0

# Go through all JPG files in the current directory
for f in *.jpg; do
   # Create new output directory if first of new batch of 2000
   if [ $n -eq 0 ]; then
      outdir=folder$outnum
      mkdir $outdir
      ((outnum++))
   fi
   # Move the file to the new subdirectory
   mv "$f" "$outdir"

   # Count how many we have moved to there
   ((n++))

   # Start a new output directory if we have sent 2000
   [ $n -eq 2000 ] && n=0
done
于 2015-03-18T09:27:55.460 回答
2

上面的答案很有用,但是在 Mac(10.13.6) 终端中有一个非常重要的点。因为 xargs "-i" 参数不可用,所以我将命令从上方更改为下方。

ls -1 | sort -n | head -2000| xargs -I '{}' mv {} /folder/

然后,我使用下面的 shell 脚本(参考 tmp 的答案)

#!/bin/bash

dir_size=500
dir_name="folder"
n=$((`find . -maxdepth 1 -type f | wc -l`/$dir_size+1))
for i in `seq 1 $n`;
do
    mkdir -p "$dir_name$i";
    find . -maxdepth 1 -type f | head -n $dir_size | xargs -I '{}' mv {} "$dir_name$i"
done
于 2018-08-05T05:10:27.887 回答
0

您当然必须为此编写脚本。脚本中要包含的内容提示:

首先计算源目录中的文件数

NBFiles=$(find . -type f -name *.jpg | wc -l)

将此计数除以 2000 并加 1,以确定要创建的目录数

NBDIR=$(( $NBFILES / 2000 + 1 ))

最后遍历您的文件并将它们移动到子目录中。您将不得不使用两个叠瓦循环:一个选择并创建目标目录,另一个在此子目录中移动 2000 个文件,然后创建下一个子目录并将接下来的 2000 个文件移动到新目录,等等...

于 2015-03-18T09:27:00.230 回答
0

这是对 Mark Setchell 的调整

用法:bash splitfiles.bash $PWD/directoryofiles splitsize

它不需要脚本位于与要拆分的文件相同的目录中,它将对所有文件进行操作,而不仅仅是 .jpg 并允许您将拆分大小指定为参数。

#!/bin/bash
# outnum generates the name of the output directory
outnum=1
# n is the number of files we have moved
n=0

if [ "$#" -ne 2 ]; then
    echo Wrong number of args
    echo Usage: bash splitfiles.bash $PWD/directoryoffiles splitsize
    exit 1
fi

# Go through all files in the specified directory
for f in $1/*; do
   # Create new output directory if first of new batch
   if [ $n -eq 0 ]; then
      outdir=$1/$outnum
      mkdir $outdir
      ((outnum++))
   fi
   # Move the file to the new subdirectory
   mv "$f" "$outdir"

   # Count how many we have moved to there
   ((n++))

   # Start a new output directory if current new dir is full
   [ $n -eq $2 ] && n=0
done
于 2019-08-07T11:28:55.627 回答