Results 1 to 9 of 9

Thread: Rename episode files

  1. #1
    Join Date
    Feb 2007
    Beans
    60
    Distro
    Ubuntu 11.04 Natty Narwhal

    Rename episode files

    All,

    I have a directory filled with many subdirectories of tv episodes. I am looking to get to an end state with a simple script that leaves me with the season/episode number of sxxexx. Right now the majority of the files (but not all) are formatted showname - 1x23 - epname.ext. I have a Regex that identifies files that include the numbering pattern. [0-9]*[0-9]x[0-9][0-9] this catches episodes without the 01x01 pattern. I am unsure about how to make this rename to the desired pattern. Any help would be appriciated.

  2. #2
    Join Date
    Feb 2008
    Beans
    5,636

    Re: Rename episode files

    Personal opinion: it would be too dangerous to do it with a script - you could create a big mess of unrecognizable video files.
    Install thunar and look at its tool Bulk Rename.

  3. #3
    Join Date
    Feb 2007
    Beans
    60
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Rename episode files

    This is going to be a good way for me to learn some bash scripting so I would like to move forward. I can test in small folders in the meanwhile.

    Struggling trying to figure out how to set a variable. When I run the following command without trying to set a variable it gets me exactly what I want. But the following

    Code:
    episodes=$(ls -R | grep [0-9]*[0-9]x[0-9][0-9])
    just gets me

    Code:
    media:~/Movies/TV$ $episodes
    2: command not found
    At work I primarily script in Powershell so I am still learning the ways of bash. Is it object oriented?

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Rename episode files

    There's a very helpful GUI application called pyRenamer, that is perfect for that. It is on the Software Center.

    Regards.

  5. #5
    Join Date
    Feb 2007
    Beans
    60
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Rename episode files

    This is a headless server and I am looking to learn some bash scripting in the meanwhile. Thanks though

  6. #6
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Rename episode files

    Have done that myself, I would suggest a few things from experience:
    • Start by backing up your data.
    • Test your code by echoing the commands instead of executing them.
    • It is easier to write and run a few simple scripts, that try to design and write the perfect script that take care of all cases.

    Here an skeleton script that can get you started. This script change the file names to all caps:
    Code:
    #!/bin/bash
    
    find -iname '*[0-9][0-9]x[0-9][0-9].*' | \
    while read -r filename
    do
        newfilename=${filename^^}  # key line for renaming
        echo mv -b -- "$filename" "$newfilename"
    done
    This is safe to execute since I'm echoing the 'mv' line, and not actually renaming anything.

    I hope this helps,
    Regards.

  7. #7
    Join Date
    Mar 2007
    Location
    Portsmouth, UK
    Beans
    Hidden!
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Rename episode files

    Quote Originally Posted by m3_del View Post
    All,

    I have a directory filled with many subdirectories of tv episodes. I am looking to get to an end state with a simple script that leaves me with the season/episode number of sxxexx. Right now the majority of the files (but not all) are formatted showname - 1x23 - epname.ext. I have a Regex that identifies files that include the numbering pattern. [0-9]*[0-9]x[0-9][0-9] this catches episodes without the 01x01 pattern. I am unsure about how to make this rename to the desired pattern. Any help would be appriciated.
    While my way is probably not particularly eloquent, here's what I might do. To rename the following files:

    Code:
    Chuck.The.Woodpecker.1x56.randomdata
    Chuck.The.Woodpecker.1x57.randomdata
    etc
    From the directory containing the files:

    Code:
    for f in *
    do echo $(echo "$f" | sed 's/Chuck\.The\.Woodpecker\.1x\([0-9][0-9]\)\.randomdata/chuck\.the\.woodpecker\.s01e\1\.newdata/'
    done
    Which will simply echo what the new file names would be. If you're happy with the new names, then you can simply change the initial echo to a mv:

    Code:
    for f in *
    do mv "$f" $(echo "$f" | sed 's/Chuck\.The\.Woodpecker\.1x\([0-9][0-9]\)\.randomdata/chuck\.the\.woodpecker\.s01e\1\.newdata/')
    done
    Which gets you:

    Code:
    chuck.the.woodpecker.s01e56.newdata
    chuck.the.woodpecker.s01e57.newdata
    etc
    Last edited by Grenage; October 3rd, 2011 at 09:10 AM.

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Rename episode files

    prename should do the trick:

    Code:
    prename -n 's/([0-9]x[0-9][0-9])/0$1/' *[!0-9][0-9]x[0-9][0-9]*.avi
    prename -n 's/(.*[0-9]x)([0-9].*avi)/${1}0$2/' *[0-9]x[0-9][!0-9]*.avi

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Rename episode files

    Quote Originally Posted by m3_del View Post
    This is going to be a good way for me to learn some bash scripting so I would like to move forward. I can test in small folders in the meanwhile.

    Struggling trying to figure out how to set a variable. When I run the following command without trying to set a variable it gets me exactly what I want. But the following

    Code:
    episodes=$(ls -R | grep [0-9]*[0-9]x[0-9][0-9])
    just gets me

    Code:
    media:~/Movies/TV$ $episodes
    2: command not found
    At work I primarily script in Powershell so I am still learning the ways of bash. Is it object oriented?
    ls shows you a representation of files. They aren't file names, so don't try to parse it.

    If you want to learn bash, check out the links in my signature.

    In bash, I'd try something similar to what Grenage suggested:
    Code:
    #!/bin/bash
    
    shopt -s nullglob
    
    for file in *[!0-9][0-9]x[0-9][0-9]*.avi
    do
        newname="$(printf '%s\n' "$file" | sed -e 's/\([0-9]x[0-9]\)/0\1/')"
        echo mv -- "$file" "$newname"
    done
    @papibe

    Check out BashFAQ #20. File names may contain newlines and other special characters. You should use find's -print0 option:
    Code:
    while IFS= read -r -d '' file
    do
        printf '%s\n' "${file^^}"
    done< <(find /path OPTIONS -print0)

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
  •