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

script <oldName> <newName>

Code snippet:

#! /usr/bin/bash
if [ $# -lt 2 ]; then
        echo "useage: script <oldName> <newName>"
        exit 1
fi
oldName=$1
newName=$2
var=$(ls)
beforeList=($var) # to array
len=${#beforeList[@]} # array length
afterList=($(ls |sed s/${oldName}/${newName}/g))
#echo ${afterList[@]}

i=0
while [ $i -lt $len ]; do
        echo ${beforeList[$i]}" --> "${afterList[$i]}
        mv ${beforeList[$i]} ${afterList[$i]} 2>/dev/null
        i=$(expr $i + 1)
done

Result:

Desktop --> Desktop
Documents --> Documents
Downloads --> Downloads
file2.sh --> file2.sh
file.sh --> file.sh
Music --> Music
Pictures --> Pictures
Public --> Public
te2.txt --> mmp2.txt
te3.txt --> mmp3.txt
Templammps --> Templammps
te.txt --> mmp.txt
Videos --> Videos
Categories: MacTool

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