How can I batch rename files using the Terminal?

后端 未结 10 1736
情歌与酒
情歌与酒 2020-12-12 17:43

I have a set of files, all of them nnn.MP4.mov. How could I rename them so that it is just nnn.mov?

相关标签:
10条回答
  • 2020-12-12 18:23
    ls -1 *.MP4.mov | while read f; do mv -i "$f" "$(basename \"$f\" .MP4.mov)"; done
    

    Edit: completly rewritten.

    0 讨论(0)
  • 2020-12-12 18:24

    OS X has a Rename Files… contextual menu item which is invoked when you select two or more files in Finder. Provides the same function as the Automator answer up above. Though Automator provides you with the tools to go further if you wish.

    (I know OP asked for Terminal but 33 others like Automator response)

    0 讨论(0)
  • 2020-12-12 18:27

    To test print the operation:

    for file in *.MP4.mov; do j=`echo $file | cut -d . -f 1`;j=$j".mov";echo mv \"$file\" \"$j\"; done
    

    To make it work:

    for file in *.MP4.mov; do j=`echo $file | cut -d . -f 1`;j=$j".mov";mv "$file" "$j"; done
    
    0 讨论(0)
  • 2020-12-12 18:29
    for n in *.MP4.mov
    do
       mv $n $(echo $n | sed -e 's/.MP4//')
    done
    

    This will work even on really old shells that don't have parameter substitution, and it's a tad more readable to my eyes at least.

    0 讨论(0)
  • 2020-12-12 18:32
    for i in *; 
    do j=`echo $i | cut -d . -f 1`; 
    j=$j".mov";
    mv $i $j; 
    done
    

    this will cut everything before the first dot and appends .mov

    but if some files are e.g. hi.2.mov and hi.1.mov one will be overwritten, so use it carefully ^^

    0 讨论(0)
  • 2020-12-12 18:36

    First, do a dry run (will not actually rename any files) with the following:

    for file in *.mov
    do
      echo mv "$file" "${file/MP4./}"
    done
    

    If it all looks fine, remove the echo from the third line to actually rename the files.

    0 讨论(0)
提交回复
热议问题