Three Simple Linux Commands to Batch Rename Files Efficiently
Learn how to quickly rename multiple files in Linux using the rename utility, a bash for‑loop with mv, and a sed‑based loop, each illustrated with clear command examples and explanations of the underlying string manipulation techniques.
1. Using the rename command
The rename utility can replace parts of filenames in bulk. Its basic syntax is: rename source_string target_string files For example, to change mod to adb in files like atb_mod_01.cpp, run:
# list files
ls
# rename
rename mod adb *
# verify
lsAfter execution the files become atb_adb_01.cpp, atb_adb_02.cpp, etc.
2. Batch renaming with mv inside a for‑loop
When you need to change file extensions, a simple bash loop combined with mv works well. The script below converts all .txt files to .cpp:
#!/bin/bash
for name in `ls *.txt`
do
mv $name ${name%.txt}.cpp
doneThe expression ${name%.txt} strips the .txt suffix, and the new .cpp extension is appended, effectively renaming each file.
3. Using sed with a for‑loop for pattern‑based renaming
If filenames contain a numeric part that should be separated by a hyphen, sed can reformat them. The following script turns test01.txt into test-01.txt:
#!/bin/bash
for file in `ls *.txt`
do
newFile=`echo $file | sed 's/\([a-z]\+\)\([0-9]\+\)/\1-\2/'`
mv $file $newFile
doneThe sed expression uses captured groups: \([a-z]\+\) matches the alphabetic prefix, \([0-9]\+\) matches the numeric suffix, and \1-\2 inserts a hyphen between them. After the loop, each file is renamed accordingly.
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.
