Skip to content

Instantly share code, notes, and snippets.

@tankxu
Last active June 1, 2020 07:58
Show Gist options
  • Save tankxu/d3c0c68aa4c7d1dff229ae162a13c7e0 to your computer and use it in GitHub Desktop.
Save tankxu/d3c0c68aa4c7d1dff229ae162a13c7e0 to your computer and use it in GitHub Desktop.
Rename files name.sh

批量重命名文件的两种方法:

1. 通过 rename 命令配合 regex 修改替换文件名

macOS 需安装 rename brew install rename

rename -n 测试 regex 修改结果,不会执行修改命令

rename -n "s/oldname/newname/" input_files

在 oldname 中使用 regex 匹配文件名中的内容,newname 中可使用\1来引用匹配的结果

示例:

❯ rename -n "s/英文儿歌[\\s\\S]+?\.\\s*([\\s\\S]+?)_[\\s\\S]*/\1.mp4/" 英文儿歌*
'英文儿歌 1. Head Shoulders Knees And Toes_Super Simple Songs (P1. 1. Head Shoulders .mp4' would be renamed to 'Head Shoulders Knees And Toes.mp4'
'英文儿歌 2. Six in the bed__Super Simple Songs (P1. 2. Six in the bed_压制版).mp4' would be renamed to 'Six in the bed.mp4'
'英文儿歌 3. The Wheels On The Bus_Super Simple Songs (P1. 3. The Wheels On The Bus_压.mp4' would be renamed to 'The Wheels On The Bus.mp4'
'英文儿歌 4. Open Shut Them_Super Simple Songs (P1. 4. Open Shut Them_压制版).mp4' would be renamed to 'Open Shut Them.mp4'
'英文儿歌 5. If You_re Happy_Super Simple Songs (P1. If You_re Happy _ Super Simple S.mp4' would be renamed to 'If You.mp4'
'英文儿歌 6. One Little Finger_Super Simple Songs (P1. One Little Finger_压制版).mp4' would be renamed to 'One Little Finger.mp4'

2. 通过 bash shell 的 for loop 替换文件名为准备好的数组内容

有时候要对文件名做多种处理,除匹配内容外还需要增加空格、切换大小写等,在 sublime text 或 vscode 里两下就改完了,用 rename 话可能要写大半天的 regex。

示例:

要改的文件名列表:

【合辑】Super Simple Songs 英文启蒙儿歌 (P1. 10LittleDinosaurs十只小恐龙).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P10. SevenSteps七步曲).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P11. TheAntsGoMarching蚂蚁行军).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P12. TheBathSong洗澡歌).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P13. ThisIsTheWay我就是这样做的).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P14. TwinkleTwinkleLittleStar一闪一闪亮晶晶).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P2. BabyShark鲨鱼宝宝).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P3. DownByTheBay1在小湖边1).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P4. GoodMorningMrRooster早上好公鸡先生).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P5. Hello-你好).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P6. If You’re Happy-幸福拍手歌).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P7. ISeeSomething Pink我看见粉色的东西).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P8. Little Snowflake小雪花).mp4
【合辑】Super Simple Songs 英文启蒙儿歌 (P9. One Little Finger一个小手指).mp4

  1. 在熟悉编辑器里准备好新的文件名列表,写成 shell script 的数组并在 bash 中执行:

new_files=("10 Little Dinosaurs 十只小恐龙.mp4" "Seven Steps 七步曲.mp4" "The Ants Go Marching 蚂蚁行军.mp4" "The Bath Song 洗澡歌.mp4" "This Is The Way 我就是这样做的.mp4" "Twinkle Twinkle Little Star 一闪一闪亮晶晶.mp4" "Baby Shark 鲨鱼宝宝.mp4" "Down By The Bay 在小湖边.mp4" "Good Morning Mr Rooster 早上好公鸡先生.mp4" "Hello 你好.mp4" "If You Re Happy 幸福拍手歌.mp4" "I See Something Pink 我看见粉色的东西.mp4" "Little Snowflake 小雪花.mp4" "One Little Finger 一个小手指.mp4")

  1. shell script 改名字

i=0; for f in 【合辑】*; do mv "$f" "${new_files[$i]}"; i=`expr $i + 1`; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment