Zsh 根据找到的文件创建多个目录

Zsh 根据找到的文件创建多个目录,zsh,Zsh,我正在尝试创建目录来存储基于“find”命令的图像序列 假设在“test”目录中的不同位置有2个图像序列 test_123.####.dpx test_abc.####.dpx 我会运行类似于: testDir=$(find /Users/Tim/test/ -type f -name "test*.dpx") 它将返回上面列出的所有文件 我想做的是创建两个名为test_123和test_abc的目录 mkdir /Users/Tim/test/scan/${testDi

我正在尝试创建目录来存储基于“find”命令的图像序列

假设在“test”目录中的不同位置有2个图像序列

test_123.####.dpx
test_abc.####.dpx
我会运行类似于:

testDir=$(find /Users/Tim/test/ -type f -name "test*.dpx")
它将返回上面列出的所有文件

我想做的是创建两个名为test_123和test_abc的目录

mkdir /Users/Tim/test/scan/${testDir:t:r:r}
如果我运行这个,那么它将只创建一个目录,大概是基于第一个结果


我如何才能使这项工作能够创建共享相同基本名称的目录,以获得无限数量的结果?(不只是本例中的两个)。

如果在zsh中完成并不重要,您可以在python中这样做:

#!/usr/bin/python3
import os
x = 0
y = 0
if input(f"{os.getcwd()} is current working dir. press y to continue \n") == "y":
    for file in os.listdir(): # get files in current folder
        split = file.split(".") # split by .
        if len(split) > 1: # only files with more than one . are considered
            if split[0] not in os.listdir(): # if the folder it fits doesn't exist
                os.mkdir(split[0])  # make the folder
                print(f"made new folder {split[0]}")
                y = y + 1
            os.rename(file, os.path.join(split[0],file)) # then move it there - OS agnostic path generated!
            x = x + 1
print(f"moved {x} files to {y} folders!")

我在它运行之前添加了一个检查,只是为了防止发现它的随机用户造成严重破坏。
if len(split)>1:
还应该通过确保只有至少有2个点的文件被移动来防止一些事故,因为这是一种不寻常的命名方案。

您可以将匹配目录的列表存储到一个数组中,然后在此数组中循环并相应地处理每个条目。在COLD中,
testDir
只是一个字符串,它保存了
find
命令的完整输出。不是一个非常有用的数据结构来处理其组成部分。。。。