String manipulation tricks in bash

  • In some cases we have to modify the strings in a bash variable. Lets say : There are many files inside $(pwd)  with name as  a_ver10, b_ver10, c_ver10. We have to modify them back to a,b,c respectively. This below bash one liner can help to achieve this.
  • Inside the directory run this below command.


for file in $(ls -1d *_ver10); do mv $file  ${file%_ver10}; done

Moving some files into another dir or deleting them(change mv to rm).

for file in $(ls -1d *_ver11); do mv ${file%_ver11} bkp/ ;done


Leave a Reply