Strip prefix from a string using Bash

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


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.


  • [1] Bash string operations


Leave a Reply