Shell script to replace a part of filenames

I came across a situation I want to change a part of filenames.
I have many nvXXXX.nii files, and I want to change it to n1vXXXX.nii.
Below is the shell script I came up with.
The point is using sed.

#!/bin/bash
# A script to change a part of filenames.
for FILE in $*
do
  echo $FILE | sed -e "s/nv/n1v/" # This is optional. I just want to display the filenames on the monitor.
  mv $FILE `echo $FILE | sed -e "s/nv/n1v/"`
done

The below might be better if I want to use the new filenames as some variables.

#!/bin/bash
# A script to change a part of filenames.
for FILE in $*
do
  echo $FILE | sed -e "s/nv/n1v/"
  NEWFILE=`echo $FILE | sed -e "s/nv/n1v/"`
  mv $FILE $NEWFILE
done

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください