-1

I am creating a script which would enable me do something in a shorter amount of time. Is there a way that I can rename a file automatically from:

WDMyCloud_plexmediaserver_1.16.1.1291.bin(07082019)'

to

WDMyCloud_plexmediaserver_1.16.1.1291.bin

So it would detect the numbers, but would delete the part after .bin.

I am using Ubuntu.

Prvt_Yadav
  • 5,882
LaMpiR
  • 3

2 Answers2

0

Use mv:

file='WDMyCloud_plexmediaserver_1.16.1.1291.bin(07082019)'
mv "$file" "${file%.bin*}.bin"

Use prename or file-rename (in Debian/Ubuntu also known as just rename):

prename -n 's/\.bin.*$/.bin/' WDMyCloud_plexmediaserver_1.16.1.1291.bin\(07082019\)

Remove the -n to actually perform the rename.

pLumo
  • 22,565
  • Rename is not working it shows a proper new filename, but doesn't rename it in the end. Any chance of getting it right, because it would be the best solution? Version number of the filename is something that changes, so mv doesn't work. WDMyCloud_plexmediaserver is fixed. I basically just want to get rid of the last part which could work for any version number. Thank you! – LaMpiR Jul 08 '19 at 16:44
  • Sry, forgot to say. Remove the -n to actually perform the rename. – pLumo Jul 08 '19 at 16:53
  • Perfect. I've used: rename -n 's/.bin.$/.bin/' WDMyCloud_plexmediaserver_.bin(*) and it's working perfectly. Thank you! I now have to use only one script file. – LaMpiR Jul 08 '19 at 20:07
0

You can create script:

#!/bin/bash
for i
do
mv "$i" "${i%\(*}"
done

Now you can pass the filenames as arguments to this script provided this script is in same folder with all files.

e.g.

file 'WDMyCloud_plexmediaserver_1.16.1.1291.bin(07082019)' 'WDMyCloud_plexmediaserver_1.16.1.1292.bin(07082011)'

Will be renamed in the same folder as

WDMyCloud_plexmediaserver_1.16.1.1291.bin
WDMyCloud_plexmediaserver_1.16.1.1292.bin
Prvt_Yadav
  • 5,882
  • Solution worked, but had to create another script outside of my file to run it. Thank you. – LaMpiR Jul 08 '19 at 16:46
  • @LaMplR if you don't want to pass argument to file then replace that for loop with for i in WDMyCloud_plexmediaserver_*.bin(*). Passing arguments are helpful if you don't want to rename every file matching the pattern. – Prvt_Yadav Jul 10 '19 at 06:52