1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Bash script - multiple file rename

Discussion in 'Linux and Unix' started by kwdrum42, Jan 28, 2004.

Thread Status:
Not open for further replies.
  1. kwdrum42

    kwdrum42 Thread Starter

    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
  2. 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):
    for i in *
        if [ -z "$NEWFILENAME" ]
            echo did not find $FILENAME in $LISTFILE
            echo would mv $FILENAME $NEWFILENAME
    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
As Seen On...

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.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/198913