Sometimes, you can extract only a part of a text segment by using shell's greedy match. % is a match from the right to the left, and # Is a match from the left to the right, as shown in the following example:
[Root @ WWW ~] #Qq = www.qq.com
[Root @ WWW ~] #Echo $ {QQ #*.}// Exclude the first
QQ.com
[Root @ WWW ~] #Echo $ {QQ ##*.}// Exclude the first two
Com
[Root @ WWW ~] #Echo $ {QQ % .*}// Exclude the last one
Www. qq
[Root @ WWW ~] #Echo $ {QQ % .*}// Exclude the last two
WWW
[Root @ WWW ~] #
The above is a simple example. Let's take a look at the usage in a specific script.
#Vim test. Sh
#Chmod + X test. Sh
#CAT test. Sh
#! /Bin/bashnum = 1for I in *. tar * .tar.gz donew = new _ $ num. $ {I #*.} MV $ I $ new 2>/dev/nullif [$? -EQ 0]; then Echo "remove $ I to $ new" Let num ++ fi done
#Touch test2.161..52.16.tar test2.161..52.16.tar.gz
#Ls
Test1.tar test2.tar test3.tar test4.tar test5.tar test. Sh
Test1.tar.gz test2.tar.gz test3.tar.gz test4.tar.gz test5.tar.gz
#./Test. Sh
Remove test1.tar to new_1.tar
Remove test2.tar to new_2.tar
Remove test3.tar to new_3.tar
Remove test4.tar to new_4.tar
Remove test5.tar to new_5.tar
Remove test1.tar.gz to new_6.tar.gz
Remove test2.tar.gz to new_7.tar.gz
Remove test3.tar.gz to new_8.tar.gz
Remove test4.tar.gz to new_9.tar.gz
Remove test5.tar.gz to new_10.tar.gz
#