简体   繁体   English

如何在具有特定编号的目录中查找具有特定模式的文件? 的Linux

[英]How to find files with specific pattern in directory with specific number? Linux

I've got folders named folder1 all the way up to folder150 and maybe beyond.. but I only want to find the complete path to text files in some of the folders (for example folder1 to folder50). 我有一个名为folder1的文件夹,一直到folder150甚至可能更远..但我只想在某些文件夹中找到文本文件的完整路径(例如,从folder1到folder50)。

I thought a command like the following might work, but it is incorrect. 我认为以下命令可能有效,但这是不正确的。

find '/path/to/directory/folder{1..50}' -name '*.txt'

The solution doesn't have to use find, as long as it does the correct thing. 只要解决方案正确,它就不必使用find。

find /path/to/directory/folder{1..50} -name '*.txt'  2>/dev/null 

Or only basename 或仅基名

find /path/to/directory/folder{1..50} -name '*.txt' -exec basename {} \; 2>/dev/null

Or basename without .txt 或没有.txt的基本名称

 find /path/to/directory/folder{1..50} -name '*.txt' -exec basename {} .txt \; 2>/dev/null

V. Michel's answer directly solves your problem; V. Michel的答案直接解决了您的问题; to complement it with an explanation : 补充说明

Bash's brace expansion is only applied to unquoted strings; Bash的大括号扩展名仅适用于未加引号的字符串。 your solution attempt uses a single-quoted string, whose contents are by definition interpreted as literals . 您的解决方案尝试使用单引号字符串,其内容根据定义被解释为文字

Contrast the following two statements: 对比以下两个语句:

# WRONG:
# {...} inside a single-quoted (or double-quoted) string: interpreted as *literal*.
echo 'folder{1..3}' # -> 'folder{1..3}'

# OK:
# Unquoted use of {...} -> *brace expansion* is applied.
echo 'folder'{1..3} # -> 'folder1 folder2 folder 3'

Note how only the brace expression is left unquoted in the 2nd example above, which demonstrates that you can selectively mix quoted and unquoted substrings in Bash. 请注意,在上面的第二个示例中,仅大括号表达式不加引号,这表明您可以在Bash中有选择地混合带引号和未引号的子字符串。


It is worth noting that it is - and can only be - Bash that performs brace expansion here, and find only sees the resulting, literal paths. 值得注意的是,它是-并且只能是-在这里执行括号扩展的Bash ,并且find仅看到生成的文字路径。 [1] [1]

find only accepts literal paths as filename operands. find仅接受文字路径作为文件名操作数。
(Some of find 's primaries (tests), such as -name and -path , do support globs (as demonstrated in the question), but not brace expansion; to ensure that such globs are passed through intact to find , without premature expansion by Bash, they must be quoted ; eg, -name '*.txt' ) (某些find的原语(测试),例如-name-path ,确实支持glob (如问题所示),但不支持大括号扩展;以确保此类glob完整地传递给find ,而不会过早扩展由Bash加上引号 ;例如-name '*.txt'


[1] After Bash performs brace expansion, globbing (pathname expansion) may occur in addition , as demonstrated in ehaymore's answer ; [1]击执行括号扩展, 通配 (路径扩展)后可能发生, 此外 ,如在证实ehaymore的回答 ; folder(?,[1-4]?,50) is brace-expanded to tokens folder? folder(?,[1-4]?,50)大括号扩展到令牌folder? , folder[1-4]? folder[1-4]? , and folder50 , the first two of which are subject to globbing, due to containing pattern metacharacters ( ? , [...] ). folder50 ,由于其中包含模式元字符( ?[...] ),因此其中的前两个会受到folder50 Whether globbing is involved or not, the target program ultimately only sees the resulting literal paths. 无论是否涉及遍历,目标程序最终只会看到生成的文字路径。

You can give multiple directories to the find command, each matching part of the pattern you're looking for. 您可以为find命令提供多个目录,每个目录都与您要查找的模式匹配。 For example, 例如,

find /path/to/directory/folder{?,[1-4]?,50} -name '*.txt'

which expands to three patterns: 扩展为三种模式:

  • folder? 夹? (matches 0-9) (匹配0-9)
  • folder[1-4]? 文件夹[1-4]? (matches 10-49) (匹配10-49)
  • folder50 资料夹50

The question mark is a single-character wildcard. 问号是一个单字符通配符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM