Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Renaming a batch of files

  1. #1
    Join Date
    May 2012
    Beans
    304

    Renaming a batch of files

    Good day to you,

    I would like to rename a batch of files using command line:

    Code:
    00001-fqAQHelS.mp4
    00002-cKIaSnn1.mp4
    00003-d6lha9ys.mp4
    00004-eUyAzS7x.mp4
    00005-nVFSjsiW.mp4
    ..
    00010-ffdsfselS.mp4
    00011-fqAQHelS.mp4
    00012-cKIaSnn1.mp4
    00013-d6lha9ys.mp4
    00014-eUyAzS7x.mp4
    00015-nVFSjsiW.mp4
    Some files have 4 zeros at the begining (0001) and some 3 (00010), then 2 (00100),.. my command line is:

    Code:
    N=1; for i in *.mp4; do echo $i title_$[N+1].mp4; N=$[N+1]; done
    The output is something like:

    Code:
    title_2.mp4
    title_3.mp4
    ..
    title_10.mp4
    title_11.mp4
    title_12.mp4
    How can I keep the same number of zeros as the first ones:

    Code:
    title_00002.mp4
    title_00003.mp4
    ..
    title_00010.mp4
    title_00011.mp4
    title_00012.mp4
    so number of digits (5) will remain the same??

    Thanks in advance

  2. #2
    Join Date
    Apr 2012
    Beans
    5,430

    Re: Renaming a batch of files

    If you are set on using your current loop solution, you could use printf to create the new name with a zero-padded number i.e. (simple illustration)

    Code:
    $ n=1; for i in {1..10}; do printf -v newname "title_%05d" $((i++)); echo "$newname"; done
    title_00001
    title_00002
    title_00003
    title_00004
    title_00005
    title_00006
    title_00007
    title_00008
    title_00009
    title_00010
    However you could just use the perl-based 'rename' command to substitute the original numbering e.g.

    Code:
    $ rename -vn 's/(\d+)-[^.]*/title_$1/' *.mp4
    00010-ffdsfselS.mp4 renamed as title_00010.mp4
    00011-fqAQHelS.mp4 renamed as title_00011.mp4
    00012-cKIaSnn1.mp4 renamed as title_00012.mp4
    00013-d6lha9ys.mp4 renamed as title_00013.mp4
    00014-eUyAzS7x.mp4 renamed as title_00014.mp4
    00015-nVFSjsiW.mp4 renamed as title_00015.mp4

  3. #3
    Join Date
    Mar 2010
    Location
    Metro-ATL
    Beans
    Hidden!
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Renaming a batch of files

    There is a regex rename tool already ... called rename.
    rename 's/[-].*$/.mp4/g' *mp4
    will remove the - to the end.

    Then rename 's/^/title_/g' *mp4
    will insert the beginning.

    Clear as mud? BTW, I didn't test this, but I use rename ALL_THE_TIME.

    Now, if you WANT to retain your method, we need to know which scripting language are you using.

    I like steeldrivers answer better. I didn't remember the use of substitutions off the top of my head. Pretty slick, huh?
    Last edited by TheFu; September 19th, 2013 at 01:11 AM.

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,707
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Renaming a batch of files

    EDIT: this solves your sequence output, but not your renaming problem.

    Hi alfirdaous.

    You can use 'seq' to generate your number's sequence. For instance:
    Code:
    $ for n in $(seq 10); do echo $n; done
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    If you set a format you can keep all number with the same amount of digits and proper padding zeros:
    Code:
    $ for n in $(seq -f "%02.0f" 10); do echo $n; done
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    In your case you'd need something like "%05.0f"

    Hope it helps. Let us know how it goes.
    Regards.
    Last edited by papibe; September 19th, 2013 at 01:20 AM.

  5. #5
    Join Date
    Apr 2012
    Beans
    5,430

    Re: Renaming a batch of files

    Just for completeness, the built-in bash sequence generator will pad field widths as well i.e.

    Code:
    $ for i in {0006..0012}; do echo $i; done
    0006
    0007
    0008
    0009
    0010
    0011
    0012

  6. #6
    Join Date
    May 2012
    Beans
    304

    Re: Renaming a batch of files

    Quote Originally Posted by steeldriver View Post
    If you are set on using your current loop solution, you could use printf to create the new name with a zero-padded number i.e. (simple illustration)
    Thanks in advance, I did not get the printf way

    Quote Originally Posted by TheFu View Post
    There is a regex rename tool already ... called rename.
    In this case I need to use 2 commands line

    Quote Originally Posted by papibe View Post

    EDIT: this solves your sequence output, but not your renaming problem.
    I liked the code, quite simple, but still don't understand the printf

    Another thing, If I would like to increment the names, for exemple:

    0001 ==> 0005
    0002 ==> 0006
    0003 ==> 0007

    and so on

  7. #7
    Join Date
    Apr 2012
    Beans
    5,430

    Re: Renaming a batch of files

    Well you basically have 3 approaches available to you

    1. (as per your original post) loop over the *.mp4 files using a shell glob, and separately increment a counter that will form the basis of the new file name. In that case, if you want the number to have a particular format, you can do that using 'printf'. The bash printf builtin is usually used to write formatted output to the terminal, but the optional form 'printf -v var ... ' allows you to write it to shell variable var, which you can use in your script. Type 'help printf' at the bash prompt for more info.
    2. you can create a suitable numeric sequence in the desired (zero padded) format using either 'seq -f' or like 'for n in {00000...00199}', and then look for a file with that number in its name, and rename it
    3. you can use rename (or sed) to create the new name from the old name by substitution

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,346
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Renaming a batch of files

    printf is a classic command/function used in many languages. Format string has % placeholders in which values of provided parameters are put. %05d is a placeholder that means 0-padded 5 digits wide decimal number, so eg
    Code:
    i=5; printf "filename-%05d" $i
    will produce "filename-00005" string. optionally -v variable stores the result in variable

    and if you want to shift numbers, rename is out of question - regular expressions rock but they don't know concept of math (it's all characters)
    Assuming transition from 99999-garbage.mp4 to title_99999.mp4, you'd need something like
    Code:
    for f in *.mp4; do n=${f%%-*}; n=$(( 10#$n+4 )); mv "$f" "$( printf "title_%05d.mp4" "$n" )"; done
    n=${f%%-*} = strip rightside '-'+anything from filename, leaving the digits
    n=$(( 10#$n+4 )) = by default leading 0s suggest octal number, so 10# makes sure it is considered decimal, add 4.

    but be warned - if you want to change numbers by adding N but preserve the filename format you need to be careful. Going in ascending order means overwriting files with higher indices ( file-0001 becomes file-0005 overwriting old file-0005 before it gets processed), you need to go in descending order or use temporary format to avoid naming collisions
    Last edited by Vaphell; September 19th, 2013 at 05:47 AM.
    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

  9. #9
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    674
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Renaming a batch of files

    This is pretty cool, although I don't understand how it works.
    For a long time I've been wanting to be able to batch rename photos from the command line.

    If I have a bunch of photos with names of various lengths and ending in .jpg or .JPG,
    how could I rename the entire batch to something like September-19-2013-1.jpg September-19-2013-2.jpg etc?
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,346
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Renaming a batch of files

    does order or any part of original filenames matter?
    if not you simply do something like

    Code:
    i=1; for f in *.[jJ][pP][gG]; do mv -- "$f" "$( printf "September-19-2013-%04d.jpg" $i )"; ((i++)); done
    printf is only for 0-padding so if you dont need it you can simply use plain "September-19-2013-$i.jpg" instead
    how it works: take all files matching given glob and rename them in loop to format with $counter plugged in, increment $counter after each file

    Code:
    $ ls
    garbage.jpg  something.JPG  trolololo.JPG
    $ i=1; for f in *.[jJ][pP][gG]; do mv -- "$f" "$( printf "September-19-2013-%04d.jpg" $i )"; ((i++)); done
    $ ls
    September-19-2013-0001.jpg  September-19-2013-0002.jpg  September-19-2013-0003.jpg
    Last edited by Vaphell; September 19th, 2013 at 06:58 AM.
    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

Page 1 of 3 123 LastLast

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
  •