Skip to content

Instantly share code, notes, and snippets.

@KyonLi
Last active June 16, 2024 05:42
Show Gist options
  • Save KyonLi/13519d7f9b06046723c2de3da88c5bfc to your computer and use it in GitHub Desktop.
Save KyonLi/13519d7f9b06046723c2de3da88c5bfc to your computer and use it in GitHub Desktop.
Batch rename files
#!/bin/bash
prefix="*-"
shopt -s nullglob
rename(){
local files=( $prefix* )
for i in ${!files[*]}
do
local file=${files[i]}
mv "$file" "${file#$prefix}"
echo "$file → ${file#$prefix}"
done
}
rename
#!/bin/bash
ext=jpg
shopt -s nullglob
OLDIFS=$IFS
rename(){
IFS=$'\n'
local arr=( $(ls -v *.$ext) )
IFS=$OLDIFS
for i in ${!arr[*]}
do
local filename=$(printf "%03d" $(($i+1)))
mv "${arr[i]}" "${filename}.${ext}"
echo "${arr[i]} → ${filename}.${ext}"
done
}
rename
#!/bin/bash
vid_ext=mkv
sub_ext=ass
after_sub_ext=sc.ass
shopt -s nullglob
rename(){
local vid_arr=( *.$vid_ext )
local sub_arr=( *.$sub_ext )
for i in ${!vid_arr[*]}
do
local filename=${vid_arr[i]%.*}
mv "${sub_arr[i]}" "${filename}.${after_sub_ext}"
echo "${sub_arr[i]} → ${filename}.${after_sub_ext}"
done
}
rename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment