查找名称开头相同的文件
Find files which names start the same
我有一堆按以下模式命名的文件:
NAME_TYPE.viz
而且我有兴趣查找所有 "NAME" 有多个文件的文件。例如假设文件列表是
A_type1.viz
A_type2.viz
1_type1.viz
1A_grop.viz
1A_grop2.viz
我想要"A"和“1A”。
编辑:我好像没说清楚。
我不知道 NAMES 是什么,所以我无法搜索特定的模式。
我只对输出 NAME
的列表感兴趣,其中存在至少两个与模式 NAME*.viz
匹配的文件,而不是所有 NAME
这样的文件只有一个NAME_*.viz
.
提前致谢。
Edit2:感谢@hek2mgl,我找到了一个解决方案
find /my/path/ -type f -name '*_*.viz' | sed -r 's/([^_]+).*//' | sort | uniq -d
您可以使用 find
和 sed
:
find /path/to/files type f -name '*_*.viz' | sed -r 's/([^_]+).*//'
上面的命令将递归地查找文件,这意味着它还会在 /path/to/files
的子文件夹中查找文件。如果您不希望将参数 -maxdepth 1
传递给 find
.
find
-唯一的解决方案,它在 /your/path/
中找到所有 .viz
以 NAME_
:
开头的文件
find /your/path/ -type f -name "NAME_*.viz"
使用正则表达式:
find /your/path/ -type f -regex ".*/NAME_.*.viz"
因此,如果您想查找所有以 A_
或 1A_
开头的 .viz
文件:
find /your/path/ -type f -regex ".*/1?A_.*.viz"
你的问题很复杂,我会用python。我测试了这个,它应该适合你:
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import re
names = set({})
for root, dirs, files in os.walk("/your/path"):
if len(files) > 0:
for f in files:
m = re.search('^([^_]+)_.*$', f)
if m:
names.add(m.group(1))
for name in names:
print(name)
将代码放在 foo.py
和 运行 python foo.py
中,您的系统应该安装了 python 解释器,大部分都安装了 :D
我有一堆按以下模式命名的文件:
NAME_TYPE.viz
而且我有兴趣查找所有 "NAME" 有多个文件的文件。例如假设文件列表是
A_type1.viz
A_type2.viz
1_type1.viz
1A_grop.viz
1A_grop2.viz
我想要"A"和“1A”。
编辑:我好像没说清楚。
我不知道 NAMES 是什么,所以我无法搜索特定的模式。
我只对输出
NAME
的列表感兴趣,其中存在至少两个与模式NAME*.viz
匹配的文件,而不是所有NAME
这样的文件只有一个NAME_*.viz
.
提前致谢。
Edit2:感谢@hek2mgl,我找到了一个解决方案
find /my/path/ -type f -name '*_*.viz' | sed -r 's/([^_]+).*//' | sort | uniq -d
您可以使用 find
和 sed
:
find /path/to/files type f -name '*_*.viz' | sed -r 's/([^_]+).*//'
上面的命令将递归地查找文件,这意味着它还会在 /path/to/files
的子文件夹中查找文件。如果您不希望将参数 -maxdepth 1
传递给 find
.
find
-唯一的解决方案,它在 /your/path/
中找到所有 .viz
以 NAME_
:
find /your/path/ -type f -name "NAME_*.viz"
使用正则表达式:
find /your/path/ -type f -regex ".*/NAME_.*.viz"
因此,如果您想查找所有以 A_
或 1A_
开头的 .viz
文件:
find /your/path/ -type f -regex ".*/1?A_.*.viz"
你的问题很复杂,我会用python。我测试了这个,它应该适合你:
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import re
names = set({})
for root, dirs, files in os.walk("/your/path"):
if len(files) > 0:
for f in files:
m = re.search('^([^_]+)_.*$', f)
if m:
names.add(m.group(1))
for name in names:
print(name)
将代码放在 foo.py
和 运行 python foo.py
中,您的系统应该安装了 python 解释器,大部分都安装了 :D