-1

Have following files.

test.tar.gz.part00
test.tar.gz.part01
test.tar.gz.part02
…
test.tar.gz.part99

Need to replace .gz by .lz4… ideally without using dependencies (such as rename) that do not ship with Debian.

Thanks for helping out!

0

2 Answers 2

3

You can use rename for that.

rename gz lz4 ./*part*

Without rename:

for file in ./*part*; do mv "$file" "${file//gz/lz4}"; done

1
  • 1
    Your substitution works for the given names, but you're using // to replace all instances of the string gz in the names, even though you only expect one (and it would mess up the name if there were many gz substrings). Safer in the more general case to glob on *.gz.part* and then do two substitutions to first remove the .gz.part* bit from the end and then insert .lz4 followed the .part* bit from the original name. But if the names are simple as in the question, this does not matter.
    – Kusalananda
    Feb 27, 2022 at 14:33
3

rename started out as very short perl example script in the 80s.

It's so short you can easily inline it into any script:

perl -e 'for (@ARGV) {
           $old = $_; rename $old, $_ if s/\.gz(\.part\d+)\Z/\.lz4$1/s
         }' -- *.gz.part*

Besides perl, bash is another language interpreter that is usually shipped by default on system based on Debian GNU/Linux (as that's the GNU shell). In that shell, you could do:

shopt -s extglob nullglob
for file in *.gz.part+([0123456789]); do
  mv -T -- "$file" "${file%.*.*}.lz4.${file##*.}"
done

As an equivalent.

1
  • Interesting contribution to the question… thanks Stéphane!
    – sunknudsen
    Feb 27, 2022 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .