linuxbashslackware

How to rename all files in a directory in linux with string substitution?


i want to rename multiple files in my linux system directory....

my file names are as:

Lec 1 - xxx.webm
Lec 2 - xxx.webm
Lec 3 - xxx.webm
Lec 4 - xxx.webm

and the list goes on...

here xxx could be any list of characters(not consistent)....

i would like to rename every file in here like:

mv Lec 1 - xxx.webm Lec 1.webm
mv Lec 2 - xxx.webm Lec 2.webm
mv Lec 3 - xxx.webm Lec 3.webm

and so on....

for in loop could do but how to do the substitution?

*strip all characters after the number should be my renamed file


Solution

  • This for loop should do the job:

    for f in *.webm; do
       mv "$f" "${f/ -*/}.webm"
    done