2013-03-06 63 views
0

在bash腳本中,我想複製一個文件,但文件名將隨時間而改變。 但是文件名的開始和結束將保持不變。bash複製文件其中一些文件名未知

有沒有辦法讓我得到像這樣的文件:

cp start~end.jar 

的地方〜可以是任何東西?

cp命令會在ubuntu機器上運行一個bash腳本,如果這樣做和區別的話。

回答

4

一個水珠(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 
+0

一個忠告位;儘量使模式儘可能具體,以避免比預期更多的文件匹配。例如,如果您知道中間部分是YYYY-MM-DD格式的時間戳,則可以將'start - ???? - ?? - end.jar'設置爲'start * end.jar'。甚至更好,開始 - @(19 | 20)[0-9] [0-9] - @([1-9] | 10 | 11 | 12) - @([1-9] | 1 [1 -9] | 2 [1-9] | 3 [01] -end.jar'。 – chepner 2013-03-06 16:55:17

2

使用glob捕捉可變文本:

cp start*end.jar