# Bash script - multiple file rename



## kwdrum42 (Oct 22, 1999)

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 (Feb 11, 2002)

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):

```
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.


----------

