在 terminal/bash 脚本中将文件夹拆分为多个子文件夹
Split a folder into multiple subfolders in terminal/bash script
我有几个文件夹,每个文件夹有 15,000 到 40,000 张照片。我希望将其中的每一个都分成子文件夹 - 每个子文件夹中有 2,000 个文件。
有什么快速方法可以创建我需要的每个文件夹并移动所有文件?
目前我只能找到如何将文件夹中的前 x 项移动到预先存在的目录中。为了在包含 20,000 个项目的文件夹上使用它...我需要手动创建 10 个文件夹,然后 运行 命令 10 次。
ls -1 | sort -n | head -2000| xargs -i mv "{}" /folder/
我尝试将其放入 for 循环中,但无法使用 mkdir 正确创建文件夹。即使在我解决了这个问题之后,我也需要程序只为每 20 个文件(新组的开始)创建文件夹。它想为每个文件创建一个新文件夹。
那么...我怎样才能轻松地将大量文件移动到每个文件夹中包含任意数量文件的文件夹中?
任何帮助都会非常……很好……很有帮助!
尝试这样的事情:
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
对于傻瓜:
- 创建一个新文件:
vim split_files.sh
- 更新
dir_size
和 dir_name
值以满足您的需求
- 请注意
dir_name
将附加一个数字
- 导航到所需的文件夹:
cd my_folder
- 运行 脚本:
sh ../split_files.sh
您当然必须为此编写脚本。
脚本中要包含的内容提示:
首先计算源目录中的文件数
NBFiles=$(find . -type f -name *.jpg | wc -l)
将此计数除以 2000 并加 1,以确定要创建的目录数
NBDIR=$(( $NBFILES / 2000 + 1 ))
最后遍历您的文件并将它们移动到子目录中。
你必须使用两个叠层循环:一个选择并创建目标目录,另一个在这个子目录中移动 2000 个文件,然后创建下一个子目录并将下一个 2000 个文件移动到新的子目录,等等......
我会选择这样的东西:
#!/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
此解决方案可以处理带有空格和通配符的名称,并且可以轻松扩展以支持不太直接的树结构。它将在工作目录的所有直接子目录中查找文件,并将它们分类到这些子目录的新子目录中。新目录将被命名为 0
、1
等:
#!/bin/bash
maxfilesperdir=20
# loop through all top level directories:
while IFS= read -r -d $'[=10=]' 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 $'[=10=]' 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 中窃取。
需要注意的是,简单的通配符也可以处理各种奇怪的目录和文件名。然而,它不容易扩展为多级目录。
下面的代码假定文件名不包含换行符、空格、制表符、单引号、双引号或反斜杠,并且文件名不以破折号开头。它还假定 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 {} {#}
下面的命令假定文件名不包含换行符。它使用 Aristotle Pagaltzis 的 rename
实现,这是 Homebrew 中的 rename
公式,其中需要 -p
来创建目录,需要 --stdin
从 STDIN 获取路径,其中 $N
是文件的编号。在其他实现中,您可以使用 $.
或 ++$::i
而不是 $N
.
ls|rename --stdin -p 's,^,1+int(($N-1)/2000)."/",e'
上面的回答很有用,但是在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
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 个元素的子文件夹。
这是对 Mark Setchell 的
的调整
用法:bash splitfiles.bash $PWD/directoryoffiles 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 /*; do
# Create new output directory if first of new batch
if [ $n -eq 0 ]; then
outdir=/$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 ] && n=0
done
我有几个文件夹,每个文件夹有 15,000 到 40,000 张照片。我希望将其中的每一个都分成子文件夹 - 每个子文件夹中有 2,000 个文件。
有什么快速方法可以创建我需要的每个文件夹并移动所有文件?
目前我只能找到如何将文件夹中的前 x 项移动到预先存在的目录中。为了在包含 20,000 个项目的文件夹上使用它...我需要手动创建 10 个文件夹,然后 运行 命令 10 次。
ls -1 | sort -n | head -2000| xargs -i mv "{}" /folder/
我尝试将其放入 for 循环中,但无法使用 mkdir 正确创建文件夹。即使在我解决了这个问题之后,我也需要程序只为每 20 个文件(新组的开始)创建文件夹。它想为每个文件创建一个新文件夹。
那么...我怎样才能轻松地将大量文件移动到每个文件夹中包含任意数量文件的文件夹中?
任何帮助都会非常……很好……很有帮助!
尝试这样的事情:
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
对于傻瓜:
- 创建一个新文件:
vim split_files.sh
- 更新
dir_size
和dir_name
值以满足您的需求- 请注意
dir_name
将附加一个数字
- 请注意
- 导航到所需的文件夹:
cd my_folder
- 运行 脚本:
sh ../split_files.sh
您当然必须为此编写脚本。 脚本中要包含的内容提示:
首先计算源目录中的文件数
NBFiles=$(find . -type f -name *.jpg | wc -l)
将此计数除以 2000 并加 1,以确定要创建的目录数
NBDIR=$(( $NBFILES / 2000 + 1 ))
最后遍历您的文件并将它们移动到子目录中。 你必须使用两个叠层循环:一个选择并创建目标目录,另一个在这个子目录中移动 2000 个文件,然后创建下一个子目录并将下一个 2000 个文件移动到新的子目录,等等......
我会选择这样的东西:
#!/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
此解决方案可以处理带有空格和通配符的名称,并且可以轻松扩展以支持不太直接的树结构。它将在工作目录的所有直接子目录中查找文件,并将它们分类到这些子目录的新子目录中。新目录将被命名为 0
、1
等:
#!/bin/bash
maxfilesperdir=20
# loop through all top level directories:
while IFS= read -r -d $'[=10=]' 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 $'[=10=]' 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 中窃取。
需要注意的是,简单的通配符也可以处理各种奇怪的目录和文件名。然而,它不容易扩展为多级目录。
下面的代码假定文件名不包含换行符、空格、制表符、单引号、双引号或反斜杠,并且文件名不以破折号开头。它还假定 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 {} {#}
下面的命令假定文件名不包含换行符。它使用 Aristotle Pagaltzis 的 rename
实现,这是 Homebrew 中的 rename
公式,其中需要 -p
来创建目录,需要 --stdin
从 STDIN 获取路径,其中 $N
是文件的编号。在其他实现中,您可以使用 $.
或 ++$::i
而不是 $N
.
ls|rename --stdin -p 's,^,1+int(($N-1)/2000)."/",e'
上面的回答很有用,但是在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
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 个元素的子文件夹。
这是对 Mark Setchell 的
的调整用法:bash splitfiles.bash $PWD/directoryoffiles 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 /*; do
# Create new output directory if first of new batch
if [ $n -eq 0 ]; then
outdir=/$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 ] && n=0
done