0

I need to rename files like

filename-367519.mp4 
otherfilename-367515.mp4
andotherfilename-377530.mp4

to

367519-filename.mp4
...
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

1

I wrote a simple script for you.

#!/bin/bash

full_filename=$(basename $1)
filename_noextension="${full_filename%%.*}"

filename=$(echo "$filename_noextension" | cut -d"-" -f1);
number=$(echo "$filename_noextension" | cut -d"-" -f2);
extension="${full_filename##*.}"

result="$number-$filename.$extension"

mv $1 $result

run it with: bash ./rename.sh filename-367519.mp4

Note that command cut use sign - as delimiter, and later it is used in result="$number-$filename.$extension" as just a string.

Reference: http://wiki.bash-hackers.org/syntax/pe#substring_removal

Karol Flis
  • 311
  • 2
  • 12
  • I got to much space in all file name so i do a loop with a script to replace white space with '_' FILES=* for f in $FILES do mv "$f" `echo $f | tr ' ' '_'` done than with same loop I use you code Thank so much. – Hamza Benes Jan 31 '18 at 12:05
  • If that solved your problem, please mark the question as an answered. More points for me then, yay! – Karol Flis Jan 31 '18 at 16:06