Bash Shell: Rename Files – Prepend and Append

Pre-pending

# Prepend any file that doesn't have the word "/thumbs_" in its full path
regex="\/thumbs_"
for file in */thumbs/*.jpg ; do
    if [[ "$file"  =~ ${regex} ]]
    then
      echo "skipping: $file"
    else      
      mv "$file" "$(dirname "$file")/thumbs_$(basename "$file")"
    fi
done

Appending

# Append any file that doesn't have the word ".bak" in its full path
regex=".bak"
for file in */thumbs/*.jpg ; do
    if [[ "$file"  =~ ${regex} ]]
    then
      echo "skipping: $file"
    else      
      mv "$file" "$(dirname "$file")/$(basename "$file").bak"
    fi
done
# Appending file name right before extension
keyword=dragon
for file in *.jpg; do
    echo mv -- "$file" "${file%.jpg}_$keyword.jpg"
done

Leave a Reply

Your email address will not be published. Required fields are marked *