Filenames
如何在特定字元後刪除多個文件名中的字元串?
我在一個文件夾中有多個具有這種名稱格式的文件
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
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
您可以像這樣使用 bash 參數替換:
for i in *; do # ${i%%-*} get everything before the first '-' (use ${i%-*} for the last '-') # ${i/.*} remove everything after the '.' # ${i##${i%.*}} remove everything except everything after the last '.' mv "${i}" "${i%%-*}${i##${i%.*}}" done