Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Search Search
Search for:
Tech Support Guy > > >

Bash script - multiple file rename


(!)

kwdrum42's Avatar
kwdrum42 kwdrum42 is offline
Member with 66 posts.
THREAD STARTER
 
Join Date: Oct 1999
28-Jan-2004, 07:20 PM #1
Bash script - multiple file rename
I have a large number of images in a directory, all named in the format yyyy-mm-dd.gif. I also have a text file in the format:

yyyy-mm-dd <string1>
yyyy-mm-dd <string2>

Each of the dates in the file corresponds to a filename in the directory, and all the strings are different (they are descriptions of the images). What I would like to do is rename all the files in the directory to yyyy-mm-dd-<string#>.gif in a bash script.

Any ideas?

- Kevin
codejockey's Avatar
Senior Member with 1,405 posts.
 
Join Date: Feb 2002
29-Jan-2004, 04:10 AM #2
I assume that each file in the directory has a unique filename (i.e., the dates are unique). If the file that contains the list of filenames is called filenames.txt, you might try something like the following (note that spacing, quotes, etc. are all significant):
Code:
LISTFILE=filenames.txt
for i in *
do
    FILENAME=$i
    NEWFILENAME=`egrep $FILENAME $LISTFILE`

    if [ -z "$NEWFILENAME" ]
    then
        echo did not find $FILENAME in $LISTFILE
    else
        echo would mv $FILENAME $NEWFILENAME
    fi
done
If you run this script, it should print out a list of the files to be renamed along with their new names, or a message if a file could not be found in the list of filenames (you will probably have at least one of these -- i.e., the "filenames.txt" file, although you could certainly tweak the script to skip over this file). When you are satisfied that the script does what you want, just remove the words "echo would" from the else clause, and re-run the script.

Warning: this is strictly off the top of my head; I haven't tested it.

Hope this helps.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑