查找以相同 17 个字符结尾的文件组

Find groups of files that end with the same 17 characters

我正在抓取具有独特且常见模式的文件。我试图在共同点上进行匹配。目前正在尝试 bash。我可以使用 python 或其他任何东西。

file1_02_01_2021_002244.mp4
file2_02_01_2021_002244.mp4
file3_02_01_2021_002244.mp4
# _02_01_2021_002244.mp4 should be the 'match all files that contain this string'

file1_03_01_2021_092200.mp4
file2_03_01_2021_092200.mp4
file3_03_01_2021_092200.mp4
# _03_01_2021_092200.mp4 is the match
...    
file201_01_01_2022_112230.mp4
file202_01_01_2022_112230.mp4
file203_01_01_2022_112230.mp4
# _01_01_2022_112230.mp4 is the match

目标是找到从文件末尾到第一个 uniq 字符匹配的所有内容,然后将它们移动到一个文件夹中。可操作的部分将很容易。我只需要匹配方面的帮助。

find -type f $("all that match the same last 17 characters of the file name"); do
    do things
done

这是我的示例目录:

total 28480
drwxr-xr-x  2 user  user    64B Feb 24 10:49 dir1
drwxr-xr-x  2 user  user    64B Feb 24 10:49 dir2
-rw-r--r--  2 user  user   6.8M Feb 24 08:59 file1_02_01_2021_002244.mp4
-rw-r--r--  2 user  user   468K Feb 24 09:06 file1_03_01_2021_092200.mp4
-rw-r--r--  2 user  user   4.5M Feb 24 08:59 file2_02_01_2021_002244.mp4
-rw-r--r--  2 user  user   665K Feb 24 09:06 file2_03_01_2021_092200.mp4
-rw-r--r--  1 user  user     0B Feb 24 10:49 otherfile1
-rw-r--r--  1 user  user     0B Feb 24 10:49 otherfile2

我已将其与标记为正确的答案中的建议一起使用。他们 python 方法可能会更好地工作(尤其是文件名中有空格)但我对 python 的熟练程度不足以让它做我想做的一切。完整的脚本如下:

#!/usr/local/bin/bash
# this is my solution
# create array with patterns
aPATTERN=($(find . -type f -name "*.mp4" | sed 's/^[^_]*//'|sort -u ))

# itterate through all patterns, do things
for each in ${aPATTERN[@]}; do
        # create a temp working directory for files that match the pattern
        vDIR=`gmktemp -d -p $(pwd)`
        # create array of all files found matching the pattern
        aFIND+=(`find . -mindepth 1 -maxdepth 1 -type f -iname \*$each`)
        # move all files that match the match to the working temp directory
        for file in ${aFIND[@]}; do
                mv -iv $file $vDIR
        done
        # reset the found files array, get ready for next pattern
        aFIND=()
done

试试这个:

#!/bin/bash

while IFS= read -r line
do
    if [[ "$line" == *_+([0-9])_+([0-9])_+([0-9])_+([0-9])\.mp4 ]]
    then
        echo "MATCH: $line"
    else
        echo "no match: $line"
    fi
done < <(/bin/ls -c1)

请记住,在构建模式时使用的是通配符,而不是正则表达式。

这就是为什么我没有使用 [0-9]{2} 来匹配 2 个数字,{} 不会像在正则表达式中那样在 globbing 中那样做。


要使用正则表达式,请使用:

#!/bin/bash

while IFS= read -r line
do
    if [[ $(echo "$line" | grep -cE '*_[0-9]{2}_[0-9]{2}_[0-9]{4}_[0-9]{6}\.mp4') -ne 0 ]]
    then
        echo "MATCH: $line"
    else
        echo "no match: $line"
    fi
done < <(/bin/ls -c1)

这是一个更精确的匹配,因为您可以指定在每个 sub-pattern 中接受多少位数字。

在python中:

import os

os.chdir("folder_path")

data = {}
data = [[file[-22:], file] for file in os.listdir()]

output = {}
for pattern, filename in data:
    output.setdefault(pattern, []).append(filename)
print(output)

这将创建一个字典,将每个文件与相应的模式相关联。


输出:

{
    '_03_01_2021_092200.mp4': ['file1_03_01_2021_092200.mp4', 'file3_03_01_2021_092200.mp4', 'file2_03_01_2021_092200.mp4'], 
    '_01_01_2022_112230.mp4': ['file202_01_01_2022_112230.mp4', 'file201_01_01_2022_112230.mp4', 'file203_01_01_2022_112230.mp4'], 
    '_02_01_2021_002244.mp4': ['file1_02_01_2021_002244.mp4', 'file2_02_01_2021_002244.mp4', 'file3_02_01_2021_002244.mp4']
}

试试这个

首先对所有模式进行排序和uniq

find ./data -type f -name "*.mp4" | sed 's/^[^_]*//'|sort -u

或使用正则表达式

find ./data -type f -regextype sed -regex '.*_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{4\}_[0-9]\{6\}\.mp4$'| sed 's/^[^_]*//'|sort -u

然后通过 while 循环迭代模式以查找每个模式的文件

while read pattern
do
   # find and exec
   find ./data -type f -name "*$pattern" -exec mv {} /to/whatever/you/want/ \;
   #or find and xargs
   find ./data -type f -name "*$pattern" | xargs -I {} mv {} /to/whaterver/you/want/
done < <(find ./data -type f -name "*.mp4" | sed 's/^[^_]*//'|sort -u)

有多种方法可以解决此问题,包括编写 bash 脚本,但如果是我,我会选择快速简便的方法。使用 grep 并阅读:

PATTERN=_02_01_2021_002244.mp4
find . -name '*.mp4' | grep $PATTERN; while read -t 1 A; do echo $A; done

可能还有我没有想到的更好的方法,但这可以完成工作。