Strip prefix from a string using Bash

Videos recorded using the OnlineTvRecorder sometimes have a prefix consisting of a five-digit number, for instance:

71072_Vertrauter_Feind_13.06.19_20-15_kabel1_140_TVOON_DE.mpg.HQ.cut.mp4

Of course, these files fail to sort into the list of unprefixed videos and we cannot search a movie by its initial letters.In this situation, the Bash helps us very nicely with its build-in string manipulation functions. The following snippet takes the filename and removes any sequence of digits followed by an underscore (as regex: [0-9]*_) from the beginning of the filename:

We can pack this into a loop to rename a batch of downloaded files:

The guard expression avoids warnings by mv if the old and new filename equal.

 References

  • [1] Bash string operations

 

Leave a Reply