Subscribe to How-To Geek

Shell Geek: Rename Multiple Files At Once

Let’s say you have a directory with hundreds of files with the wrong file names, and you’d like to replace every filename containing test with prod. (this is a contrived example). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Today we’ll learn how to replace text in a variable in a for loop.

The “for” command works like this:

for var in <files>;do <command> $var;done

You can replace <files> with any file match pattern, such as * or *.txt, and you can replace <command> with any linux command. The command will be run in sequence on each of the files matched by the file match pattern.

This is where the bash variable handling makes it even more interesting. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax:

${var/originaltext/replacetext}

So now, if we run this command on our directory:

for f in *;do mv $f ${f/test/prod};done

For each file matched by *, bash will execute a command similar to this:

mv test.config prod.config

I’ve found that knowledge of the shell is invaluable when administering servers or just for managing your file collection, and has saved me hours of what would have otherwise been manual work.

And yes, I realize there are a number of tools that can accomplish renaming of multiple files.

The Geek is the founder of How-To Geek and a geek enthusiast. This article was written on 07/17/07 and tagged with: Ubuntu

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:
Similar Articles Featured Wiki Articles
Latest Software Reviews Quick Linux Tips
Geek Arcade Popular Forum Threads

Comments (4)

  1. skeptic

    thanks, really nice article, it has saved me some time and it will alot more in the future.

  2. sachadon

    Hey… thanks a lot for this article … this really saved lot of my time …. i reallly appreciate it …

  3. felipe alvarez

    You are a GOD. This is what I have been looking for. Can this functionality exist outside of “for loop”. How good is Bash’s regex engine? Is it full featured?

    “ls [^a}*” finds all files that begin with anything BUT letter “a”. What else is possible?

  4. Keith

    This is great, and I use it frequently for changing filename suffixes.

    However, if you are stuck with filenames that contain spaces, you need to quote around the variable and the replacement expression, as shown in the example below.

    for f in *;do mv “$f” “${f/\.oga/.ogg}”;done


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Sponsored Links
Getting Started
About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
Cmd.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.