前言:如果存在大量同類型的文件需要修改名稱時旅薄,可以通過命令行來修改。Mac系統(tǒng)也自帶批量修改的工具泣崩,但是不好用少梁。其實現(xiàn)在這種工作可以直接詢問AI助手,然后修改它給的腳本即可矫付。
使用到的工具:Warp模擬終端凯沪,首先該軟件還在測試階段,其次Mac獨占买优,最后它需要登錄賬號使用(就必然會收集一部分信息)妨马,比較看重隱私的朋友就不要使用了挺举,娛樂和學習則是非常好用。
官網(wǎng):https://www.warp.dev/
使用參考:https://pwa.sspai.com/post/79262
本次我們直接嘗試內(nèi)嵌的AI助手烘跺,寫腳本時就不用搜索引擎湘纵,直接問它就可以,目前每天可以免費提問100次滤淳。提問:how do I rename a bunch of jpg files
Warp AI的回答
把代碼復制出來梧喷,其實是可以直接用的。并且他還解釋了代碼脖咐,以及給腳本文件授予可執(zhí)行權限后伤柄,再執(zhí)行。
#!/bin/bash
# Set the directory where the JPG files are located
directory="/path/to/jpg/files"
# Set the new name prefix
new_name_prefix="new_name_"
# Loop through each JPG file in the directory
for file in "$directory"/*.jpg; do
# Get the current file name
current_name=$(basename "$file")
# Generate the new file name
new_name="${new_name_prefix}${current_name}"
# Rename the file
mv "$file" "$directory/$new_name"
done
但是我想要重名為001文搂、002、003這種格式的秤朗,繼續(xù)提問:how do I rename file to align them in numebr sequence
Warp AI的回答對齊排序
仔細觀察煤蹭,其中“$(printf %03d $start_number)”這一句,就實現(xiàn)了排序并且是對齊的取视。
#!/bin/bash
# Set the directory where the files are located
directory="/path/to/files"
# Set the prefix for the new file names
prefix="file"
# Set the starting number for the sequence
start_number=1
# Loop through each file in the directory
for file in "$directory"/*; do
# Get the current file name
current_name=$(basename "$file")
# Generate the new file name with aligned number sequence
new_name="${prefix}_$(printf "%03d" "$start_number")"
# Rename the file
mv "$file" "$directory/$new_name"
# Increment the sequence number
((start_number++))
done
最后硝皂,我不想要每次都修改文件夾路徑,直接拷貝腳本到對應文件夾作谭,再執(zhí)行即可稽物。更進一步的話,我們可以繼續(xù)提問折欠,比如如何執(zhí)行腳本時接受一個文件路徑參數(shù)(實際得用英文提問)贝或,這樣就是更高級的玩法了。
#!/bin/bash
# prefix 表示前綴
prefix=""
# starting 表示新的文件名稱從哪個數(shù)字開始
starting=1
for file in *.jpg; do
# new_name 表示新的文件名稱
new_name="${prefix}$(printf "%03d" $starting)".jpg
# mv 開始修改文件名稱
mv "$file" "$new_name"
# 遞增文件編號
((starting++))
done