複数のファイル名から特定の文字の後の文字列を削除するには?

複数のファイル名から特定の文字の後の文字列を削除するには?

私のフォルダには、この名前の形式のファイルが複数あります。

01. file one-sdvanv-12lknl.srt
01. file one-sdvanv-12lknl.mp4
02. file two-afdsmakl-asdfafdaf.srt
02. file two-afdsmakl-asdfafdaf.mp4
03. file three-adfadaasd-asdfadfafad-adad1d1das.srt
03. file three-adfadaasd-asdfadfafad-adad1d1das.mp4

-これで、ファイル名が次のように見えるように次の文字列を削除するにはどうすればよいですか?

01. file one.srt
01. file one.mp4
02. file two.srt
02. file two.mp4
03. file three.srt
03. file three.mp4

ベストアンサー1

for file in *; do
    ext=.${file##*.}                #Gets file extension
    [ "$ext" = ".$file" ] && ext="" #If file had no extension, set it to empty string
    nostr=${file%%-*}               #Remove everything after -
    mv "$file" "$nostr$ext"
done

おすすめ記事