I'm going to suggest three alternatives. Each is a simple single line command, but I'll provide variants for more complicated cases, mainly in case the files to process are mixed with other files in the same directrory.
mmv
I'd use the mmv command
from the package of the same name:
mmv '*HBO_DPM*' '#1dpm#2'
Note that the arguments are passed as strings, so the glob expansion does not happen in the shell. The command receives exactly two arguments, and then finds corresponding files internally, without tight limits on the number of files. Also note that the command above assumes that all the files which match the first glob shall be renamed. Of course you are free to be more specific:
mmv 'sb_606_HBO_DPM_*' 'sb_606_dpm_#1'
If you have files outside the requested number range in the same directory, you might be better off with the loop over numbers given further down in this answer. However you could also use a sequence of mmv invocations with suitable patterns:
mmv 'sb_606_HBO_DPM_0089*' 'sb_606_dpm_0089#1' # 0089000-0089999
mmv 'sb_606_HBO_DPM_009*' 'sb_606_dpm_009#1' # 0090000-0099999
mmv 'sb_606_HBO_DPM_01[0-5]*' 'sb_606_dpm_01#1#2' # 0100000-0159999
mmv 'sb_606_HBO_DPM_016[0-2]*' 'sb_606_dpm_016#1#2' # 0160000-0162999
mmv 'sb_606_HBO_DPM_01630[01]?' 'sb_606_dpm_01630#1#2' # 0163000-0163019
mmv 'sb_606_HBO_DPM_016302[0-2]' 'sb_606_dpm_016302#1' # 0163020-0163022
loop over numbers
If you want to avoid installing anything, or need to select by number range avoiding matches outside this range, and you are prepared to wait for 74,023 command invocations, you could use a plain bash loop:
for i in {0089000..0163022}; do mv sb_606_HBO_DPM_$i sb_606_dpm_$i; done
This works particularly well here since there are no gaps in the sequence. Otherwise you might want to check whether the source file actually exists.
for i in {0089000..0163022}; do
test -e sb_606_HBO_DPM_$i && mv sb_606_HBO_DPM_$i sb_606_dpm_$i
done
Note that in contrast to for ((i=89000; i<=163022; ++i))
the brace expansion does handle leading zeros since some Bash release a couple of years ago. Actually a change I requested, so I'm happy to see use cases for it.
Further reading: Brace Expansion in the Bash info pages, particularly the part about {x..y[..incr]}
.
loop over files
Another option would be to loop over a suitable glob, instead of just looping over the integer range in question. Something like this:
for i in *HBO_DPM*; do mv "$i" "${i/HBO_DPM/dpm}"; done
Again this is one mv
invocation per file. And again the loop is over a long list of elements, but the whole list is not passed as an argument to a subprocess, but handled internally by bash, so the limit won't cause you problems.
Further reading: Shell Parameter Expansion in the Bash info pages, documenting ${parameter/pattern/string}
among others.
If you wanted to restrict the number range to the one you provided, you could add a check for that:
for i in sb_606_HBO_DPM_+([0-9]); do
if [[ "${i##*_*(0)}" -ge 89000 ]] && [[ "${i##*_*(0)}" -le 163022 ]]; then
mv "$i" "${i/HBO_DPM/dpm}"
fi
done
Here ${i##pattern}
removes the longest prefix matching pattern
from $i
. That longest prefix is defined as anything, then an underscore, then zero or more zeros. The latter is written as *(0)
which is an extended glob pattern that depends on the extglob
option being set. Removing leading zeros is important to treat the number as base 10 not base 8. The +([0-9])
in the loop argument is another extended glob, matching one or more digits, just in case you have files there that start the same but don't end in a number.
ARG_MAX
limit. As this question explicitly asks for a command-line solution, (possibly equal) GUI solutions as in the other question also don’t match. – dessert May 20 '18 at 11:54mv {1..2} {3..4}
does not work, which is a whole different problem fromARG_MAX
... Everyone else who answered will probably feel the same! So, from my point of view, I wish you would rollback your last edit and, if you want to, ask a whole new question aboutmv
ing with ranges – Zanna May 22 '18 at 10:16argument list too long
error. There is no need to clarify further, this is clearly not a dupe since we need a workaround for dealing with ARG_MAX and the answers in the proposed duplicate don't do that. – terdon May 22 '18 at 11:01