Skip to content

Instantly share code, notes, and snippets.

@hexadeciman
Created June 7, 2023 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hexadeciman/c0687a6efb56ff1de2f519c1f935c90f to your computer and use it in GitHub Desktop.
Save hexadeciman/c0687a6efb56ff1de2f519c1f935c90f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Specify the directory path
directory="./"
# Change to the specified directory
cd "$directory" || exit
# Define the mapping array
declare -A mapping=(
["afg"]="af"
["zaf"]="za"
["ala"]="ax"
["alb"]="al"
["dza"]="dz"
["deu"]="de"
["and"]="ad"
["ago"]="ao"
["aia"]="ai"
["ata"]="aq"
["atg"]="ag"
["sau"]="sa"
["arg"]="ar"
["arm"]="am"
["abw"]="aw"
["aus"]="au"
["aut"]="at"
["aze"]="az"
["bhs"]="bs"
["bhr"]="bh"
["bgd"]="bd"
["brb"]="bb"
["blr"]="by"
["bel"]="be"
["blz"]="bz"
["ben"]="bj"
["bmu"]="bm"
["btn"]="bt"
["bol"]="bo"
["bes"]="bq"
["bih"]="ba"
["bwa"]="bw"
["bvt"]="bv"
["bra"]="br"
["brn"]="bn"
["bgr"]="bg"
["bfa"]="bf"
["bdi"]="bi"
["cym"]="ky"
["khm"]="kh"
["cmr"]="cm"
["can"]="ca"
["cpv"]="cv"
["caf"]="cf"
["chl"]="cl"
["chn"]="cn"
["cxr"]="cx"
["cyp"]="cy"
["cck"]="cc"
["col"]="co"
["com"]="km"
["cog"]="cg"
["cod"]="cd"
["cok"]="ck"
["kor"]="kr"
["prk"]="kp"
["cri"]="cr"
["civ"]="ci"
["hrv"]="hr"
["cub"]="cu"
["cuw"]="cw"
["dnk"]="dk"
["dji"]="dj"
["dom"]="do"
["dma"]="dm"
["egy"]="eg"
["slv"]="sv"
["are"]="ae"
["ecu"]="ec"
["eri"]="er"
["esp"]="es"
["est"]="ee"
["usa"]="us"
["eth"]="et"
["flk"]="fk"
["fro"]="fo"
["fji"]="fj"
["fin"]="fi"
["fra"]="fr"
["gab"]="ga"
["gmb"]="gm"
["geo"]="ge"
["sgs"]="gs"
["gha"]="gh"
["gib"]="gi"
["grc"]="gr"
["grd"]="gd"
["grl"]="gl"
["glp"]="gp"
["gum"]="gu"
["gtm"]="gt"
["ggy"]="gg"
["gin"]="gn"
["gnb"]="gw"
["gnq"]="gq"
["guy"]="gy"
["guf"]="gf"
["hti"]="ht"
["hmd"]="hm"
["hnd"]="hn"
["hkg"]="hk"
["hun"]="hu"
["imn"]="im"
["umi"]="um"
["vgb"]="vg"
["vir"]="vi"
["ind"]="in"
["idn"]="id"
["irn"]="ir"
["irq"]="iq"
["irl"]="ie"
["isl"]="is"
["isr"]="il"
["ita"]="it"
["jam"]="jm"
["jpn"]="jp"
["jey"]="je"
["jor"]="jo"
["kaz"]="kz"
["ken"]="ke"
["kgz"]="kg"
["kir"]="ki"
["kwt"]="kw"
["lao"]="la"
["lso"]="ls"
["lva"]="lv"
["lbn"]="lb"
["lbr"]="lr"
["lby"]="ly"
["lie"]="li"
["ltu"]="lt"
["lux"]="lu"
["mac"]="mo"
["mkd"]="mk"
["mdg"]="mg"
["mys"]="my"
["mwi"]="mw"
["mdv"]="mv"
["mli"]="ml"
["mlt"]="mt"
["mnp"]="mp"
["mar"]="ma"
["mhl"]="mh"
["mtq"]="mq"
["mus"]="mu"
["mrt"]="mr"
["myt"]="yt"
["mex"]="mx"
["fsm"]="fm"
["mda"]="md"
["mco"]="mc"
["mng"]="mn"
["mne"]="me"
["msr"]="ms"
["moz"]="mz"
["mmr"]="mm"
["nam"]="na"
["nru"]="nr"
["npl"]="np"
["nic"]="ni"
["ner"]="ne"
["nga"]="ng"
["niu"]="nu"
["nfk"]="nf"
["nor"]="no"
["ncl"]="nc"
["nzl"]="nz"
["iot"]="io"
["omn"]="om"
["uga"]="ug"
["uzb"]="uz"
["pak"]="pk"
["plw"]="pw"
["pse"]="ps"
["pan"]="pa"
["png"]="pg"
["pry"]="py"
["nld"]="nl"
["per"]="pe"
["phl"]="ph"
["pcn"]="pn"
["pol"]="pl"
["pyf"]="pf"
["pri"]="pr"
["prt"]="pt"
["qat"]="qa"
["reu"]="re"
["rou"]="ro"
["gbr"]="gb"
["rus"]="ru"
["rwa"]="rw"
["esh"]="eh"
["blm"]="bl"
["kna"]="kn"
["smr"]="sm"
["maf"]="mf"
["sxm"]="sx"
["spm"]="pm"
["vat"]="va"
["vct"]="vc"
["shn"]="sh"
["lca"]="lc"
["slb"]="sb"
["wsm"]="ws"
["asm"]="as"
["stp"]="st"
["sen"]="sn"
["srb"]="rs"
["syc"]="sc"
["sle"]="sl"
["sgp"]="sg"
["svk"]="sk"
["svn"]="si"
["som"]="so"
["sdn"]="sd"
["ssd"]="ss"
["lka"]="lk"
["swe"]="se"
["che"]="ch"
["sur"]="sr"
["sjm"]="sj"
["swz"]="sz"
["syr"]="sy"
["tjk"]="tj"
["twn"]="tw"
["tza"]="tz"
["tcd"]="td"
["cze"]="cz"
["atf"]="tf"
["tha"]="th"
["tls"]="tl"
["tgo"]="tg"
["tkl"]="tk"
["ton"]="to"
["tto"]="tt"
["tun"]="tn"
["tkm"]="tm"
["tca"]="tc"
["tur"]="tr"
["tuv"]="tv"
["ukr"]="ua"
["ury"]="uy"
["vut"]="vu"
["ven"]="ve"
["vnm"]="vn"
["wlf"]="wf"
["yem"]="ye"
["zmb"]="zm"
["zwe"]="zw"
)
# Loop through all files in the directory
for file in *; do
# Check if the file exists and is a regular file
if [[ -f "$file" ]]; then
# Get the file extension
extension="${file##*.}"
# Get the file name without the extension
filename="${file%.*}"
# Check if the file name is in the mapping array
if [[ -v mapping["$filename"] ]]; then
# Get the new file name from the mapping array
new_filename="${mapping["$filename"]}"
# Rename the file with the new name and the same extension
mv -i "$file" "$new_filename.$extension"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment