0

I need to rename a file, but keep the extension (whatever it might be, without specifying it)

For example:

file_20161118.csv.ovr needs to be renamed to "file.csv.ovr".

However in some instances it might be file_20161118.csv.rpl, in which case I need it to be "file.csv.rpl".

Thanks

1
  • 1
    mv just renames one file, or moves one or more files to a different location. It has no file name mangling support. It would also help if you can specify if you need a solution for HPUX, AIX, FreeBSD, OpenBSD, Mac OS X, Illumos, or any of the dozens of other UNIX variants and Unix-like OSes out there. – user Nov 18 '16 at 13:30
1

renamedoes the trick.

For example, a file called file_20161118.csv.ovr becomes file.csv.ovr after this command:

rename 's/file_20161118/file/' file_20161118*

It uses Perl regular expressions, so you can do a lot of things with it. Check more examples here.

0

You might want:

perl -e'for(@ARGV){ $f=$_; s/_(\d{8})\./\./; rename$f,$_}' file1 file2 files*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.