Linux 如何使bash脚本按特定数字对文件夹中的文件进行分类

Linux 如何使bash脚本按特定数字对文件夹中的文件进行分类,linux,macos,bash,unix,Linux,Macos,Bash,Unix,我在StackOverflow上找到了一个脚本,我对它做了一些修改。 该脚本对子文件夹中文件夹中的所有文件进行分类,每个子文件夹只有8个文件。但我有这样的文件名0541_2pcs.jpg。2件是指两件(副本) 因此,我希望脚本在将文件划分到每个文件夹时考虑到这一点。e、 g.一个文件夹可能有6个文件,这个0541_2pcs.jpg字面意思是2个文件,依此类推,具体取决于文件名中显示的数字 以下是脚本: cd photos; dir="${1-.}" x="${1-8}" let n=0 let

我在StackOverflow上找到了一个脚本,我对它做了一些修改。 该脚本对子文件夹中文件夹中的所有文件进行分类,每个子文件夹只有8个文件。但我有这样的文件名0541_2pcs.jpg。2件是指两件(副本)

因此,我希望脚本在将文件划分到每个文件夹时考虑到这一点。e、 g.一个文件夹可能有6个文件,这个0541_2pcs.jpg字面意思是2个文件,依此类推,具体取决于文件名中显示的数字

以下是脚本:

cd photos;
dir="${1-.}"
x="${1-8}"

let n=0
let sub=0
while IFS= read -r file ; do
    if [ $(bc <<< "$n % $x") -eq 0 ] ; then
            let sub+=1
            mkdir -p "Page-$sub"
            n=0
    fi

    mv "$file" "Page-$sub"
    let n+=1
done < <(find "$dir" -maxdepth 1 -type f)
cd照片;
dir=“${1-.}”
x=“${1-8}”
设n=0
设sub=0
而IFS=read-r文件;做

如果[$(bc您可以通过类似于
[${file/2pcs/x}!=$file]的代码添加一个文件名是否包含字符串“2pcs”的测试
。在下面显示的脚本中,如果该测试成功,则第二次递增
n
。注意,如果测试要包含在目录中的第八个文件是多段文件,则该目录中的文件将过多。这可以通过脚本不执行的其他测试来处理。注意,没有充分的理由如果脚本调用
bc
进行模数运算,并且从第一个参数设置
dir
x
都不起作用;我的脚本使用两个参数

#!/bin/bash

# To test pagescript, create dirs four and five in a tmp dir.
# In four, say
#    for i in {01..30}; do touch $i.jpg; done
#    for i in 03 04 05 11 16 17 18; do mv $i.jpg ${i}_2pcs.jpg; done
# Then in the tmp dir, say
#     rm -rf Page-*; cp four/* five/; ../pagescript five; ls -R

#cd photos;   # Leave commented out when testing script as above
dir="${1-.}"  # Optional first param is source directory
x=${2-8}      # Optional 2nd param is files-per-result-dir

let n=x
let sub=0
for file in $(find "$dir" -maxdepth 1 -type f)
do  # Uncomment next line to see values as script runs 
    #echo file is $file, n is $n, sub is $sub, dir is $dir, x is $x
    if [ $n -ge $x ]; then
        let sub+=1
        mkdir -p Page-$sub
        n=0
    fi
    mv "$file" Page-$sub
    [ ${file/2pcs/x} != $file ] && let n+=1
    let n+=1
done  

您的要求不是很清楚。您是想说所有前缀为
0541\uu
的文件都应该移动到它们自己的子目录中吗?链接到您在此处找到原始脚本的问题/答案。