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.

  • 1
    I would like to solve this with common shell tools. I'm not saying that you don't have a good reason, but I trust you've asked and answered a question to yourself "why?". Last time I had a very similar problem to solve I immediately shoved Bash to one side and solved it in Python. So much easier (but I did already know Python, string methods, glob.glob and os.rename -- and I also had to recurse down a tree full of foldernames with spaces and other awkward characters, none of which are a cause of extra bother if using Python)
    – nigel222
    Jan 19 '17 at 9:56
  • 1
    @nigel222 I am lazy and I want to type as less characters as possible. That's why I don't want to use python here. Don't get me wrong. Up to now Python is my preferred programming language and use it daily.
    – guettli
    Jan 19 '17 at 12:41

You could use rename...

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

Remove -n after testing to actually rename the files


  • s/old/new/ replace old with new
  • ^ start of string
  • [0-9]+ some numbers
  • what kind of regex flower is supported by the rename tool. Does it use PCRE? ... would be great.
    – guettli
    Jan 19 '17 at 12:42
  • 5
    @guettli strictly speaking, no it isn't PCRE (that's Perl Compatible Regular Expressions). The rename is a Perl script so it has full Perl regular expression support; PCRE is only a subset of that. However, anything you know from PCRE will be applicable to rename.
    – terdon
    Jan 19 '17 at 17:40
  • @terdon I don't care for the implementation. If you look at it with math-glasses, then rename has perl compatible regular expressions. Yes, the pcre implementation does not get used. I understood this :-) Thank you for your fast reply.
    – guettli
    Jan 20 '17 at 6:31

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:]]}"

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


% 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
  • Yes, works. Thank you. Since I know perl compatible regular expression from Python (which I use daily), I prefer the rename command. (Less typing needed).
    – guettli
    Jan 20 '17 at 6:33
  • 1
    @guettli rename is the way to go if you are familiar with PCRE; the rename answer was already given so have to move with pure bash. Just to note, python's re (or regex) module is not fully PCRE compatible BTW.
    – heemayl
    Jan 20 '17 at 6:35
  • @guettli Also did you just downvoted my answer? :O
    – heemayl
    Jan 20 '17 at 6:36
  • Yes I did, and I upvoted your comment. I hope you don't take it personally. Don't get me wrong, but my personal opinion is that this ${f#[[:digit:]]*-[[:blank:]]} is ugly. Yes it works. But we live in the 21century. I think it is time to switch from "it works" to "it's easy and fun". If "it works" is the goal, then linux will never loose the prejudice of not being user friendly. Since you already got a lot of up-votes, this one down-vote should not hurt (I hope). If it does, tell me, and I will take it back.
    – guettli
    Jan 20 '17 at 6:40
  • 1
    @guettli Whatever suits you. Here: gnu.org/software/bash/manual/html_node/… Just to note, the character class [[:digit:]] and [[:blank:]] were there to comply with your locale, these are pretty common in Regex too.
    – heemayl
    Jan 20 '17 at 6:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.