Using wildcards with rename

I used the rename command to batch rename files. So far I have had files like:

 2010.306.18.08.11.0000.BO.ADM..BHZ.SAC 2010.306.18.08.11.0000.BO.AMM..BHZ.SAC 2010.306.18.08.11.0000.BO.ASI..BHE.SAC 2010.306.18.08.11.0000.BO.ASI..BHZ.SAC 

and using rename 2010.306.18.08.11.0000.BO. "" * rename 2010.306.18.08.11.0000.BO. "" * and rename .. _. * rename .. _. * , I reduced them to:

 ADM_.BHZ.SAC AMM_.BHZ.SAC ASI_.BHE.SAC ASI_.BHZ.SAC 

what I want. Probably a little clumsy, but it works. The problem now occurs when I have files like:

 2010.306.18.06.12.8195.TW.MASB..BHE.SAC 2010.306.18.06.14.7695.TW.CHGB..BHN.SAC 2010.306.18.06.24.4195.TW.NNSB..BHZ.SAC 2010.306.18.06.25.0695.TW.SSLB..BHZ.SAC 

which exist in one folder. I am trying to get similar results above using wildcards in the rename command, for example. rename 2010.306.18.*.*.*.*. "" rename 2010.306.18.*.*.*.*. "" , but this adds a first appearance to 2010.306.18.*.*.*.*. to the beginning of all the other files is clearly not what I need, so I get:

 2010.306.18.06.12.8195.TW.MASB..BHE.SAC 2010.306.18.06.12.8195.TW.MASB..BHE.SAC2010.306.18.06.14.7695.TW.CHGB..BHN.SAC 2010.306.18.06.12.8195.TW.MASB..BHE.SAC2010.306.18.06.24.4195.TW.NNSB..BHZ.SAC 2010.306.18.06.12.8195.TW.MASB..BHE.SAC2010.306.18.06.25.0695.TW.SSLB..BHZ.SAC 

I assume that I do not understand the rather fundamental principle of wildcards here, can someone explain why this does not work and what I can do to get the desired result (preferably with rename ).


NB

To clarify, the output should be:

 ADM_.BHZ.SAC AMM_.BHZ.SAC ASI_.BHE.SAC ASI_.BHZ.SAC MASB.BHE.SAC CHGB.BHN.SAC NNSB.BHZ.SAC SSLB.BHZ.SAC 
+4
source share
2 answers

rename does not allow wildcards in the from and to lines. When you start rename 2010.306.18.*.*.*.*. "" * rename 2010.306.18.*.*.*.*. "" * , this is actually your shell, which first extends the wildcard and then passes the result of the extension to rename , so why this doesn't work.

Instead of using rename use a loop like this:

 for file in * do tmp="${file##2010*TW.}" # remove the file prefix mv "$file" "${tmp/../_}" # replace dots with underscore done 
+3
source
 for f in *; do mv $f `echo $f | sed 's/2010.*.TW.//'` ; done 

try first

 for f in *; do echo mv $f `echo $f | sed 's/2010.*.TW.//'` ; done 

to see which commands you are going to execute

+15
source

Source: https://habr.com/ru/post/1436360/


All Articles