Operations 8 min read

Master Linux File Renaming: Sed, Substring Tricks & Extension Swaps

This guide demonstrates how to batch‑replace text with sed, rename files using Bash substring expansion, and change file extensions efficiently on a Linux system, providing clear command examples and practical one‑liners for everyday operations.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux File Renaming: Sed, Substring Tricks & Extension Swaps

1. Replace text with 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

2. Rename files using variable substring expansion

vi laolang.log
wolf_20170806_1_wolf.jpg
wolf_20170806_2_wolf.jpg
wolf_20170806_3_wolf.jpg
wolf_20170806_4_wolf.jpg
wolf_20170806_5_wolf.jpg
wolf_20170806_6_wolf.jpg
wolf_20170806_7_wolf.jpg
wolf_20170806_8_wolf.jpg
wolf_20170806_9_wolf.jpg
wolf_20170806_10_wolf.jpg
# f=wolf_20170806_10_wolf.jpg
# echo $f
wolf_20170806_10_wolf.jpg
# echo ${f%wolf*.jpg}
wolf_20170806_10_
# mv $f echo ${f%wolf*.jpg}.jpg
# for f in ls *wolf.jpg ;do mv $f echo ${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}
# for f in ls *.jpg ;do mv $f ${f/%jpg/log} ;done
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.

OperationsBashFile Renamingsedshell-scripting
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.