Results 1 to 7 of 7

Thread: Rename All Files in a Folder Numerically

  1. #1
    Join Date
    Jul 2011
    Beans
    21

    Rename All Files in a Folder Numerically

    I have about 13000 files in a folder with names following this pattern: newaaaapmr, newaaaapms, newaaaapmt. No extensions.

    I'd like to rename all the files numerically from 1-13000. How would I go about doing this?

  2. #2
    Join Date
    Apr 2007
    Beans
    616
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Rename All Files in a Folder Numerically

    I'm sure there is a quick bash script that can do this, but considering im not that good with bash I would write a quick python script to do it. something like this:

    Code:
    import os, sys
    
    files = os.listdir(sys.argv[1]) # use the full path to the folder as an arugument to the script
    files.sort()
    n=1
    for f in files:
        os.rename(f, n)
        n += 1
    Last edited by cybergalvez; April 20th, 2013 at 05:59 PM. Reason: readability

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Rename All Files in a Folder Numerically

    do you need fixed width (0-padded) as in 00001-13000? printf %05d will do that, if you don't need it you can replace whole $( printf ... $i ) with $i alone

    Code:
    i=1; for f in *; do echo mv "$f" "$( printf "%05d" $i )"; ((i++)); done
    if the printout is ok remove echo to actually rename files.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Apr 2012
    Beans
    7,068

    Re: Rename All Files in a Folder Numerically

    Might it be worth adding a 'plain file' test - in case the directory contains subdirectories that are not to be included in the sequential rename?

    Code:
    i=1; for f in *; do if [ -f "$f" ]; then echo mv "$f" "$( printf "%05d" $i )"; ((i++)); fi; done
    Or if the target files all have the "new" prefix we could be even more specific

    Code:
    i=1; for f in new*; do if [ -f "$f" ]; then echo mv "$f" "$( printf "%05d" $i )"; ((i++)); fi; done

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Rename All Files in a Folder Numerically

    good point, better safe than sorry
    one could also write the condition as [ -f "$f" ] || continue;
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    May 2005
    Location
    Windsor, Ontario, Canada
    Beans
    88
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Rename All Files in a Folder Numerically

    I think both the (GTK-based) "pyrenamer" and "gprename" applications, available through the Software Centre, could assist you.
    --
    uc50_ic4more

  7. #7
    Join Date
    Jul 2011
    Beans
    21

    Re: Rename All Files in a Folder Numerically

    Great answers - always informative to get input from different people. Much appreciated.

    I used the answer from Vaphell and it worked perfectly for my use case.

Bookmarks

Posting Permissions

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