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.
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
yujing2. 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;done3. 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} ;doneSigned-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
