Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Renaming Multiple files at once in Terminal

  1. #1
    Join Date
    Jan 2011
    Beans
    8

    Renaming Multiple files at once in Terminal

    Hello i am very new to Ubuntu and programing in general. i am trying to rename files in terminal. I would like to rename a list of mp3 files from Track 1, track 2, ect. to LOTR_01-01, LOTR_01-02 ect. I have a program in windows that does this for me but figure i should be able to do this in terminal as well. If there are any programs that will do the same in bulk on Unbuntu 10.10 that would help as well.

  2. #2
    Join Date
    Aug 2010
    Location
    Central Florida
    Beans
    121
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Renaming Multiple files at once in Terminal

    Hi bazinga,

    There is a much easier way of renaming files.

    Places>Home Folder

    Navigate to the folder where the files you want to rename are.

    Right click on the file that you want to rename.

    Select rename from the menu.

    If you want to learn how to do it in the terminal (CLI)
    open the terminal and enter
    Code:
    man mv
    Hope that helps...
    Hp Compaq Core2 Duo 4G-DDR2 2.16GHz Ubuntu 64bit Gnome, LMDE64 (squeeze, testing, sid), Debian 6.0

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

    Re: Renaming Multiple files at once in Terminal

    Code:
    #!/bin/bash
    
    if [ "$1" ]
    then
      label="$1"
    else
      echo missing parameter \(label\)
      exit 1
    fi
    
    for f in *.mp3
    do
      if [ $( echo "$f" | grep -E -c '[0-9]+\.mp3$' ) == "0" ]
      then
        echo $f - skipped, track number not found
        continue
      fi
      i=${f%.mp3}
      i=${i##*[^0-9]}
      mv -v "$f" "$label"-$( printf '%02g' $i ).mp3
    done
    it takes label as a parameter, for each .mp3 file it hunts for the last sequence of digits before .mp3 and assumes this is the track number, normalizes # to 2digits and renames the file to LABEL-##.mp3. Maybe not perfect but should work in trivial cases where there are no conflicts in track numbers.
    Last edited by Vaphell; January 24th, 2011 at 07:10 PM. Reason: script upgrade

  4. #4
    Join Date
    Aug 2007
    Beans
    160

    Re: Renaming Multiple files at once in Terminal

    I'd suggest to try this step by step, as it may prove useful in the future.
    In your case this will do the job:
    Code:
    $ rename 's/ /_/' *.mp3
    Replaces spaces in the filenames with underscore.
    Code:
    $ N=1; for i in *.mp3; do echo mv $i TRACK_$N.mp3; N=$[N+1]; done
    N: Number to begin counting from
    echo: after executing you will see exactly what is "feeded" to terminal; useful to avoid problems. To actually rename files using mv remove "echo" from the line.
    However I'd suggest reading this beforehead, as it may prove quite useful to you http://tips.webdesign10.com/how-to-b...n-the-terminal

  5. #5
    Join Date
    Jan 2011
    Beans
    8

    Re: Renaming Multiple files at once in Terminal

    Thank You PunkLV and Vaphell. These look like the type of solutions I was looking for. I will try later and repost my results.

  6. #6
    Join Date
    Sep 2008
    Location
    South Australia
    Beans
    58
    Distro
    Xubuntu 10.10 Maverick Meerkat

    Re: Renaming Multiple files at once in Terminal

    I'm trying to do something similar from work through ssh to rename tv series episodes.

    for example
    Code:
    sudo mv The.Simpsons.S08E06.DVDRip.XviD-TOPAZ.avi TS-S08E06.avi
    except there are 22 seasons of the simpsons and 22 episodes in some seasons.
    How do I do that?

    Edit:
    Ok I got my head around rename
    Code:
    sudo rename 's/the.simpsons.03x/TS-S03E/' *.avi
    but how do I remove exeverything inbetween the TS-S03E(2 numbers here) and the .avi?

    I need something like
    Code:
    sudo find /home/share2/TV_Series/ -name '[NAME]-S[0-9][0-9]E[0-9][0-9][EPISODE NAME].avi -exec mv {} [NAME]-S[0-9][0-9]E[0-9][0-9].avi
    I know that wont work but I need something that does that please?
    Last edited by Teh_Messiah; July 19th, 2011 at 04:08 AM.
    (\_/) Uber
    (o.o) Xubuntu
    (___)0 Tech!

  7. #7
    Join Date
    Sep 2008
    Location
    South Australia
    Beans
    58
    Distro
    Xubuntu 10.10 Maverick Meerkat

    Re: Renaming Multiple files at once in Terminal

    Ok
    Code:
    sudo find . -name '*S[0-9][0-9]E[0-9][0-9]*.avi'
    actually worked
    (\_/) Uber
    (o.o) Xubuntu
    (___)0 Tech!

  8. #8
    Join Date
    Aug 2007
    Beans
    160

    Re: Renaming Multiple files at once in Terminal

    Now officially you are one step closer to the abyss that is RegExp.
    Enjoy the fall into botomless pit of variations and limitless creativity. Quite useful though.

  9. #9
    Join Date
    Sep 2008
    Location
    South Australia
    Beans
    58
    Distro
    Xubuntu 10.10 Maverick Meerkat

    Re: Renaming Multiple files at once in Terminal

    Finally after being bugged by this for so long and finally looking up and researching regex stuff I got my head around it.

    To rename files like:
    series.name.s01e01.episode.title.avi to SN-S01E01.avi
    series.name.s01e02.episode.title.avi to SN-S01E02.avi
    series.name.s01e03.episode.title.avi to SN-S01E03.avi
    series.name.s01e04.episode.title.mpg to SN-S01E04.mpg

    I used this!
    Code:
    sudo rename -n 's/series\.name\.s(\d{2})e(\d{2}).+(\.\D{3})/SN-S$1E$2$3/' *
    Note: I am using -n to make only a test and see if the result is what I want
    \. is making it so it reads a . as text not regex code.
    What is in between the ()'s become a string value which is reflected in order of appearance as
    $1 = whats in the 1st set of ()
    $2 = whats in the 2nd set of ()
    $3 = whats in the 3rd set of ()
    The dot or . or (period, if you wish) can be taken to match any character whatsoever!
    The + sign tells Perl to 'match one or more of the preceding character'.
    Once you have received the desired output from the test run change the -n to -v so you can verbosely see what it has done.
    It is possible to use it without using -v but if you make a mistake this is your only record of how what was moved to the new what.
    I used information gathered from these sites.
    REGEX (LINK)
    Rename multiple files with Linux (LINK)
    (\_/) Uber
    (o.o) Xubuntu
    (___)0 Tech!

  10. #10
    Join Date
    Mar 2011
    Beans
    110

    Re: Renaming Multiple files at once in Terminal

    I always use sed to make formulate commands and pipe those to a shell.

    for example:
    Code:
    marvink@M-MWK-Ubuntu:~/testing$ ls
    series.name.s01e01.episode.title.avi  series.name.s01e03.episode.title.avi
    series.name.s01e02.episode.title.avi  series.name.s01e04.episode.title.avi
    marvink@M-MWK-Ubuntu:~/testing$ ls | sed 's|^\([a-Z]\)[a-Z]*\.\([a-Z]\)[a-Z]*\.\(s[0-9]\{2\}e[0-9]\{2\}\)\..*$| mv & \1\2-\3.avi|'
     mv series.name.s01e01.episode.title.avi sn-s01e01.avi
     mv series.name.s01e02.episode.title.avi sn-s01e02.avi
     mv series.name.s01e03.episode.title.avi sn-s01e03.avi
     mv series.name.s01e04.episode.title.avi sn-s01e04.avi
    marvink@M-MWK-Ubuntu:~/testing$ ls | sed 's|^\([a-Z]\)[a-Z]*\.\([a-Z]\)[a-Z]*\.\(s[0-9]\{2\}e[0-9]\{2\}\)\..*$| mv & \1\2-\3.avi|' | sh
    marvink@M-MWK-Ubuntu:~/testing$ ls
    sn-s01e01.avi  sn-s01e02.avi  sn-s01e03.avi  sn-s01e04.avi
    many roads lead to rome

Page 1 of 2 12 LastLast

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
  •