How to Batch Rename Files with sed and Bash Parameter Expansion
This guide demonstrates three Linux techniques—using sed, Bash substring replacement, and extension change—to efficiently rename multiple files in bulk, complete with command examples and step‑by‑step explanations.
This article provides practical methods for batch renaming files on a Linux system.
1. Rename using sed
# find / -type f -name wolf.log
/wolf.log
/tmp/wolf.log
/root/wolf/wolf.log
# sed -i 's#wolf#yujing#g' find / -type f -name wolf.log
# find / -type f -name "wolf.log" | xargs cat
yujing
yujing
yujingThe sed command replaces the string wolf with yujing directly in the files found.
2. Rename using Bash variable substring replacement
# Example file list
wolf_20170806_1_wolf.jpg
wolf_20170806_2_wolf.jpg
...
# f=wolf_20170806_10_wolf.jpg
# echo ${f%wolf*.jpg}
wolf_20170806_10_
# mv $f ${f%wolf*.jpg}.jpgThe expression ${f%wolf*.jpg} removes the trailing wolf*.jpg part, allowing the file to be renamed to wolf_20170806_10_.jpg. A loop can process all matching files:
for f in $(ls *wolf.jpg); do mv $f ${f%wolf*.jpg}.jpg; done3. Change file extensions
# f=wolf_20170806_10_.jpg
# echo ${f/%jpg/log}
wolf_20170806_10_.log
# mv $f ${f/%jpg/log}The pattern ${f/%jpg/log} substitutes the .jpg suffix with .log. A loop can apply this to all .jpg files:
for f in $(ls *.jpg); do mv $f ${f/%jpg/log}; doneThese techniques enable fast, scriptable renaming of large numbers of files without manual intervention.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
