1

I have multiple files in a folder and files name format like below

12.xyz.dat.cache
13.abc.dat.cache

I have to rename above files like below

12.xyz.dat
13.abc.dat

Basically I have to remove tailing '.cache' Can you provide the command.

I tried below, did not work.

rename 's/\.dat\.cache$/\.dat' *.dat.cache

Thanks, Mahdu

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Madhuprathap
  • 209
  • 1
  • 3
  • 15
  • 4
    Possible duplicate of [Removing part of a filename for multiple files on Linux](https://stackoverflow.com/q/12174947/608639) – jww Dec 15 '18 at 15:33

2 Answers2

1

This is because you missed the trailing slash '/' in the perlexp. And to be sure to catch a '.dat' file, you should escape the .:

rename 's/\.dat.cache$/\.dat/' *.dat.cache

Try this one.

Tobi
  • 414
  • 3
  • 9
  • Did not work. Tried rename 's/\.dat.cache$/\.dat/' *.dat.cache and rename 's/\.dat\.cache$/\.dat/' *.dat.cache – Madhuprathap Dec 13 '18 at 08:06
  • @Madhuprathap, the answer is working. Maybe you are missing something else. Weird whitespace chars at the start/end of the file names? – Melih Taşdizen Dec 13 '18 at 08:28
0

If you don't have or don't like rename, you can use this following alternative for remove all trailing .cache :

find . -name '*.cache' | while read NAME; do mv "${NAME}" "${NAME%.cache}"; done
iElden
  • 1,272
  • 1
  • 13
  • 26
  • 1
    Another suggestion following the same idea: ==> for file in *.cache ; do mv "${file}" "${file%.cache}" ; done It saves a call to find... – binarym Dec 13 '18 at 13:27