Bash string manipulation Linux 09.01.2014

I have a set of files which I want to rename by special pattern. The set of files is in form like image{1..30}.jpg and I need all files in form image{1..30}.thumbnail.jpg.

Result script is

#! /bin/bash
for file in *.jpg; do
    parts=(${file//./ });
    mv $file $newfile

As bonus small collection of commands for string manipulation in bash

  • array=(${string//./ }) - split string to array by character (in this case, point);
  • ${#string} string length;
  • ${string:start:length} - extract a substring from start with length characters;
  • ${string/pattern/replace} - find and replace first match;
  • ${string//pattern/replace} - find and replace all matches;

Check if string contains substring, first alternative

string='lorem ipsum';
if [[ $string =~ ipsum ]]
  echo "It's there!";

Second alternative

case "$string" in
    *lorem* ) echo "I've found lorem";;
    *foo* ) echo "I've found foo";;
    * ) echo "I have not found any interesting ...";;