With bash
or ksh
, assuming the files are in the current directory:
for name in ./"Nora Hmann - sub-"??.zip; do
newname="./${name##* }"
mv -i "$name" "$newname"
done
The parameter expansion ${name##* }
will remove everything from $name
up until (and including) the last space character.
If the name at the start of the filename is not fixed, then the following will grab all Zip files and try to rename them in the same manner:
for name in ./*.zip; do
newname="./${name##* }"
mv -i "$name" "$newname"
done
I've opted for using mv -i
in these examples so that you interactively have to confirm any renaming that would overwrite an already existing file.
Another way to do it would be to simply skip files that would otherwise overwrite existing files:
for name in ./*.zip; do
newname="./${name##* }"
if [[ ! -e "$newname" ]]; then
mv "$name" "$newname"
else
printf 'Not renaming "%s", new filename exists\n' "$name" >&2
fi
done
sed
. Please edit your question and show us the command you were using. Also, please clarify if all your lines will containNora Hmann -
. Would it be enough to remove that specific string? Can we always remove everything before the first-
? What will change on the different lines?