在bash脚本中,我想复制一个文件,但文件名会随着时间而改变。 然而,文件名的开头和结尾将保持不变。
有没有办法让我得到这样的文件:
cp start~end.jar
哪里〜可以是什么?
cp命令将在ubuntu机器上运行一个bash脚本,如果这样做有所不同。
答案 0 :(得分:4)
glob(start*end
)将为您提供所有匹配的文件。
查看bash手册的Expansion > Pathname Expansion > Pattern Matching
部分以获取更具体的控制
* Matches any string, including the null string.
? Matches any single character.
[...] Matches any one of the enclosed characters. A pair of characters separated by a hyphen denotes a range expression; any character that sorts between those two characters, inclusive, using the current locale's collat-
ing sequence and character set, is matched. If the first character following the [ is a ! or a ^ then any character not enclosed is matched. The sorting order of characters in range expressions is determined by
the current locale and the value of the LC_COLLATE shell variable, if set. A - may be matched by including it as the first or last character in the set. A ] may be matched by including it as the first character in
the set.
如果您启用extglob
:
?(pattern-list)
Matches zero or one occurrence of the given patterns
*(pattern-list)
Matches zero or more occurrences of the given patterns
+(pattern-list)
Matches one or more occurrences of the given patterns
@(pattern-list)
Matches one of the given patterns
!(pattern-list)
Matches anything except one of the given patterns
答案 1 :(得分:2)
使用glob
捕获变量文本:
cp start*end.jar