1

I've this Naruto anime downloaded on my PC. They are named like

NARUTO 50 — The Fifth Gate- A Splendid Ninja is Born_low.mp4

I want to simply batch rename them to something like this Naruto_50.

Is there an easy way to do it? I'm comfortable using the terminal.

Zanna
  • 70,465

4 Answers4

2

There is a simple, albeit tedious, way if you want to stick with a shell. You can make use of bash's parameter expansion.

  • String length modification:

    ${PARAMETER:OFFSET}: expand from position OFFSET.

    ${PARAMETER:OFFSET:LENGTH}: expand from position OFFSET to a final LENGTH.

    For example:

    $ test="This is a long string"
    $ echo $test
    This is a long string.
    $ echo ${test:0:4}
    This
    
  • Case modification:

    ${PARAMETER^}: first character uppercase.

    ${PARAMETER^^}: all uppercase.

    ${PARAMETER,}: first lowercase.

    ${PARAMETER,,}: all lowercase.

    $ test="This Is A Long String"
    $ echo ${test,}
    this Is A Long String.
    $ echo ${test,,}
    this is a long string.
    
  • Substring removal:

    ${PARAMETER#PATTERN}: remove a PATTERN, like bash understands, from the beginning of the string. Tries to find the shortest match.

    ${PARAMETER##PATTERN}: same, but longest match.

    ${PARAMETER%PATTERN}, ${PARAMETER%%PATTERN}: from the ending of the string.

    For example:

    $ test="This is a long, long string."
    $ echo ${test#*long}
    , long string.
    $ echo ${test##*long}
    string.
    $ echo ${test%long*}
    This is a long, 
    
  • Search and replace:

    ${PARAMETER/PATTERN/STRING}: replace the first occurrence of PATTERN with STRING.

    ${PARAMETER//PATTERN/STRING}: replace all the occurrences.

    ${PARAMETER/PATTERN}: remove the first occurrence.

    ${PARAMETER//PATTERN}: remove all.

    $ test="This is a long, long string."
    $ echo ${test/long/big}
    This is a big, long string.
    $ echo ${test//long/big}
    This is a big, big string.
    $ echo ${test//long}
    This is a , string. 
    

So, you could make a simple for loop and make changes progressively. For example:

#!/bin/bash
for i in *; do
    ext="${i##*.}" # get extension
    name="${i%% --*}" # get original name
    lname="${name,,}" # lowercase
    nsname="${lname// /_}" # all spaces to underscore
    mv "$i" "$nsname.$ext"
done
2

pyRenamer from the from the default Ubuntu repositories is a graphical renamer application that supports renaming files using patterns, including pattern expressions that are built-in in pyRenamer. Move the mouse over the Original file name pattern and Renamed file name pattern fields to show the list of pattern expressions that are available for that field. Click the Preview button to preview your changes before applying them. Click the Rename button to rename the files. This is the easiest way to batch rename a series of video files.

pyRenamer is available from the default Ubuntu repositories in Ubuntu 16.04 and earlier. In Ubuntu 18.04 and later pyRenamer has been replaced by GPRename or the Smart File Renamer snap package (sudo snap install smart-file-renamer).

enter image description here

For your example you would match the numbers of the videos in the series and the .mp4 extension at the end of the files' names and then replace with Naruto_ at the beginning of the files' names in the Renamed file name pattern field.

karel
  • 114,770
1

You could also use the perl-based rename command: something like

rename -- 's/(\w+)\s+(\w+).*/$1_$2.mp4/' *.mp4

which takes the first two "words" (sequences of word characters, separated by whitespace), discards the remainder of the name, and then outputs the words separated by an underscore followed by the .mp4 dot extension.

If you want to title-case the first word as indicated in your question, you could do that using \u and \L modifiers

rename -- 's/(\w+)\s+(\w+).*/\u\L$1_$2.mp4/' *.mp4

Testing using the -n (no-op) and -v (verbose) switches with your sample filename

$ rename -nv -- 's/(\w+)\s+(\w+).*/\u\L$1_$2.mp4/' *.mp4
NARUTO 50 — The Fifth Gate- A Splendid Ninja is Born_low.mp4 renamed as Naruto_50.mp4

You could get fancier - for example making it handle dot suffixes more generally rather than assuming they are all .mp4 - you may find this Regex Tutorial useful.

steeldriver
  • 136,215
  • 21
  • 243
  • 336
0

I recommend you Thunar that comes with Thunar Bulk Rename

  • Open a terminal

  • To install it, write and execute the command:

    • sudo apt install --no-install-recommends thunar

thunar-bulk-rename-1


In your case this could be the way (It's in Spanish)

First step: thunar-bulk-rename-2

Second step: thunar-bulk-rename-3

Sebastian
  • 403
  • 3
  • 8