Renaming files using "rename"

Discussion in 'all things UNIX' started by vasa1, Dec 9, 2011.

Thread Status:
Not open for further replies.
  1. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    I have several files with spaces in their names. I wanted to replace the spaces with underscores:
    For example, I used
    Code:
    rename -n 's/ /_/g' *.htm
    to get
    Code:
    02 09 dp.htm renamed as 02_09_dp.htm
    02 10 dp.htm renamed as 02_10_dp.htm
    02 11 dp.htm renamed as 02_11_dp.htm
    In the code I know that "s" stands for substitute but what is "g"? (It should be clear that I don't know perl but I know that spaces in filenames can be bad things in *nix.)

    This is with Ubuntu.

    Edit: -n does a mock run to let one know what will happen without actually doing anything. It can be replaced with -v (for verbose) or just removed when doing the real thing.
     
    Last edited: Dec 10, 2011
  2. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    Okay, it's a modifier:
    I guess it's needed to take care of multiple spaces?

    Edit: Oops! That was a modifier for the match operator.
    Code:
    g 	Replaces all occurrences of the found expression
    	with the replacement text
    is for the substitution operator!
     
    Last edited: Dec 9, 2011
  3. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    Nobody to share their perls of wisdom? :(

    I couldn't resist :D
     
  4. iceni60

    iceni60 ( ^o^)

    Joined:
    Jun 29, 2004
    Posts:
    5,116
    Code:
    rename ' ' '_' *.htm
    i hate giving stuff like that without trying it first. so please make a backup first :D

    you could try sed too, or thunars bulk renamer, i really like that.

    EDIT. i see they are file names not contents. i was going to just say use gedit that lets you replace charactors lol
     
    Last edited: Dec 10, 2011
  5. Mrkvonic

    Mrkvonic Linux Systems Expert

    Joined:
    May 9, 2005
    Posts:
    10,300
    I prefer doing it like this, either singly or in loop:

    PARAM=`echo "$ORIG" | sed "what i want to replace"`

    Then, if I'm satisfied:

    mv $ORIG $PARAM

    That way I avoid the tiny regex errors and whatnot.

    Cheers,
    Mrk
     
  6. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    Does that work for you? In what context and with what OS? It doesn't do anything for me on Ubuntu 11.10.

    As far as I can tell, the Debian version of rename requires a perl expression:
    Code:
    SYNOPSIS
           rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
     
  7. iceni60

    iceni60 ( ^o^)

    Joined:
    Jun 29, 2004
    Posts:
    5,116
    erm, no. i didn't really think, sorry. i checked this out though and it should work. i wrote it and checked it on other sites o_0
    Code:
    for FILE in *.html ; do NEW=`echo $FILE | sed 's/ /_/g'` ; mv "$FILE" $NEW ; done
    it's driving me a bit crazy. what's the simplest way to run the command using renameo_O?
     
    Last edited: Dec 11, 2011
  8. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    No problems! I'll keep sed and awk for another time. Right now I'm quite happy learning to rename a lot of files just the way I like using rename and a smattering of regex. I just managed to change ddmmyy.* to yyyymmdd.* for a bunch of stuff (all of this century ;) ).

    The next frontier for me is doing such things recursively as opposed to folder-wise.
     
  9. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    As I go along, I find that there are at least three related beasts: rename, prename and rename.ul. The last uses a much simpler command than rename and looks very much like what iceni60 has given above. I have to digest this for a while!
     
  10. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    According to this link,
    oldName001.mat, oldName002.mat, ..., oldName200.mat can be renamed to newName001.mat, newName002.mat, ..., newName200.mat
    using rename
    Code:
    rename "s/oldName/newName/" *mat
    but using rename.ul seems simpler
    Code:
    rename.ul oldName newName *mat
    I don't know how more complex renamings will be handled by the latter.
     
  11. iceni60

    iceni60 ( ^o^)

    Joined:
    Jun 29, 2004
    Posts:
    5,116
    part of the reason i'm posting a little more is because i'm starting to learn linux again, i've been using it everyday, but it's easy to watch films/TV and documentaries. with a bit of luck i may give some more sensible answers lol
     
  12. iceni60

    iceni60 ( ^o^)

    Joined:
    Jun 29, 2004
    Posts:
    5,116
  13. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
  14. lotuseclat79

    lotuseclat79 Registered Member

    Joined:
    Jun 16, 2005
    Posts:
    5,390
    Hi vasa1,

    The g at the end of the substitution command actually replaces all occurrences of the found expression with the replacement text multiple times on the same line given it exists multiple times on the same line. That is what they mean by global substitution of g for the range of lines the command has been given - instead of simply replacing only one instance on each line the expression exists which is the default when the g is not used.

    -- Tom
     
  15. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,417
    Thanks, Tom :thumb:
     
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.