Ubuntu – Mass renaming files with special format

batch-rename

I want to rename several hundred files whose names are formatted as follows:

A Study in Scarlet - Arthur Conan Doyle.mobi
Anvil of Stars - Greg Bear.mobi
City and the Stars, The - Arthur C. Clarke.mobi

After renaming I want to reach this naming scheme:

Arthur Conan Doyle - A Study in Scarlett.mobi
Greg Bear - Avil of Stars.mobi
Arthur C. Clarke - City and the Stars, The.mobi

A bonus would be to remove all irregular characters from the name, as there are:

  1. _ [underscores] to replace by blank
  2. %20 to replace by blank
  3. [] to replace with rounded ones ()

Best Answer

I wrote a small bash script to do the job.

#!/bin/bash

# Variables
extension='mobi'
report='report.log'    

if [ -f $report ];
then
    rm -rf $report
fi

echo $'renaming files . . .\n'

for filename in *.$extension
do
    temp=$(echo $filename | tr '_' ' ' | tr '%20' ' ' | tr '[]' '()' | tr -s ' ' | sed 's/\.[^.]*$//' )

    part1=$(echo $temp | cut -f1 -d-)
    part2=$(echo $temp | cut -f2 -d-)
    new_filename=$(echo "${part2#?} - ${part1%?}.$extension")
    echo $(mv -v "$filename" "$new_filename") | tee -a $report
    if [ $? -ne 0 ]; then
        echo $'\n\nScript FAILED'
        exit 1
    fi
done

echo $'\n\nScript SUCCESSFUL'
exit 0

Create a .bsh file inside the directory in which your .mobi files are stored and paste the above code :

Open a Terminal with Ctrl + Alt + T and navigate to the directory in which your script is stored :

cd /path/to/directory/

To change the permissions of the script :

chmod +x <filename>.bsh

To execute the script :

bash <filename>.bsh

If for some reason the mv fails you will receive an error 'Script FAILED', otherwise you'll get 'Script SUCCESSFUL'.


Explanation

tr '_' ' ' replace underscores with whitespace

tr '%20' ' ' replace %20 with whitespace

tr '[]' '()' replace square brackets with parentheses

tr -s ' ' replace multiple spaces with one

sed 's/\.[^.]*$//' extracts only the name of the file without the extension

${string#?} remove first character of a string

${string%?} remove last character of a string

mv -v this will show in the terminal which files are being moved. -v stands for verbose.

tee -a changelog.log By default tee command reads from standard input, and writes to standard output and files. -a stands for append.

if [ $? -ne 0 ] checks if the previous command was successful.

report.log stores the previous and latest name of each file.


Note

The script assumes that the filename contains only one dash -