Operations 9 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Batch Rename Files with sed and Bash Parameter Expansion

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
yujing

The 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}.jpg

The 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; done

3. 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}; done

These techniques enable fast, scriptable renaming of large numbers of files without manual intervention.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxFile Renamingsed
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.