将目录内容拆分为多个子目录


Answers:


23

以下对我来说效果很好。在终端中打开目录,然后复制并粘贴以下脚本,然后按Enter。将在内部创建名为dir_001,dir_002等的子目录。

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

这被标记为低质量。既然您是OP,您可以补充一下您所做的事情吗?
muru 2015年

1
完成。添加了解释。
Aneeez

1

使用带有范围和偏移量的数组移动固定数量的文件。

#!/bin/bash

shopt -s nullglob

a=(./src/*)
for ((i=0; i<${#a[@]}; i+=100)); do
    printf -v b ./img_%03d $((++n))
    mkdir -p $b && mv -t $b "${a[@]:$i:100}"
done

0

这将提示您需要多少个目录和子目录前缀。ls命令仅提醒您拥有多少文件。

ls -1 | wc -l

read -p 'How Many Directories: ' F;
read -p 'Sub-Directories Prefix: ' S;

PARRENT=${PWD}
# cd $PARRENT 
n=0
for i in *
do
  if [ $((n+=1)) -gt $F ]; then
    n=1
  fi
  todir=$PARRENT/"$S"_$n
  [ -d "$todir" ] || mkdir "$todir" 
  mv "$i" "$todir" 
done

来源:https//www.unix.com/shell-programming-and-scripting/248539-split-folder-huge-number-files-n-folders.html


如果当前目录的路径中有空格,恐怕会引起问题,但我可能是错的。
瓦尔说莫妮卡
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.