Unix, Xenix and ODT General FAQ

How can I rename multiple files using wildcards?

If you are using Linux, you should have the "rename" command, which lets you use wildcards to do some simple renaming.

If you are using some other Unix, you may not have "rename", or even if you do, whatever you want to do may be too complex for that. In that circumstance, you can't do it as you would in DOS, but you can do it.

If using Windows, Randi suggested biterScripting, a free Windows scripting tool (there are others, including Microsoft's own offerings).

Back to Unix and Linux: First, the "mv" command is what renames a file- you are "moving" it to a new name.

To rename with wildcards, you need to write a simple shell script. This can be done with any shell, but shells like ksh make it easier. For example, here's a couple of scripts to rename a bunch of uppercase file names to lowercase:

 # [A-Z]* matches upper case names
 for i in [A-Z]*
        j=`echo $i | tr '[A-Z]' '[a-z]'`
        mv $i $j

Note the backtics ("`") carefully.

With ksh this becomes easier:

 typeset -l j
 for i in [A-Z]*
   mv $i $j

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> (SCO Unix) How can I rename multiple files using wildcards?


Increase ad revenue 50-250% with Ezoic

Mon Aug 22 18:01:09 2005: 1006   anonymous

There's a rename command for bash shells on linux, for more info, type man rename.
rename .cpp .cc *.cpp
rename oldname newname *.cpp

Mon Aug 22 20:56:20 2005: 1008   TonyLawrence

Thanks! Hadn't noticed that.

Tue Oct 25 03:18:10 2005: 1239   anonymous

for i in pic?.jpg; do mv $i ${i/pic/pic0}

adds a 0 to all files with only one counter. For instance:

pic1.jpg -> pic01.jpg

Tue Oct 25 22:42:49 2005: 1247   anonymous

looks like that would change {pic2, pic 5, picx, pic21} to pic01, pic02, pic03 but not move pic21 at all.

Wed Oct 26 08:32:47 2005: 1252   TonyLawrence

Though that may be just what he wants..

You do have to be careful with expansions; they don't always do what you expect.

Thu Jun 8 12:59:12 2006: 2080   anonymous

I need to do the opposit here. I have multiple * in my filenames & need to translate mass files to use a _ instead. Help! dennis.o'brien@qimonda.com

Thu Jun 8 16:10:13 2006: 2084   TonyLawrence

That's not an issue:

j=`echo $i | tr '*' '_'`

Fri Jul 20 05:47:45 2007: 3062   BatchFileRename

You could try using a program for renaming. I have included a link above to a rename program for the mac.

Sat Sep 27 05:07:19 2008: 4609   anonymous

Ummmm... I think you mean

Wed Mar 4 20:26:53 2009: 5601   Michaeldaily

Here is a python script which does this using string.split (unfortunately the python indents got lost in converting to text):
#efficient batch file renamer using split function of strings
import sys
import os
pat1, pat2 = sys.argv[1], sys.argv[2]
myls = os.listdir('.')
for f in myls:
fsplit = f.split(pat1)
if len(fsplit) < 2:
Nstr = len(fsplit)
newname = fsplit[0]
for i in range(1,Nstr):
newname += pat2 + fsplit[i]
#print f
#print newname
command = 'mv %s %s' % (f, newname)
#print command


Wed Mar 4 20:45:15 2009: 5602   TonyLawrence

Thanks Mike -

I fixed the indents for you.

Tue Mar 17 14:43:11 2009: 5737   Todd

I have noticed that some distrobutions have different rename commands so I wouldn't trust it. ArchLinux comes with one from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/ but Ubuntu appears to come with one from perl.

Thu Dec 3 10:07:42 2009: 7709   anonymous

I need to rename the only the first four common chareacters in 20 files. like i have file name as abcd_1,abcd_2 ... abcd_20 so i need to only change abcd with xy. How can i achiev this. Using script or any othe rmethod. Thanks for the help.

Thu Dec 3 13:49:12 2009: 7710   TonyLawrence

The historic way was something like this:

for i in abc*; do newn=`echo $i | sed s/^abc/xy/`; mv $ $newn; done

Tue Feb 9 17:23:27 2010: 8042   Lena


I used the following method in the ftp script, but got "We only support non print format, sorry"

for i in *.txt;
do mv $i ${i/edi/work}

Tue Feb 9 17:43:44 2010: 8043   TonyLawrence


You are confused. You can't use shell syntax inside an FTP script.


Tue Feb 9 18:15:06 2010: 8044   LenaKeung


Thanks for your response. I looked at the examples provided in your link. Forgive me for not being an experience script writer. I do not understand what the following code is doing:

echo "machine my.remote.server login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /home/postgres/scripts/activite/export" >> $HOME/.netrc
echo "cd sauve" >> $HOME/.netrc
for i in *.sql
echo "put $i " >> $HOME/.netrc
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
# always end a macdef with a blank line
chmod 600 $HOME/.netrc
ftp my.remote.server

Tue Feb 9 18:24:10 2010: 8045   TonyLawrence


Those lines are creating a .netrc file. FTP reads commands from that file.

Tue Feb 9 18:44:57 2010: 8046   lenakeung


in other words, I should have those lines included in my script to create a .netrc file before even connecting to the ftp remote site? I should also modified those lines to do renaming of the file instead of retrieving the file, right? Can I replace the logic in your link to the logic I have in my ftp script for the creation of the .netrc file?

Tue Feb 9 18:50:47 2010: 8047   TonyLawrence


Commands in a .netrc file are the same commands you'd use in an ftp session.

Wed Feb 10 15:39:08 2010: 8055   lenakeung


Here is my entire script :
ADDR=testtransfer.sterlingtms.com; export ADDR

ftp -inv $ADDR <<- ftpcmds
user $USER $PASS
put 214.ex
rename 214.ex 214.done
put 990.ex
rename 990.ex 990.done

ftp -inv $ADDR <<- ftpcmds
user $USER $PASS
cd outgoing
mget *.edi
mdelete *.edi

The reason I have to rename the file before the download is to avoid deleting new files dropped into the mailbox after the download and before the delete. Where should I put the piece of code to create the .netrc file.

Wed Feb 10 15:47:33 2010: 8056   TonyLawrence


Go back and read the ftp article I pointed you at above. If you don't understand that, see

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us