Utsav
Utsav

Reputation: 8093

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 filename01.gz.20160704, where the last part date is dynamic. I want it to be renamed to filename01.gz

When I try to rename like below, it works

rename   gz.20160704   gz   filename01.gz.20160704

Output: filename01.gz

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  

The man 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 $?
0
$> ls -ltr filename01*
-rw-rw----+ 1 foo foo 0 Jul  4 08:34 filename01.gz.20160704

Upvotes: 1

Views: 176

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60056

  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
    

Upvotes: 1

Related Questions