Using Wildcards with 'rename'
Solution 1
rename
does not allow wildcards in the from
and to
strings. When you run rename 2010.306.18.*.*.*.*. "" *
it is actually your shell which first expands the wildcard and then passes the result of the expansion to rename
, hence why it does not work.
Instead of using rename
, use a loop as follows:
for file in *
do
tmp="${file##2010*TW.}" # remove the file prefix
mv "$file" "${tmp/../_}" # replace dots with underscore
done
Solution 2
You can try this first to see what commands would be executed
for f in *; do echo mv $f `echo $f | sed 's/2010.*.TW.//'` ; done
If it's what you expect, you can remove echo
from the command to execute
for f in *; do mv $f `echo $f | sed 's/2010.*.TW.//'` ; done
heds1
Not got a whole load of experience with programming languages, but I'm constantly discovering how to do clever things with code.
Updated on September 15, 2022Comments
-
heds1 over 1 year
I have been using the
rename
command to batch rename files. Up to now, 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. "" *
andrename .. _. *
I have reduced them to:ADM_.BHZ.SAC AMM_.BHZ.SAC ASI_.BHE.SAC ASI_.BHZ.SAC
which is exactly what I want. A bit clumsy, I guess, but it works. The problem occurs now that 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 the same folder. I have been trying to get the similar results to above using wildcards in the
rename
command eg.rename 2010.306.18.*.*.*.*. ""
but this appends the first appearance of2010.306.18.*.*.*.*.
to the beginning of all the other files - clearly not what I'm after, such that 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 guess I am not understanding a fairly fundamental principal of wildcards here so, can someone please explain why this doesn't work and what I can do to get the desired result (preferably using
rename
).
N.B.
To clarify, the output wants to 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