Note: this wiki is no longer maintained. If you have any questions related to this wiki, please post them on the CrunchBang forums.

Translations of this page?:

ADMIN Batch rename files in terminal

Whenever I convert from one file format to another (for example, flac → ogg), I use find and ffmpeg. However, that leaves me with files like song_1.flac.ogg which is really annoying. Since my music library is distributed among multiple folders, it would have been annoying to use Thunar's Bulk Renamer. That's why I put together the following script:

find -name *.$1 | while read f
    mv ./"$f" "${f%$1}$2"
echo "Successful!"

(Yes, the last line is completely extraneous and completely meaningless in the context of error messages ;-)) The script takes two arguments - the first is the extension you want to remove and the second is the extension you want to replace it with. For example: To rename a bunch of files like song_1.flac.ogg, I'd call the script like this: flac.ogg ogg

Notice that I did not include a leading dot for either the first or second arguments.

That's it! Enjoy! :-D


The shell may try to interpret the asterisk in find's -name option, so it is safer to enclose it in quotes. It's possible to avoid piping the output of find to read, by using the -exec option. (usage same as above)

find -name "*.$1" -exec bash -c 'mv "$1" "${1%$2}$3"' _ '{}' "$1" "$2" \;

NB The $1 $2 and $3 inside the miniscript 'mv “$1” “${1%$2}$3”' are independent from the script's $1 and $2. See Using Find for more details, and much general advice about find.

howto/batch_rename_files_in_terminal.txt · Last modified: 2012/06/05 06:17 by johnraff
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported

Powered by DokuWiki. Hosted by Linode.
Copyright © 2010 CrunchBang Linux.
Proudly powered by Debian GNU/Linux.
Debian is a registered trademark of Software in the Public Interest, Inc.