The following shell script is used to rename the files suffix at the current directory. After giving it the executable permission, we can use it like

script <oldSuffix> <newSuffix>

Code snippet:

#! /usr/bin/bash
set -e
if [ $# -lt 2 ]; then
    echo "useage: script <oldSuffix> <newSuffix>"
    exit 1
fi
oldSuffix=$1
newSuffix=$2
cmd1=$(ls)
cmd2=$(ls |sed s/${oldSuffix}/${newSuffix}/g)

array1=()
len1=0
for i in $cmd1; do
  array1[$len1]=$i
  len1=$(expr $len1 + 1)
done

array2=()
len2=0
for i in $cmd2; do
  array2[$len2]=$i
  len2=$(expr $len2 + 1)
done

for(( i=0;i<$len1;i++)); do
  if [ "${array1[$i]}" != "${array2[$i]}" ]; then
        echo ${array1[$i]}" ==> "${array2[$i]}
        mv ${array1[$i]} ${array2[$i]}
    fi
done

Result:

Categories: ShellTool

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

File Charset Converter
: help you to convert character encoding of file.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x