Find the answer to your Linux question:
Results 1 to 3 of 3

Thread: Batch rename

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Batch rename


    I have tried searching for any solution without any luck.

    I have about 11 000 jpg images that is named like this:

    image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)
    image-250211-142301.jpg (25. february 2011, at 02:23:01 PM)

    All of these files have correct file system created/modified date (meaning its the same as my timestamp in the file names).

    How do I rename all the files like this:


    ...sorted by date?

    I would very much appreciate any help.

  2. #2
    hmmm found no easy way to do this.

    Maybe this will help you:
    for i in `ls /directory/*.jpg -tr`; do ##you can add R to make it recursive, of course
    if [ $value -lt 10 ]; then
    elif [ $value -lt 100 ]; then
    elif [ $value -lt 1000 ]; then
    elif [ $value -lt 10000 ]; then
    elif [ $value -lt 100000 ]; then
    mv $i image$new_value.jpg

  3. #3
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris

    This script generates commands to rename the files:
    #!/usr/bin/env bash
    # @(#) s1	Demonstrate rename of inconveniently-named files.
    # Utility functions: print-as-echo, print-line-with-visual-space.
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    # Context with local commands.
    [[ -f ~/bin/context ]] && . context sed paste sort seq date
    pl " Sample data file $FILE:"
    cat $FILE
    pl " Strings denoting seconds since 1970:"
    # Remove some bad characters from date string,
    # remove string "at" from date,
    # remove all data *except* the date string,
    # feed into GNU date to get time since epoch (1970)
    sed -e 's/[.,]//g' -e 's/at//' $FILE |
    tee t1 |
    sed -e 's/^.* (//' -e 's/)//' |
    tee t2 |
    date +'%s' --file - |
    tee t3 
    pl " Epoch time pasted in front of file names:"
    # put the time since epoch to front of file name,
    # sort numerically.
    paste t3 $FILE |
    sort -k1,1n |
    tee t4
    pl " Move commands, to be saved and executed later:"
    # extract the original file name, now in order by date,
    # adding a "move" command string at the front,
    # create a file of new names as in requirements,
    # write a series of move commands, to be executed later after
    # correctness for situation has been verified.
    # to files named like " image000001.jpg"
    cut -f2 t4| sed -e 's/^/"/' -e 's/$/"/' -e 's/^/mv /' > t5
    n=$( wc -l < t5 )
    printf "image%06d.jpg\n" $( seq 1 $n ) > t6
    paste -d" " t5 t6
    % ./s1
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
    GNU bash 3.2.39
    GNU sed version 4.1.5
    paste (GNU coreutils) 6.10
    sort (GNU coreutils) 6.10
    seq (GNU coreutils) 6.10
    date (GNU coreutils) 6.10
     Sample data file data1:
    image-250211-142301.jpg (25. february 2011, at 02:23:01 PM)
    image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)
     Strings denoting seconds since 1970:
     Epoch time pasted in front of file names:
    1298575381	image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)
    1298665381	image-250211-142301.jpg (25. february 2011, at 02:23:01 PM)
     Move commands, to be saved and executed later:
    mv "image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)" image000001.jpg
    mv "image-250211-142301.jpg (25. february 2011, at 02:23:01 PM)" image000002.jpg
    See comments and man pages for details.

    It's possible that the mtime of the files could be used to make things easier -- say with command stat, but I didn't / don't have those files ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts