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.

2

2 Answers

You could use rename...

rename -n 's/^[0-9]+ - //' * 

Remove -n after testing to actually rename the files

Explanation

  • s/old/new/ replace old with new
  • ^ start of string
  • [0-9]+ some numbers
3

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy