Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61

    script for batch renaming files


    hello to everyone,

    I'm looking for a bash script that would takes some files using the find command and rename them all to the sequence 001, 002, 003, ...

    for example for files 189445.png, 18542647.png 154cc4e.png i would get the output 001.png, 002.png, 003.png (it would be cool if it counted in Hexdecimal but decimal would be fine too).

    thank you for your help.

  2. #2
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    I'm no BASH guru, but I can help you with the logic...

    Code:
    for $x in FIND_COMMAND
    parse $x to remove the leading directory
    mv $x/$y $x/$z++.jpg
    Hope this helps...all you have to do is look up a little BASH tut and put it into real code.
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  3. #3
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    I'm neither a BASH guru, but since most (all?) linux distrubution come equipped with python, here is a script that does the same: (execute it in the directory in which you have your files)

    Code:
    #! /usr/bin/env python
    
    import os, sys
    
    count = 0
    for x in os.listdir('.'):
      if x[-4:] == '.png':
        os.rename(x, ('%03x.png' % count)) # change %03x to %03d to count in decimal
        count = count + 1
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61
    thank you, but i do not understand it.
    i wanted bash script because i know just enough of bash to understand the script i asked.
    i belive you're script does work, but i do not wan't it to "just work" i wan't to know how it works.

    thak you, maxim.

  6. #5
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Like this:
    Code:
    #! /bin/bash
    
    x=0
    for fname in *.png
    do
      mv $fname `printf "%03x.png" $x`
      x=$(($x+1))
    done
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  7. #6
    Just Joined!
    Join Date
    May 2004
    Location
    Israel
    Posts
    61
    exactly

    thank you very much.

Posting Permissions

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