Introduction

Renaming multiple files one by one can be a time-consuming and error-prone task, especially when working with a large number of files. A for loop in a shell script allows you to automate this process by iterating through every file in a directory and applying a consistent naming pattern. This method is commonly used in Linux systems to rename log files, images, backups, documents, or any other group of files efficiently. Using a for loop not only saves time but also ensures consistency and reduces the risk of manual mistakes.

Prerequisites

Before using a for loop to rename all files in a folder, ensure the following requirements are met:

  • A Linux operating system or a Unix-based environment.
  • Access to a terminal with a user account that has permission to rename files in the target directory.
  • Basic knowledge of Linux commands such as cd, ls, and mv.
  • The files to be renamed should be located in a single directory.
  • It is recommended to create a backup of important files before performing bulk renaming operations.
  • Verify the current filenames using the ls command before executing the renaming script.

IMPLEMENTATION

Using For Loop to Rename All Files in a Folder
 
 
 
This is the batch script for windows to rename all files in a folder on windows. This works well perfectly. This was useful for me. It may help others to do the same. I was worried to change the files name and folder using script, atlast i find my script to fix the issue.
 
 
 
 
rem: Some testing batch command in Windows XP
 
setlocal ENABLEDELAYEDEXPANSION
@echo off
 
rem ***************** Test 1 **********************
rem about variables
 
set var1= This is a variable VAR1
set var2=’This is a variable VAR2′
set var3=”This is a variable VAR3″
echo %var1%
echo ‘%var2%’
echo “%var3%”
 
rem ***********************************************
rem Conclusion from test 1 is that assigning text
rem value to variable doesn’t need to be quoted
rem ***********************************************
 
rem **************** Test 2 **********************
rem about for loop
 
set count=0
for %%a in (1 2 3) do (
echo !count!
set /A count=!count!+1
echo %%a
)
 
for %%A in (1 2) do for %%B in (A B) DO ECHO %%A%%B
 
rem **********************************************
rem Conclusion from test 2 is that variable in
rem the for loop has to be single character like
rem %%a. %%var will not work
rem you don’t have to have
rem setlocal ENABLEDELAYEDEXPANSION for the count
rem to work but you do have to use !count! instead
rem of %count%
rem **********************************************
 
rem **************** Test 3 **********************
rem use for loop to list all .txt file names
 
for /f %%a in (’dir /b transaction*.txt’) do (
echo %%a
)
 
rem **********************************************
rem I don’t know what the switch /f in the for
rem statement and the /A switch in the set
rem statement mean but it works.
rem Can someone explain the switch? =)
rem **********************************************
 
rem **************** Test 4 **********************
rem rename all .txt file so that all of them
rem starts with “trans” instead of “transaction”
 
for /f “tokens=1,2 delims=_” %%a in (’dir /b transaction*.txt’) do (
if “%%a”==”transaction” echo yes
echo %%a
echo %%b
ren %%a_%%b tran_%%b
)
 
rem **********************************************
rem The above script works fine. It renames all
rem files named in partern transaction_XXXX.txt
rem to trans_XXXXX.txt
rem “tokens=1,2 delims=_” tells it to separate
rem whatever returned in dir command in two parts
rem using the first “_” found and put the first
rem token in %%a, the second in %%b.
rem It is very powerful and interesting!
rem **********************************************
 
goto :eof
 
 
 
 
One additional remark: if you decide NOT to set ENABLEDELAYEDEXPANSION, then the !variable! syntax fails. Not sure if that means that %variable% would work in that situation instead, but just thought I’d be explicit about where ENABLEDELAYEDEXPANSION had an effect (though obviously it’s relevant in other parts too)
 
 
 

Conclusion

Using a for loop to rename all files in a folder is a simple yet powerful way to automate repetitive file management tasks in Linux. It eliminates the need for manual renaming, improves efficiency, and ensures uniform file naming. By understanding the basic structure of a for loop and the mv command, you can easily customize renaming operations to meet different requirements. Always test your script on a small set of files or create backups before applying changes to important data.

Leave a Reply