Utsav Utsav - 1 year ago 104
Linux Question

Unix: Rename file with dynamic extension

I am using RHEL 5.10 and I am trying to remove dynamic date extension from a bunch of files.

The file format is like

, where the last part
is dynamic. I want it to be renamed to

When I try to rename like below, it works

rename gz.20160704 gz filename01.gz.20160704


But when I try to rename like this it doesn't work

rename gz.* gz filename01.gz.20160704

I tried searching other answer and in some of them, they used rename like below, but sadly that is also not working for me.

rename 's/gz*/gz/' filename01.gz.20160704
rename 's/gz.*/gz/' filename01.gz.20160704

page for my linux says

For example, given the files foo1, ..., foo9, foo10, ..., foo278, the commands
rename foo foo0 foo?
rename foo foo0 foo??

Could someone please help me with it or is there any alternative approach I can use?

Update: Added the solution given by @PSkocik but it is not working either.

$> ls -ltr filename01*
-rw-rw----+ 1 foo foo 0 Jul 4 08:34 filename01.gz.20160704
$> rename -n 's/\.gz\.[^.]*/.gz/' filename01.gz.*
$> echo $?
$> ls -ltr filename01*
-rw-rw----+ 1 foo foo 0 Jul 4 08:34 filename01.gz.20160704

Answer Source
  1. with prename (perl-rename):

    rename -n 's/\.gz\.[^.]*/.gz/' filename01.gz.*   
  2. with mv and a POSIX shell:

    for f in *.gz.*; do mv "$f" "${f%.*}"; done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download