Rename multiple files

November 5th, 2006 toydi Posted in Common, for, mv, Regular Expression | Hits: 214755 | 29 Comments »

Often over time, we will want to reorganize a group of files by renaming them.

To rename *.txt to *.bak
(e.g. to rename ham.txt to ham.bak)

for f in *.txt; do mv "$f" "${f%.txt}.bak"; done

To remove ‘new-‘ from new-*
(e.g. to rename new-ham.txt to ham.txt)

for f in new-*; do mv "$f" "${f#new-}"; done

${variable%pattern} vs ${variable#pattern}

The funny-looking symbol, ${f%.txt} is a useful match-and-remove string operator:

If the pattern ‘.txt’ matches the end of variable $f, it will remove the matching part (that’s ‘.txt‘) and return the rest. Try this:

f=new-ham.txt      # define $f as 'new-ham.txt'
echo ${f%.txt}     # display 'new-ham'

What about ${f#new-}? It’s almost the same, but it matches the pattern at the beginning of the variable.

echo ${f#new-}     # display 'ham.txt'

29 Responses to “Rename multiple files”

  1. Great tips, this is an example related to regular expression, why don’t add in to RE category?

  2. As you wish. ;-)

  3. This is the 2-cent tip that worths $1,000,00 dollars. Thank you very much. It’s what I was looking for days ago.

  4. rename ‘s/\.txt/.bak/’ *.txt

  5. Cool, that is convenient. I have trace it . It is a perl script ‘prename’ in my box.

  6. Hi
    You can accomplish the same trick and much more using Vladimir Lanin’s mmv utility. It is available for most distros.

  7. At first I was like “NO WAY” that can be done, “it looks exactly like bash/sh syntx” but then I realized that it was on linux.

    MS DOS have some syntax in common with UNIX shell syntaxes, but not that much.

    BTW I saw that you could use a hosts.deny in MS Windows (just put it in C:\hosts.deny apparently). I dont think it has anything to do with the linux/BSD tcp wrappers though ;D

  8. great tip! i can now speed up my `split`s w00t

  9. I tried rename ’s/\.txt/.bak/’ *.txt but it does nothing what so ever on my system (Suse 10.2). Looks simple enough but nothing ever is with Linux, lol

  10. Ubuntu 7.04 works

  11. thanks for the tip

  12. /usr/bin/rename appears to come-with Perl (v5.8.8 on my system). It’s a Perl script.

  13. this is very useful. first time i thik it can’t be possible. but the question was i tryed to find it.

  14. […] reveal about you? A funny blog by David Gorcey to keep you marginally entertainedRename multiple files ? Linux by Examples Tags baby shower favor wedding favor party favor candy favor bar wrappers wrapper This product […]

  15. should you need to rename or change the filename on multiple files where the char or chars that needs to be replaced is in the middle e.g. file-is-this.txt say you want to replace “is” with “or” you can do this:

    for i in *.txt ; do mv “${i}” “${i/is/or}” ;done;

    i hope that helps someone

  16. Thanks this helped allot. I have to rename all the chapters in the bible now it is not a problem. Thanks again

  17. thanks zonkie! yes that helped me

    if someone got some output like this

    mv: cannot stat `“4322.txt”’: No such file or directory
    just pay attention on quotation marks ” ” by copying

  18. I have bunch of files with this pattern
    where I need to remove _02032010 this from filename
    so that XYZon01_02032010.txt = XYZon01.txt


    any help

  19. manjunathpuru Says:

    Great tip………This is what i needed………

  20. Thanks zonkie, exactly what I was looking for!

  21. thanks zonkie and toydi

  22. dear sir

    my files are in the order of
    zzaa100918 soooo on, so i want to change as all these files are as to change

    soooooooo on
    am wait for u reply

  23. pretty nice tips, thank you very much!

  24. You could easy rename filenames like this.

    # Purpose: Rename many files

    #Following script is useful if you have many files that #begins with the same name and you want them renamed to #another name.
    #Forexample: XYZ_blabla, XYZ_blabla to ABC.1, ABC.2

    #This script needs 2 arguments when you run the script.
    #The first argument is to specify the filenames that should #be renamed. The second argument is the new filename.
    #Variable COUNT makes the filename unique by counting up 1
    #after each mv command inside the for loop.


    #checks if 2 parameter is applied
    if [ $# != $PARAM ]
    echo At least $PARAM parameter required
    exit $BADARG

    # $1 = first parameter and $2 = second parameter

    for file in `ls -A $1*`
    mv $file $2.$COUNT
    let “COUNT += 1”

  25. Smart stuff, I look forward to reading more.

  26. zonkie, thank you so much, your tip is a God sent, and balkonskij saved me hours!

  27. you are a super hero thanks a ton

  28. I’m gone to say to my little brother, that he should also go to see this web site on regular basis to obtain updated from newest
    news update.

  29. I see, that your blog needs unique and fresh content. I know
    it is hard to write posts manually everyday, but there is solution for this.
    Just search in g00gle for- Atonemen’s tips

Leave a Reply