APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version


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]*
 do
        j=`echo $i | tr '[A-Z]' '[a-z]'`
        mv $i $j
 done
 

Note the backtics ("`") carefully.



With ksh this becomes easier:

 #!/bin/ksh
 typeset -l j
 for i in [A-Z]*
 do
   j=$i
   mv $i $j
 done
 


If this page was useful to you, please help others find it:  





22 comments



Click here to add your comments





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.
Example:
rename .cpp .cc *.cpp
rename oldname newname *.cpp



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

gravatar
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

gravatar
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

gravatar
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 http://gotoes.org/sales/Batch_File_Rename/



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

gravatar
Here is a python script which does this using string.split (unfortunately the python indents got lost in converting to text):
================= #!/usr/bin/python #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:         continue     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     os.system(command) 

=================




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

gravatar
Thanks Mike -

I fixed the indents for you.



Tue Mar 17 14:43:11 2009: 5737   Todd
http://quadronyx.org/blogs/fallen/
gravatar
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
http://mindgrillq.blogspot.com
gravatar
Hi,
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

gravatar
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

gravatar


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}
done






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

gravatar


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

See http://aplawrence.com/Unixart/scripting_ftp.html



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

gravatar


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:

#!/bin/bash
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
do
echo "put $i " >> $HOME/.netrc
done
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

gravatar


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



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

gravatar


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

gravatar


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

gravatar


Here is my entire script :
ADDR=testtransfer.sterlingtms.com; export ADDR
USER='N21fF125';
PASS='O16$h296';

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

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

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

gravatar


Go back and read the ftp article I pointed you at above. If you don't understand that, see http://aplawrence.com/rightnow.html




Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



LOD Communications, Inc.

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

g_face.jpg

This post tagged:

       - FAQ
       - Popular






















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals