10

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.

Zanna
  • 72,471
guettli
  • 1,765
  • 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 09: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

2 Answers2

18

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
Zanna
  • 72,471
  • 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 06:31
9

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
heemayl
  • 94,145
  • 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 06: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 06:35
  • @guettli Also did you just downvoted my answer? :O – heemayl Jan 20 '17 at 06: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 06:40
  • @guettli It's fine. How about ${f#[0-9]*- }? Looking good on your eyes? – heemayl Jan 20 '17 at 06:42
  • at school I like math and physics, and disliked foreign languages. It's the same here. I know how to speak one language for this (perl compatible regular expressions). I have no interest in learning a different flavor. I know that other people feel different. They know the regexs which are used in gnu shell tools. Maybe they don't want to learn pcre. That's ok. In other word: I am lazy ... But I am willing to learn. Can you please show me the reference of this syntax? – guettli Jan 20 '17 at 06:48
  • 1
    @guettli Whatever suits you. Here: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html 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 06:52