Results 1 to 6 of 6

Thread: Renaming files

  1. #1
    Join Date
    Jan 2007
    Location
    Toronto ON Canada
    Beans
    1,127
    Distro
    Xubuntu

    Renaming files

    I have a normal enough task to complete - renaming some files. Unfortunately I seem to be unable to come up with a command string that will accomplish what LOOKS to be a simple operation. I have multiple(!) files of the form ## - Song Name.mp3 (eg: 01 - Season of the Witch) where the number refers to the track number on the CD it came from. When I create a 'set' for in-car use, I do not need, or want, any track numbering.

    It seems that mv tries to find a directory to move it to - and that rename requires Perl knowledge to even operate it - knowledge I don't have. There are a least 3 ways I could do this in AmigaDOS - 2 of them requiring only a single command, and the other involves having the list command write a script for me to execute - and thus is 2 commands. Anything I have puzzled out so far on my terminal - OR on Nautilus - seems to involve hundreds of actions.. so far! Anything else will take a long time to puzzle out a script with sed and cut and awk and...?

    Any experts out there - can you give something simple (preferably) or at least working and comprehensible (to allow changes where necessary) to get this done a little more effectively?

    Thanks in advance to whichever guru meditates on this....


    PS: If anyone is interested, here is what would work in AmigaDOS:

    move ?????#?.mp3 #?.mp3 // first 5 characters, then match 'anything' - to 'anything' without the 5 (obviously you don't want any .mp3 files WITHOUT the 5 leading characters in the directory when you do this!)

    One could also match the numbers (0-9|) and strip them, and match ' - ' and leave it off too... OR
    use list to find the files, and output them to a file including a rename command with both versions of the file name filled in - then execute the file (AmigaDOS doesn't require a chmod to make a file executable).

    Not as fancy, or in the end quite as capable as full regex and all the tools - but a lot less confusing too!
    Last edited by freebird54; March 31st, 2014 at 04:53 AM. Reason: minor oops
    | Xubuntu 20.04 / Arch rolling XFCE/ MX-19 patito feo / Arcolinux 20.5.7 rolling / EndeavourOS rolling XFCE / Ubuntu Unity 20.04 / Arch rolling Cinnamon / EndeavorsOS rolling Budgie / Arch rolling XFCE-compiz / Kubuntu 20.04 |

  2. #2
    Join Date
    Nov 2011
    Beans
    2,336
    Distro
    Ubuntu

    Re: Easy in AmigaDOS CLI - possible in Linux?

    If the filename contains spaces, mv will see each word as a separate argument. Try escaping it -- put quotes around it.

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

    Re: Easy in AmigaDOS CLI - possible in Linux?

    You could do something similar to your Amiga examples with bash parameter substitution (although a loop is needed, afaik)

    Code:
    $ for file in *.mp3; do mv -v -- "$file" "${file#[0-9][0-9] - }"; done
    `01 - Season of the Witch.mp3' -> `Season of the Witch.mp3'
    (strips the leading 2-digits-plus-space-hyphen-space from any mp3 files that have it - mv will skip any that don't match)

    The perl syntax in the rename command is not too bad

    Code:
    $ rename -v -- 's/^\d+\s-\s//' *.mp3
    01 - Season of the Witch.mp3 renamed as Season of the Witch.mp3
    1999 - The Music of Canada.mp3 renamed as The Music of Canada.mp3
    (match 1 or more digits followed by whitespace-hyphen-whitespace at the start of the name, and replace it with nothing), and rename supports older non-perl specific regex syntax if you're more comfortable with that e.g.
    Code:
    rename -v -- 's/^.....//' *.mp3          # match and replace (cut) five leading characters
    
    rename -v -- 's/^.{5}//' *.mp3           # match and replace (cut)five leading characters (alternative)
    
    rename -v -- 's/^[0-9]+ - //' *.mp3      # uses older style [0-9] instead of Perl \d and literal space instead of more general \s
    Last edited by steeldriver; March 26th, 2014 at 02:21 PM. Reason: tidied up and re-ordered

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

    Re: Renaming files

    i'd add that if you are going to experiment with rename you can test the results without making changes with -n parameter, so you can get the hang of it without risking anything.

    on a sidenote, spending few hours to learn basics of regular expressions is one of the best investments one could possibly make and they are really not that confusing.
    Last edited by Vaphell; March 26th, 2014 at 02:46 PM.
    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

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

    Re: Renaming files

    Good point Vaphell - I actually REMOVED the -n switches before posting because I always seem to get people posting back "I ran your commands and nothing happened"...

    Also PLEASE point out if I have made any obvious snafus on the bash side of things

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

    Re: Renaming files

    heh when i post snippets that can go really wrong, usually i default to dry runs but explicitly mention it, even marking with bold/color which parts of code act as a safeguard (echos in front of mv/rm, -n in rename, etc).

    obvious snafus in bash? not seeing anything, though i thought about something like this
    Code:
    for file in [0-9][0-9]\ -\ *.mp3; do mv -v -- "$file" "${file#*- }"; done
    filtering done on the glob level which cuts down the number of calls and allows for lazier substitution within the loop, though it's more academic than anything
    Last edited by Vaphell; March 26th, 2014 at 02:59 PM.
    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

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
  •