我有几个文件夹,每个文件夹有15,000到40,000张照片。我希望将每个文件拆分为子文件夹 - 每个文件夹中包含2,000个文件。
有什么快速的方法可以创建我需要的每个文件夹并移动所有文件?
目前我只能找到如何将文件夹中的前x个项目移动到预先存在的目录中。为了在包含20,000个项目的文件夹上使用它...我需要手动创建10个文件夹,并运行命令10次。
ls -1 | sort -n | head -2000| xargs -i mv "{}" /folder/
我尝试将它放入for循环中,但是在使用mkdir正确制作文件夹时遇到了麻烦。即使我解决了这个问题,我还是需要程序只为每个第20个文件创建文件夹(新组的开始)。它想为每个文件创建一个新文件夹。
那么......我怎样才能轻松地将大量文件移动到每个文件中任意数量文件的文件夹中?
任何帮助都会......非常......乐于助人!
此解决方案可以处理带有空格和通配符的名称,并且可以轻松扩展以支持不太直接的树结构。它将在工作目录的所有直接子目录中查找文件,并将它们分类到这些子目录的新子目录中。新目录将被命名为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
与过程替代的组合已从another question被盗。
应该注意的是,简单的globbing也可以处理各种奇怪的目录和文件名。但是,对于多级目录而言,它不易扩展。
尝试这样的事情:
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
下面的代码假定文件名不包含换行符,空格,制表符,单引号,双引号或反斜杠,并且文件名不以短划线开头。它还假设IFS
没有被改变,因为它使用while read
而不是while IFS= read
,并且因为变量没有被引用。在Zsh中添加setopt shwordsplit
。
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 {} {#}
以下命令假定文件名不包含换行符。它使用了亚里士多德Pagaltzis的rename
的实现,它是Homebrew中的rename
公式,需要-p
来创建目录,其中需要--stdin
来从STDIN获取路径,其中$N
是文件的编号。在其他实现中,您可以使用$.
或++$::i
而不是$N
。
ls|rename --stdin -p 's,^,1+int(($N-1)/2000)."/",e'
我会用这样的东西:
#!/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
This solution在MacOS上为我工作:
i=0; for f in *; do d=dir_$(printf %03d $((i/100+1))); mkdir -p $d; mv "$f" $d; let i++; done
它创建每个100个元素的子文件夹。
你肯定要写一个脚本。脚本中包含的事项提示:
首先计算源目录中的文件数
NBFiles=$(find . -type f -name *.jpg | wc -l)
将此计数除以2000并加1,以确定要创建的目录数
NBDIR=$(( $NBFILES / 2000 + 1 ))
最后遍历您的文件并将它们移动到子目录中。你将不得不使用两个叠加的循环:一个用于选择和创建目标目录,另一个用于在此子目录中移动2000个文件,然后创建下一个子目录并将下一个2000个文件移动到新的等等...
上面的答案非常有用,但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