'To delete all the character from a filename before a matching keyword is found using shell script one liner

I have lots of files with below formats

2022-01-04_09-14-07_1003-pbx72900a45_0349821623_0333947635_0027333947635_cloud.mp3
2022-01-04_09-03-18__27829201595_0027349821623__cloud.-1641279798.89196.mp3

I want to remove all the character before the word cloud for e.g

cloud.mp3
cloud.-1641279798.89196.mp3


Solution 1:[1]

You could use shell parameter expansion to remove the longest string from the start that matches *cloud and then prepend "cloud" back to it:

for file in ./*cloud*.mp3
do
  mv -- "$file" cloud"${file#*cloud}"
done

You can adjust the wildcard pattern ./*cloud*.mp3 as needed. This doesn't check for clobbering of filenames.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1