How to batch rename files

This afternoon I needed to rename a bunch of files from one form to another in a command shell… Well technically I didn't need to do it in a shell - but, as sure as there is a hole in my ass, I wasn't gonna go through renaming them all manually!

They needed to go from, for example, add.png to add_32.png. After a little research into commands like printf, awk, bison and so on - I suddenly realized that 'cut' held the key!

for i in *; do j=`echo $i | cut -d . -f 1`; j=$j"_32.png"; mv $i $j; done

[adsense:468x60:4496506397]

Basically, this says "for every file in the folder, cut the filename on all dots and take the first result into variable 'j'. Then append '_32.png' onto the end of the variable. Finally move the original file to the new filename".

I hope this saves someone else a bit of time!

Comment Icon

25 Comments

The most recent comment was on Wed, 1st Feb 2012 - 21:40

Looking for a solution..please any help...

Any scripts, software or work arounds, that will allow renaming of 3 part file which would add a 3 digit value example a folio id. If a change to some of the programs could be made to add an option, example do a search and replace for the next 3 files. Then a list could be used.

Thanks in advance...

Rich

Not sure what you mean but an example might help...

Wkflow consists of a folder with up to 96 pages. Each page consists of a 3 part file. These are a handshake format.

Example CustomerA3149R.pg
CustomerA3149R.ch
CustomerA3149R.lh

Need to rename with a folio at the beginning.

Example 003_LHJ3149R.pg
003_LHJ3149R.ch
003_LHJ3149R.lh

Typical renaming programs support a single or group of single files, ie ,,one image file or a .mp3 file.

My problem is i'm looking to be able to handle a 3 part file as shown, utilizing a sequential renaming function but I get hung up on the 3 part factor.
It wants to rename 003,004,005. I wish as stated before to have the program prompt how many files do you want to use the identified sequential number. 1 for normal processing , 2 for a 2 part file or 3 for a 3 part.....hope this better explains.

The other factor is the link between the .pg file must still link the .ch and the .lh

Thanks

Rich

This should do the trick:

prefix="003_LHJ"; for i in *.{pg,ch,lh}; do j=${i/CustomerA/$prefix}; mv $i $j; done

If the prefix is a part of a path, you might use something like this:

prefix="`pwd | cut -d / -f 3`"

That would set prefix to the 3rd part of a path (eg: /x/y/z -> z). Attention to the inversed quotes (`) inside the double quotes.

Or you can use another "for" before to get the prefix from paths/files.

I just figured out a more universal script...

for i in * ; do j=`echo $i | sed 's#searchstring#replacestring#g' - ` ; mv "$i" "$j" ; done

change searchstring and replacestring for whatever you want to replace. If a file is missing this searchstring, then you will get an error for that file and nothing will happen to it.

Just a quick note to say I stumbled across this page perhaps a few weeks ago, and the little snippet of code you've posted repeatedly saves me absurd amounts of time. Thanks!

This one works recursively:

for i in `find` ; do j=`echo $i | sed 's#searchstring#replacestring#g' - ` ; mv "$i" "$j" ; done

The only change is the initial file gathering, from "*" to "`find`".

The script breaks if the folder name contains white space...

Hi, i just want to rename multiple rec. which is present in a particular single directory.for EX:
P889158-271-20090908-112524-1252434324.4956.wav
P889158-271-20090908-112558-1252434324.4956.wav
P889158-271-20090908-112625-1252434324.4956.wav

Like this there are 1000 of rec...I have to just rename the first character of those rec. Ex:- abc-271-20090908-112524-1252434324.4956.wav
abc-271-20090908-112558-1252434324.4956.wav

so can u plz help me how can i rename those rec. at once.

The "rename" command also does exactly what you are looking for. It batch renames files based using Perl regular expressions. Here is the rename command that accomplishes what you are doing:

rename -v 's/\./_32./g' *

Cheers,
Jason

Try this command:

rename -v 's!P889158!abc!g' *.wav

If you want to replace everything before the 1st "-" with abc, do the following:

rename -v 's!^.*?-!abc-!g' *.wav

(-v just prints out the changes that were made)

Also if you want to see what the rename command would do without actually renaming the files, use the "-n" flag.

I want to add dates next to the file name. For example: "hello_25November2009.txt" or "hello_25-11-2009.txt", assuming that the original file name is hello.txt

Thanks in advance.

samiLyle says:

> I want to add dates next to the file name. For
> example: "hello_25November2009.txt" or "hello_25-11-2009.txt",
> assuming that the original file name is hello.txt

If someone wants to do this on Windows, here is a quick script.

# Script TimeRenamer.txt
# Input argument - folder path
var str path
# Go to the folder path
cd $path
# Collect the list of files.
var str list, file, time, name
find -n "*" $path ($ftype=="f") > $list
# Go thru list of files one by one
while ($list <> "")
do
    # Get the next file.
    lex "1" $list > $file
    # Get the file creation time.
    af $file ; set $time = $fctime
    # Get the file's name from the path
    stex -p "^/^l[" $file > $name
    # Insert $time before the last dot.
    sin "^.^l" $time $name
    # Rename file.
    system rename ("\""+$file+"\"") ("\""+$name+"\"")
done

Script is in biterscripting ( http://www.biterscripting.com ) . Copy and paste the script into a file /Scripts/TimeRenamer.txt, start biterscripting and enter the following command.

script "/Scripts/TimeRenamer.txt" path("/path/to/some/directory")

That's it.

Thank you very much. This saved me a lot of time! rename ftw!

Cheers,
Daniel

For those who are not afraid of GUI tools ;) gnome has GPRename, surely fit your needs and more, there is also krename for KDE users, haven't tried the last though.
May be useful for anyone stumbling along here, no matter the post talked in the first place about solutions from the command line. That's why we appreciate the fine work behind every of the proposed solutions.

That's the thing that should have been found a lot of time ago! Instead of f*****g with a batch of shell commands and trying to somehow escape characters in order to avoid errors. Thanks, dude!

A word of caution.
Your first snippet:

for i in *; do j=`echo $i | cut -d . -f 1`; j=$j"_32.png"; mv $i $j; done

will change everything it finds, including directories. So it would be desirable to limit the scope of the files to be renamed, like so:

for i in *png; do j=`echo $i | cut -d . -f 1`; j=$j"_32.png"; mv $i $j; done

Other than that, very helpful tips.
Best regards.

Thanks buddy for posting this script. It helped me from saving my ass-hole to be fucked by my boss. :) :) :)

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.