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:
_
[underscores] to replace by blank%20
to replace by blank[]
to replace with rounded ones()
Best Answer
I wrote a small bash script to do the job.
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 :
To change the permissions of the script :
To execute the script :
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 whitespacetr '%20' ' '
replace%20
with whitespacetr '[]' '()'
replace square brackets with parenthesestr -s ' '
replace multiple spaces with onesed 's/\.[^.]*$//'
extracts only the name of the file without the extension${string#?}
remove first character of a string${string%?}
remove last character of a stringmv -v
this will show in the terminal which files are being moved.-v
stands for verbose.tee -a changelog.log
By defaulttee
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
-