Loop over the names and use variable substitutions to delete the unwanted bits. The following assumes that $HOME
is /home/anik
and that this is where your files are located (change topdir
to some other value if the files are located elsewhere):
#!/bin/sh
topdir=$HOME
for dirpath in "$topdir"/Zt.*.Spades; do
[ ! -d "$dirpath" ] && continue
newdirpath=$topdir/${dirpath#$topdir/Zt.}
newdirpath=${newdirpath%.Spades}
if [ -e "$newdirpath" ]; then
printf 'Can not rename "%s" into "%s", name taken\n' "$dirpath" "$newdirpath"
else
printf 'Renaming "%s" into "%s"\n' "$dirpath" "$newdirpath"
echo mv "$dirpath" "$newdirpath"
fi
done
The expression $topdir/${dirpath#$topdir/Zt.}
will be replaced by the directory's pathname, but without the Zt.
in the start of the directory filename. It does this by removing the prefix $topdir/Zt.
and then adding $topdir/
at the start again.
The expression ${newdirpath%.Spades}
expands to $newdirpath
but removes the string .Spades
from the end of the value.
I've added echo
in front of the mv
command to output the command that would be executed instead of executing it. Test the code and then remove the echo
if it looks correct.