I have this directory, and want to rename the files:
===> ls -1 Der-vierte-Weise/ 10 - 10 - Erkenntniss.ogg 1 - 01 - Hoffnung.ogg 11 - 11 - Wahrheit.ogg 12 - 12 - Dankbarkeit.ogg 13 - 13 - Kredits.ogg 2 - 02 - Familie.ogg 3 - 03 - Demut.ogg 4 - 04 - Freude.ogg 5 - 05 - Schoenheit.ogg 6 - 06 - Solidaritaet.ogg 7 - 07 - Entscheidung.ogg 8 - 08 - Hilfe.ogg 9 - 09 - Unterstuetzung.ogg The result should look like this:
===> ls -1 Der-vierte-Weise/ 01 - Hoffnung.ogg 02 - Familie.ogg ... I would like to solve this with common shell tools.
22 Answers
You could use rename...
rename -n 's/^[0-9]+ - //' * Remove -n after testing to actually rename the files
Explanation
s/old/new/replaceoldwithnew^start of string[0-9]+some numbers
bash parameter expansion to strip off the required portion from start:
for f in *[[:blank:]]*.ogg; do echo mv -i -- "$f" "${f#[[:digit:]]*-[[:blank:]]}"; done Expanded form:
for f in *[[:blank:]]*.ogg; do echo mv -i -- "$f" "${f#[[:digit:]]*-[[:blank:]]}" done echo is for dry-running; get rid of it for actual action:
for f in *[[:blank:]]*.ogg; do mv -i -- "$f" "${f#[[:digit:]]*-[[:blank:]]}"; done Example:
% ls -1 1 - 01 - Hoffnung.ogg 2 - 02 - Familie.ogg % for f in *[[:blank:]]*.ogg; do mv -i -- "$f" "${f#[[:digit:]]*-[[:blank:]]}"; done % ls -1 01 - Hoffnung.ogg 02 - Familie.ogg 7