Results 1 to 9 of 9

Thread: Help with script to mass rename 1k+ directories

  1. #1
    Join Date
    Jan 2006
    Beans
    Hidden!

    Post Help with script to mass rename 1k+ directories

    I just noticed that all my old photos on my backup are stored in some different directory naming conventions than my newer ones and renaming everything by hand... well lets just say that there is 1000 plus directories that needs fixing and I've let myself become extremely rusty in scripting

    Does anyone have time on their hands to write a small script that mass renames them for me?

    What I got:
    (YEAR) XXX YYY
    XXX YYY (YEAR)

    How they should be named:
    YEAR - XXX YYY


    Examples:

    From this:
    (2001) International Space Station (ISS)
    My Dark Basement (1984)

    To this:
    2001 - International Space Station (ISS)
    1984 - My Dark Basement

    There's a few [ ] and dashes in the directory names too I think (they don't need changing - just a warning if it messes with the script). The files are fine too (DDMMYYYY.xxx) so it's just the directories that need fixing. I tried googling but only found file mass renaming.

    It's on a headless server, so no GUI tools

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

    Re: Help with script to mass rename 1k+ directories

    always 4digits in ()?
    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

  3. #3
    Join Date
    Jan 2006
    Beans
    Hidden!

    Re: Help with script to mass rename 1k+ directories

    Yes the year is always in four digits

    EDIT: Uh, forgot to say that some directories have no year at all (and hence need no fixing).
    Last edited by TheForumTroll; November 23rd, 2012 at 03:28 AM. Reason: added info

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

    Re: Help with script to mass rename 1k+ directories

    The rename command works pretty well for stuff like this - like Vaphell implies, you can refine the expression if you need to be more (or less) specific about the match condition e.g.'[0-9]{4}' (exactly 4 digits) instead of '[0-9]+' (1 or more digits)

    Code:
    $ mkdir "(2001) International Space Station (ISS)"
    $ mkdir "My Dark Basement (1984)"
    $
    $ rename -nv 's/\(([0-9]+)\) (.*)/$1 - $2/' */
    (2001) International Space Station (ISS)/ renamed as 2001 - International Space Station (ISS)/
    $
    $ rename -nv 's/(.*) \(([0-9]+)\)/$2 - $1/' */
    My Dark Basement (1984)/ renamed as 1984 - My Dark Basement/
    You can use globstar if you need to do it recursively on subdirectories

  5. #5
    Join Date
    Jan 2006
    Beans
    Hidden!

    Re: Help with script to mass rename 1k+ directories

    Hmm, I don't know why but I get no output out of the script at all and nothing changes


    EDIT: Oh, there's two different lines. Doh. Let me try again
    EDIT 2: Nope, no luck. One line does nothing, the other says it changes it but doesn't.
    Last edited by TheForumTroll; November 23rd, 2012 at 03:58 AM. Reason: Update

  6. #6
    Join Date
    Jan 2010
    Location
    TN USA
    Beans
    398
    Distro
    Xubuntu

    Re: Help with script to mass rename 1k+ directories

    The -n option to rename shows what the command would do but doesn't actually do it. Once you are happy with the command, remove the -n option.

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!

    Re: Help with script to mass rename 1k+ directories

    Oh, that will teach me to look more closely at a script before running it

    It doesn't go into subdirs and change it, even with globstar on though.
    Last edited by TheForumTroll; November 23rd, 2012 at 04:20 AM.

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

    Re: Help with script to mass rename 1k+ directories

    Code:
    rename -nv 's/(.*) \(([0-9]+)\)/$2 - $1/'
    this will probaly fail with paths

    Code:
    $ rename -nv 's/(.*) \(([0-9]+)\)/$2 - $1/' ../mass_rename/*(*)*
    ../mass_rename/abcdef ghi XoXoXo (2063) renamed as 2063 - ../mass_rename/abcdef ghi XoXoXo
    standard while read
    Code:
    #!/bin/bash
    
    while IFS= read -rd $'\0' d
    do
      p=${d%/*}; nd=${d##*/}
      if [[ $nd =~ [(][0-9]{4}[)]$ ]]
      then
    #    yr=${nd##*(}; yr=${yr%?}
    #    nd="$yr - ${nd% *}"
    #    echo mv -v "$d" "$p/$nd"
        rename -nv 's|(.*)/(.*) \(([0-9]+)\)|$1/$3 - $2|' "$d"
      elif [[ $nd =~ ^[(][0-9]{4}[)] ]]
      then
    #    yr=${nd%%)*}; yr=${yr#?}
    #    nd="$yr - ${nd#* }"
    #    echo mv -v "$d" "$p/$nd"
        rename -nv 's/\(([0-9]+)\) (.*)/$1 - $2/' "$d"
      fi
    done < <( find . -iname '*([0-9][0-9][0-9][0-9])*' -type d -print0 )
    as a bonus pure bash manipulation in comments.
    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
    Jan 2006
    Beans
    Hidden!

    Re: Help with script to mass rename 1k+ directories

    That's....beautiful

    Thank you. I had to run it more than once (it doesn't enter a directory after it has been renamed and rename directories in it) but it works. So much time saved. I owe you a beer

Tags for this Thread

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
  •