【问题标题】:Split files in Unix by 100 files in a folder将 Unix 中的文件按文件夹中的 100 个文件拆分
【发布时间】:2015-05-11 06:10:52
【问题描述】:

我正在尝试将一个包含 10000 个文件的文件夹拆分为多个包含 100 个文件的文件夹,每个文件夹位于同一个文件夹中。这将是一个剪切操作,而不是一个复制操作。我尝试了一个命令,但该命令将文件夹以及 100 个文件的一部分计算在内。

我试过的命令:

➜  ~  mv -- *([1,100]) ~/Downloads/Pics/<FolderCount>

在这里我每次都必须创建一个文件夹。我很乐意为所有创建的新文件夹按顺序排列文件夹列表或任何随机文件夹名称。

【问题讨论】:

    标签: macos shell unix


    【解决方案1】:

    也许像这样 - 但测试你的文件的副本。如果您喜欢它的作用,请删除 echo 这个词 - 目前它只说明它会做什么......

    #!/bin/bash
    ns=0   # number ofsubdirectory
    nf=0   # number of files moved
    
    # Loop through all files
    for f in *; do
       # Skip subdirectories
       [ -d "$f" ] && continue
    
       # Generate subdirectory name and create it, if necessary
       sub="sub-$ns"
       [ ! -d "$sub" ] && mkdir $sub 
    
       # Move file and count
       echo mv "$f" "$sub"
       ((nf++))
    
       if [ $nf -eq 100 ]; then
          ((ns++))         # start new subdirectory
          nf=0             # zero filecount
       fi
    done
    

    【讨论】:

    • 工作就像一个魅力...... :)
    猜你喜欢
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多