Home > General > Rename Multiple Files

Rename Multiple Files

Renaming single files on Unix is easy and simple but whenever an average user needs to rename multiple files they always have to ask the fat admins to get off their ass and do it for them.

Well those days are over, renaming multiple files is as easy renaming single files…well almost.¬† In this article, we’ll show you multiple¬† ways you can rename files at once.¬† The examples were taken from various sources.

To rename all *.html file as *.php

$ rename .html .php *.html

To remove blank spaces from a filename:

$ rename "s/ *//g" *.php

To rename all uppercase files to lowercase:

$ rename 'y/A-Z/a-z/' *

Change .htm files to .html

$ for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done

Change .html files to .htm

$ for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done

Change .html files to .shtml

$ for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done

Change .html files to php

$ for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1php/'` ; done

Again, to rename *.txt to *.bak

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

To rename *.php5 to *.php

for x in *.php5; do n=${x/.php5/.php}; mv $x $n; done

There are many other ways of course. If you want to change the actual filename itself such as prefixing with a unique name, adding a word in between filenames or appending.. sed and awk is probably the best in that.

Categories: General Tags:
  1. October 18th, 2009 at 23:56 | #1

    This is nice helper. Sometimes I rename 100 or more files manually…I wish I had found this blog post earlier!!!!

    Thanks!!!

  2. October 30th, 2011 at 03:30 | #2

    i found error like:
    syntax error at (eval 1) line 1, near “.”
    why?

  1. No trackbacks yet.